How do I check if a zero is positive or negative?

前端 未结 7 1316
孤街浪徒
孤街浪徒 2020-12-01 06:50

Is it possible to check if a float is a positive zero (0.0) or a negative zero (-0.0)?

I\'ve converted the float to a String a

相关标签:
7条回答
  • 2020-12-01 07:54

    Yes, divide by it. 1 / +0.0f is +Infinity, but 1 / -0.0f is -Infinity. It's easy to find out which one it is with a simple comparison, so you get:

    if (1 / x > 0)
        // +0 here
    else
        // -0 here
    

    (this assumes that x can only be one of the two zeroes)

    0 讨论(0)
提交回复
热议问题