double minus double giving precision problems

后端 未结 2 673
春和景丽
春和景丽 2020-11-30 13:55

I have come across a precision issue with double in .NET I thought this only applied to floats but now I see that double is a float.

double test = 278.97 - 9         


        
2条回答
  •  暖寄归人
    2020-11-30 14:50

    This happens in many languages and stems from the fact that we cannot usually store doubles or floats precisely in digital hardware. For a detailed explanation of how doubles, floats, and all other floating point values are often stored, look at the various IEEE specs described on wikipedia. For example: http://en.wikipedia.org/wiki/Double_precision

    Of course there are other formats, such as fixed-point format. But this imprecision is in most languages, and why you often need to use epsilon tests instead of equality tests when using doubles in conditionals (i.e. abs(x - y) <= 0.0001 instead of x == y).

    How you deal with this imprecision is up to you, and depends on your application.

提交回复
热议问题