There’s some special floating point values that you should be aware of. Zero, infinity, and not a number are three cases that might surprise you.

Let’s get started. First of all, do you know the sign of zero? Is it positive or negative?

Depending on your country and traditions, zero might either have no sign or be both positive and negative. I think it’s standard for most places that zero has no sign at all. It can be important when a value approaches zero to consider what direction it’s approaching from. In other words, is a value shrinking from a positive value towards zero? Or is it increasing from a negative value towards zero? Once a value reaches zero, then it loses it’s sign. Or it gains both signs if you prefer. The main point is that zero behaves slightly differently than all other values.

This difference is most important when dividing. Listen to the full episode for more information about how dividing by a floating point 0.0 is very different than dividing by an integer 0 value. Floating point division can lead to infinity while integer division can lead to almost anything. And from there, it’s not too hard to get into “not a number” values. Or you can read the full transcript below.

## Transcript

Because you’ll be likely to use a lot of floating point numbers when building a game, this episode will explain some special values. Zero, infinity, and not a number are three cases you should be aware of.

You probably already know about zero and infinity, but even these might surprise you. And not a numbers which are usually referred to as NaNs are even more peculiar.

Let’s get started. First of all, do you know the sign of zero? Is it positive or negative?

Depending on your country and traditions, zero might either have no sign or be both positive and negative. I think it’s standard for most places that zero has no sign at all.

It can be important when a value approaches zero to consider what direction it’s approaching from. In other words, is a value shrinking from a positive value towards zero? Or is it increasing from a negative value towards zero?

Once a value reaches zero, then it loses it’s sign. Or it gains both signs if you prefer. The main point is that zero behaves slightly differently than all other values.

This is most important when dividing. If you ever try to divide by zero, you get, well, you don’t really get anything. This is just not allowed. I remember a proof once when I was in high school that showed through a series of steps that 1 was equal to 2. It was obviously wrong. But why? Every algebra step along the way looked correct. But hidden in one of the steps was a divide by zero. This broke the entire proof and led to a bad result.

In programming, dividing by zero using integer arithmetic is undefined. I wrote a small program for this episode to see what the result would be. I just printed the output of dividing 1 by 0. The first thing I noticed was a compiler warning that said this operation was undefined. The compiler was able to tell this because I was using the literal values 1 and 0 directly.

It’s more likely in your programs that the values you’ll be dividing won’t be known until runtime. They’ll be variables. The compiler can’t warn you for things like this.

Anyway, it was a warning and the program compiled. So I ran it. And it said that 1 divided by 0 was 73,896. Once you get into undefined behavior in your program, the results could be anything. Your program could crash or give you wild results that don’t make any sense. Dividing by zero is something you always need to be careful to avoid.

But that’s for integer numbers. Those are whole numbers. What about dividing by zero with floating point numbers? This turns out to have a definite answer. And it depends on whether the zero is positive or negative.

That’s right, in floating point format, the number zero can be either positive or negative. It’s all controlled by the single sign bit in the floating point standard. This sign bit exists and is used for the number 0.0 just like all the other numbers.

So what is 1.0 divided by 0.0? The answer leads us to the next special floating point number type. Infinity. And in this case, positive infinity.

If you divide 1.0 by -0.0, then you end up with negative infinity.

This is not just a really big number that can be either positive or negative. According to the floating point standard, there are specific bit patterns reserved for the special values.

This is also why you can’t just compare floating point bits to figure out what they mean. You can’t just write code that checks if all the bits in a floating point value are zero and assume then that the value must be zero. Because negative zero and positive zero both represent the value zero and differ by a single bit.

If you take any positive floating point value and divide by positive infinity, then you end up with positive zero. Dividing by negative infinity will give you negative zero.

What happens if you divide 0.0 by 0.0 though? Now we end up with a problem again. This doesn’t make sense and floating point has the answer. You get something that’s no longer a number. It’s actually called not a number. And this is written NaN with capital N, lowercase a, and another capital N.

You get the same not a number if you try subtracting infinity from infinity. Although, I always wondered why this doesn’t just result in zero?

There’s a few other things you can do to end up with not a number. But the important thing to note is that once you do get not a number, then anything you do with that in your calculation will also result in not a number. And if you ever try comparing anything with a not a number, you’ll get false. They’re not equal to anything and not a numbers spread through your results like a cold in winter.