Unexpected loss of precision when dividing doubles

前端 未结 8 2001
慢半拍i
慢半拍i 2021-02-09 14:33

I have a function getSlope which takes as parameters 4 doubles and returns another double calculated using this given parameters in the following way:

double QSw         


        
8条回答
  •  南笙
    南笙 (楼主)
    2021-02-09 15:23

    The following code:

    #include 
    using namespace std;
    
    double getSlope(double a, double b, double c, double d){
        double slope;
        slope=(d-b)/(c-a);
        return slope;
    }
    
    int main( ) {
        double s = getSlope(2.71156, -1.64161, 2.70413, -1.72219);
        cout << s << endl;
    }
    

    gives a result of 10.8452 with g++. How are you printing out the result in your code?

提交回复
热议问题