PhDoula

One woman's path through doula training, childrearing, and a computer science Ph. D. program

Friday, May 21, 2010

Guard, sticky, and round

What follows is a sex joke about floating point numbers.

Irrational numbers are ones that may not terminate, or may repeat forever. For example, the number pi is irrational (because it neither terminates nor repeats), and so is the decimal representation of 5/9=0.555555... (because it repeats forever).

There is a problem. How do you represent an irrational number digitally? You have only a limited number of bits... and possibly an infinite number of digits to encode.

The IEEE-754 standard for floating point numbers defines rounding modes. These modes are used to try to shoehorn the (possibly infinitely long) number into a finite number of bits. The standard defines functionality with which you can round up, round down, truncate, and round to even.

What makes it happen are the guard, round, and sticky bits. The guard bit is the first bit that not fit into the representation. The round bit is the second bit that does not fit the representation. Finally, the sticky bit is a bitwise-OR of all the bits after the round bit.

OK, here's the joke. It's kind of a stretch, but stay with me...

Rounding is like sex.

You gotta use the guard bit.

If you don't, you get the sticky bit.

And then... well, you're round.