Comparing floating point number to zero

后端 未结 9 998
时光说笑
时光说笑 2020-11-28 23:04

The C++ FAQ lite \"[29.17] Why doesn\'t my floating-point comparison work?\" recommends this equality test:

#include   /* for std::abs(double) *         


        
9条回答
  •  被撕碎了的回忆
    2020-11-28 23:17

    notice, that code is:

    std::abs((x - y)/x) <= epsilon
    

    you are requiring that the "relative error" on the var is <= epsilon, not that the absolute difference is

提交回复
热议问题