Wednesday, September 26, 2012

Numbers are weird

I'm working my way through Steve McConnell's excellent Code Complete 2.  I'm currently on chapter 12, which covers the basic data types a programmer might encounter.  Most of the chapter covers numbers and the errors one might encounter when using them.  I naively thought "Aha!  I can do numbers!"  Ah, if only I had paid attention in Computer Architecture, I would have known computers have a much harder time dealing with numbers than we do.

The main reason why computers have an issue with numbers is that they only have a limited space to put the numbers in.  Computers can only use zeros and ones (or "bits") to represent numbers.  "Normal" numbers usually only have 32 binary digits to describe them.  This gives us a range (in base 1) of -2,147,438,648 to 2,147,483,647 if we use the first digit to determine if the number is positive or negative and 0 to 4,294,967,295 if all 32 bits are dedicated to numbers.

So what's the problem?  Those numbers are more than enough to write a shopping list, unless you're buying 5 billion oranges.  Even if your shopping habits include ~1/3 of the annual Florida orange production, it's a simple matter to add more bits.  Using 64 bits (the next typical setting), you could purchase 18,446,744,073,709,551,615 oranges before having issues.  (The volume of said oranges is still 1,000,000x smaller than the Earth's volume).

The issue arises when we try to stuff numbers into places too small for them.  Say I wanted 127 oranges.  (I'm a college student, scurvy is a very real issue.)  Since a signed 8-bit integer can store a number up to 127, I store that in my brain as orangesToBuy (look at all the bits I'm saving!).  As I drive to the orange store, my roommate calls me and asks me to pick up an orange for him.  I add one to orangesToBuy.  When I finally arrive, I walk to the counter and order -128 oranges.  What happened?

When I added the fateful orange, the bits flipped from 01111111 (Positive, max number) to 10000000 (negative, minimum number).  xkcd illustrates this wonderfully.

When storing numbers, make sure to use the type appropriate for the situation.  It's always a better idea to use a too-big type to store it rather than spending hours debugging later on down the line.

No comments:

Post a Comment