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

前端 未结 5 1419
闹比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:28

    The problem is that with very big numbers, comparing to epsilon will fail.

    Perhaps a better (but slower) solution would be to use division, example:

    div(max(a, b), min(a, b)) < eps + 1
    

    Now the 'error' will be relative.

提交回复
热议问题