Comparing IEEE floats and doubles for equality

前端 未结 15 2326
南方客
南方客 2020-11-30 06:00

What is the best method for comparing IEEE floats and doubles for equality? I have heard of several methods, but I wanted to see what the community thought.

15条回答
  •  借酒劲吻你
    2020-11-30 06:49

    This seems to take care of most problems by combining relative and absolute error tolerance. Is the ULP approach better? If so, why?

    ULPs are a direct measure of the "distance" between two floating point numbers. This means that they don't require you to conjure up the relative and absolute error values, nor do you have to make sure to get those values "about right". With ULPs, you can express directly how close you want the numbers to be, and the same threshold works just as well for small values as for large ones.

提交回复
热议问题