Addition of Double values inconsistent

后端 未结 7 2004
渐次进展
渐次进展 2021-01-20 01:13

I came across following issue while developing some engineering rule value engine using eval(...) implementation.

    Dim first As Double = 1.1
    Dim secon         


        
7条回答
  •  感情败类
    2021-01-20 01:27

    Equality comparisons with floating point operations are always inaccurate because of how fractional values are represented within the machine. You should have some sort of epsilon value by which you're comparing against. Here is an article that describes it much more thoroughly:

    http://www.cygnus-software.com/papers/comparingfloats/Comparing%20floating%20point%20numbers.htm

    Edit: Math.Round will not be an ideal choice because of the error generated with it for certain comparisons. You are better off determining an epsilon value that can be used to limit the amount of error in the comparison (basically determining the level of accuracy).

提交回复
热议问题