Rule of thumb to test the equality of two doubles in C#?

后端 未结 6 1918
忘掉有多难
忘掉有多难 2020-12-06 17:00

Let\'s say I have some code that does some floating point arithmetic and stores the values in doubles. Because some values can\'t be represented perfectly in binary, how do

6条回答
  •  生来不讨喜
    2020-12-06 17:01

    The question is, but many times greater??

    How much larger depends on the inputs and on how many operations your performing. In addition to magnitude considerations, every operation increases your rounding error. If your doing many calculations on the numbers before comparing them it's more a matter of significant digits rather than machine accuracy.

    Your window needs to be larger than the worst-case rounding error accumulated as a result of any calculations done. If it's smaller, there may be stray situations where your comparison fails.

提交回复
热议问题