Why does signbit (-0) return 0?

From the basic specifications of the Open Group Issue 7, IEEE Std 1003.1-2008:

The macro signbit () returns a nonzero value if and only if the sign of its argument value is negative.

Why signbit(-0) return 0 ? I just want to understand the logic of this decision.

+6
source share
3 answers

In signbit(-0) :

  • 0 is an int constant.
  • -0 is the result of negating 0 , so it is zero of type int .
  • This value is converted to a floating point.
  • The sign bit in the floating point value is zero, so signbit(-0) creates 0.

If you do signbit(-0.) Instead of:

  • 0. is a constant of type double .
  • -0. is the result of negating 0. therefore it is a negative zero of type double .
  • The sign bit in the floating point value is one, so signbit(-0.) Creates 1.

The key is that -0 negates the integer type, and integer types usually do not encode negative zero as opposed to positive zero. When an integer zero is converted to a floating point, the result is a simple (positive) zero. However, -0. negates floating point types, and floating point types encode negative zero distinctly from positive zero.

+10
source

In the two additions, which are by far the most common representation for signed integers these days, there is no such thing as negative zero. -0 == +0 in all cases, even bitwise. Thus, by the time the macro code processes it, even if it includes ((float) -0) , the sign is already gone.

If you want to test, you might be lucky with something like signbit(-0.0) or signbit(-1.0 * 0) . Since you are not converting from an integer at this moment, the number must still have a sign.

+11
source

This is not true. The signbit macro returns the alphanumeric character of a floating point bit. Pay attention to the text: "if the sign of the argument" is negative, and not "if the argument" is negative.

Footnote 236 in Standard C clarifies:

The signbit macro reports the sign of all values, including infinity, zeros, and NaN.

Is this a hypothetical question or do you have a mistake?

+2
source

All Articles