Does float have a negative zero? (-0f)

后端 未结 10 752
离开以前
离开以前 2020-11-28 09:36

IEEE floating point numbers have a bit assigned to indicate the sign, which means you can technically have different binary representations of zero (+0 and -0). Is there an

10条回答
  •  天命终不由人
    2020-11-28 10:38

    There are a couple of simple arithmetic operations that result in a negative zero answer (at least on the i386/x64/ARMv7/ARMv8 systems I tested it on) :

    • -1 * 0
    • 0 / -1

    These caught me by surprise when I was writing an optimiser to simplify arithmetic expressions. Optimising "a = b * 0" to "a = 0" will result in the wrong answer (+0) if b happens to be negative (correct answer is -0).

提交回复
热议问题