Visualizing Integers in Floating-Point

Floating-point numbers are quite good at representing integers. In particular, each integer up to some finite limit has a unique, exact floating point representation (except for -0, of course). I know this isn’t an interesting observation, but when you actaully step through the math, it just doesn’t seem like really had to be the case.

One nice consequence is that JavaScript handles loops alright even though it only has a double-precision Number type for integers and non-integers alike. (Given a choice though, please don’t take this to mean you should start using float or double for all of your loop indices.)

For fun, the diagram below shows the bits of integers represented in 32-bit IEEE-754 floating-point, starting at zero. Integers from to can be represented exactly. Colors represent the sign, exponent, and significand.

Scroll to browse, mouse-over to inspect. It doesn’t currently work well on mobile, sorry.