Can we rely on op== to binary-compare floating-point values?

后端 未结 3 1809
轻奢々
轻奢々 2020-12-17 16:31

We all know (right?!) that one should not compare floating-point values by testing for equality (operator==).

But what if I actually want to determine w

3条回答
  •  無奈伤痛
    2020-12-17 16:33

    (Assuming IEEE-754 representations) almost, but not quite. If you can rule out NaNs, you still need to deal with the fact that +0.0 and -0.0 have different binary encodings, but compare equal (because both are exactly zero).

    Of course, C++ doesn't require IEEE-754. So strictly speaking, all bets are off.

    If you want to check for (in)equality of encoding, just use memcmp(&a, &b, sizeof a).

提交回复
热议问题