Should we compare floating point numbers for equality against a *relative* error?

前端 未结 5 1432
闹比i
闹比i 2020-11-30 09:13

So far I\'ve seen many posts dealing with equality of floating point numbers. The standard answer to a question like \"how should we decide if x and y are equal?\" is

<
5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-30 09:51

    As an alternative solution, why not just round or truncate the numbers and then make a straight comparison? By setting the number of significant digits in advance, you can be certain of the accuracy within that bound.

提交回复
热议问题