Compare two floats

前端 未结 5 1589
小蘑菇
小蘑菇 2020-12-03 01:39
#include 

bool Equality(double a, double b, double epsilon)
{
  if (fabs(a-b) < epsilon) return true;
  return false;
}

I trie

5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-03 02:36

    Look here: http://floating-point-gui.de/errors/comparison/

    Due to rounding errors, most floating-point numbers end up being slightly imprecise. As long as this imprecision stays small, it can usually be ignored. However, it also means that numbers expected to be equal (e.g. when calculating the same result through different correct methods) often differ slightly, and a simple equality test fails.

    And, of course, What Every Computer Scientist Should Know About Floating-Point Arithmetic

提交回复
热议问题