Behaviour of negative zero (-0.0) in comparison with positive zero (+0.0)

前端 未结 4 1720
夕颜
夕颜 2020-12-05 15:43

In my code,

float f = -0.0; // Negative 

and compared with negative zero

f == -0.0f

res

4条回答
  •  天涯浪人
    2020-12-05 16:20

    That's because the signed negative zero must compare true with zero: i.e. -0.0 == 0.0, -0f == 0f, and -0l == 0l.

    It's a requirement of any floating point scheme supported by a C++ compiler.

    (Note that most platforms these days use IEEE754 floating point, and this behaviour is explicitly documented in that specification.)

提交回复
热议问题