Under The Microscope

We’re Math Dorks – Are You Surprised?

Computer users often like to talk about their uptime. For a computer whose most taxing work consists of watching Flash animations, uptime may not matter too much, but for certain things such as broadcasting internet radio, the ability to get a long uptime is essential. The longer your station can go without problems, the less time you spend troubleshooting and the more time you spend transmitting great talk and music to your listeners.

Paul was talking about his Nicecast uptime, as seen above. That’s almost 33 days, for the lazy, which is not too shabby. We started to wonder just how much time that little counter could handle before it rolls over (or breaks entirely).

Our first thought was that this counter might just be a standard 32-bit integer ticking away. It turns out that the internal count is kept in 44.1kHz audio samples. A 32-bit integer can hold a value up to 2^32-1 (that’s a bit over four billion), so the maximum time on the counter would be (2^32-1)/44100 seconds, or about one day and three hours. Obviously that’s not it.

Next we thought that a 64-bit integer might be it. The 32-bit version can hold four billion and change, but with 64 bits you can store numbers over 18 quintillion. The maximum time on this counter would be (2^64-1)/44100 seconds, which works out to over 13 million years. Now we’re talking.

At this point we talked to Quentin, and he revealed that neither one was the right answer. Nicecast actually uses a double internally, which is a 64-bit floating point number. Time to redo the calculations again….

A double stores a number in what is basically scientific notation, like if you read that distance from the Earth to the Sun is 7.44 x 10^8 furlongs. One part of the number stores the exponent (8), one part stores the mantissa (7.44), and one bit holds whether it’s positive or negative.

In a double, the mantissa is a 52-bit value, which as actually 53 bits but with the leading 1 being implied. A double doesn’t wrap around, but it will eventually get to a point where adding 1 to it doesn’t affect its value, due to precision loss. This will happen at a point where the least-significant bit in the mantissa represents 2. Kind of like adding 1 to a quadrillion when you only have a liquor store receipt to write on, it just gets lost in the noise. Adding 1 will cease to have any effect when the value of the double is 2^53.

Since this bit of math is a little more complex, I wrote a short program to verify it:

int main( int argc, char **argv )
   double test = 1;
   int exponent = 0;

   while( 1 )
      if( test + 1 != test )
         printf( "2^%d is good\n", exponent );
         test *= 2;
         printf( "2^%d + 1 == 2^%d\n", exponent, exponent );
         return 0;
   return 0;

It basically tries 2^0, 2^1, 2^2, etc. until it finds a value where x + 1 is still x. And just as expected, the program kicks out:

2^0 is good
2^1 is good
2^2 is good
2^52 is good
2^53 + 1 == 2^53

This means that the Nicecast counter can get up to 2^53/44100 seconds, at which point it will just freeze in place and never change again. This means that Nicecast can only handle about 6,500 years of continuous operation before its “On Air” counter will get stuck, never to increase again. We’ll see about this little y8.5k bug but for now, Nicecast should keep busily counting away until the beginning of the next Ice Age. Until then, happy broadcasting!

Comments for this post have been closed. Thanks for reading!

Our Software