What’s the difference between 0 and -0?

This post was kindly contributed by The SAS Dummy - go there to comment and to read the full post.

My daughter’s math lessons this year have included the concept of negative numbers (that is, numbers less than zero, not numbers that have a bad attitude). She has used number lines such as this one to help her while she completes her homework:

Notice that in this number line, there is no -0. But there is a -0 in SAS. It’s a bit elusive, but still easier to find than a Higgs boson particle.

Here is one way to tease it out:

data neg0;
  a= 1 * 0; /* this is 0 */
  b= -1 * 0; /* is it 0? */
  put a= b=;
run;

The default formatted output hides the negative nature of the “nothing” we put in the variable b.

a=0 b=0

But by applying a hexadecimal format, we can take a closer look at the variable’s raw value:

data neg0;
  a= 1 * 0;
  b= -1 * 0;
  put "a=0x" a:hex16." b=0x" b:hex16.;
run;

Now look at the result:

a=0x0000000000000000  b=0x8000000000000000

You can see that there is just one bit of difference (literally!) between the two numbers. But would you ever notice this? Certainly not if you perform an equality test:

data work.neg0;
  a= 1 * 0;
  b= -1 * 0;
  put "Is 0x" a:hex16. "equal to 0x" b:hex16. "?";
  if (a eq b) then
    put "EQUAL!";
  else put "NOT EQUAL!";
run;

This program yields a deceiving result:

Is 0x0000000000000000 equal to 0x8000000000000000 ?
EQUAL!

So what’s happening? Is SAS broken? Or is there a hole ripped in the fabric of reality?

No, it’s okay! This is all part of the IEEE 754 standard for floating point arithmetic. From the Wikipedia entry for “Signed zero”:

The IEEE 754 standard for floating point arithmetic (presently used by most computers and programming languages that support floating point numbers) requires both +0 and −0.

In all but a few special cases, -0 behaves just like 0. Whew! That’s a relief.

(Are you interested in learning more about “nothing”? I highly recommend the book Zero: The Biography of a Dangerous Idea by Charles Seife.)

This post was kindly contributed by The SAS Dummy - go there to comment and to read the full post.