Mathematicians truly enjoy doing their math thing. As a mortal human, I don’t have a spot for math things on my “fun” spectrum. Yet, one of the more jolly things the math nerds do is discuss the value zero.

For example, I’ve learned that zero is considered an even number. I also learned that zero isn’t the same thing as infinity. But when it comes to being positive or negative, mathematicians assert that zero is neither. This assertion isn’t held by some programming languages.

Yes, Java, I’m looking at you.

But this blog is about the C programming language, which doesn’t really have a hangup regarding negative zero or positive zero. Or does it?

From last week’s Lesson, I covered the sign bit. This bit determines which values are negative for signed integer variables. Zero is `00000000`

in binary. But `10000000`

is the value -128, not -0. This approach is how all integers work.

Floating point values are also stored with a sign bit. Their bitwise arrangement is more complex than an integer, with intricate details I don’t want to get into. Still, for a real number (*float* or *double*) in C, it’s theoretically possible to have a positive or negative value of zero. The question is whether the C language recognizes the difference.

In the following code, I use the sign operator to compare the values zero, positive zero, and negative zero.

### 2021_04_10-Lesson.c

#include <stdio.h> int main() { float n,p,z; n = -0.0; p = +0.0; z = 0.0; if( z==z ) printf("%.3f is equal to zero\n",z); if( z==n ) printf("%.3f is equal to negative zero\n",z); if( z==p ) printf("%.3f is equal to positive zero\n",z); return(0); }

Three *float* variables are declared, `n`

, `p`

, and `z`

. These are assigned values in Lines 7 through 9, with `n`

and `p`

assigned negative zero and positive zero, respectively. These values are compared in a series of *if* statements. Here’s the output:

`0.000 is equal to zero`

0.000 is equal to negative zero

0.000 is equal to positive zero

As far as I can tell, the sign bit on the zero value is ignored — supposing that the sign bit is set in the first place.

For experimental purposes, I attempted to manually flip the sign bit on a zero value. My efforts failed, though I think it’s always fun to mess with bits in a value to see the effect. Still, I believe for comparison purposes, any differences between zero, positive or negative, are irrelevant in the C language.