Unexpected loss of precision when dividing doubles

前端 未结 8 1964
慢半拍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:01

    I've tried with float instead of double and I get 10.845110 as a result. It still looks better than madalina result.

    EDIT:

    I think I know why you get this results. If you get a, b, c and d parameters from somewhere else and you print it, it gives you rounded values. Then if you put it to Mathemtacia (or calc ;) ) it will give you different result.

    I tried changing a little bit one of your parameters. When I did:

    double c = 2.7041304;
    

    I get 10.845806. I only add 0.0000004 to c! So I think your "errors" aren't errors. Print a, b, c and d with better precision and then put them to Mathematica.

提交回复
热议问题