249: GameDev: Floating Point: Zero, Infinity, NaN.
Take Up Code - A podcast by Take Up Code: build your own computer games, apps, and robotics with podcasts and live classes
Categories:
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