Is it ok to compare floating points to 0.0 without epsilon?

后端 未结 5 1635
余生分开走
余生分开走 2020-12-17 10:39

I am aware, that to compare two floating point values one needs to use some epsilon precision, as they are not exact. However, I wonder if there are edge cases, where I don\

5条回答
  •  北海茫月
    2020-12-17 11:20

    Yes, if you return 0.0 you can compare it to 0.0; 0 is representable exactly as a floating-point value. If you return 3.3 you have to be a much more careful, since 3.3 is not exactly representable, so a conversion from double to float, for example, will produce a different value.

提交回复
热议问题