Unexpected loss of precision when dividing doubles

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

    Better Print out the arguments, too. When you are, as I guess, transferring parameters in decimal notation, you will lose precision for each and every one of them. The problem being that 1/5 is an infinite series in binary, so e.g. 0.2 becomes .001001001.... Also, decimals are chopped when converting an binary float to a textual representation in decimal.

    Next to that, sometimes the compiler chooses speed over precision. This should be a documented compiler switch.

提交回复
热议问题