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

前端 未结 7 1319
孤街浪徒
孤街浪徒 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)

提交回复
热议问题