Skip to Content
Navigation:

A stick figure smiling

Here's a preview from my zine, How Integers and Floats Work! If you want to see more comics like this, sign up for my saturday comics newsletter or browse more comics!

Image of a comic. To read the full HTML alt text, click "read the transcript".

browse more comics! get the zine!
read the transcript!

title: the gaps between floats

panel 1: floating point numbers have to fit into 32 or 64 bits

This means there are only 2^64 64-bit floats, the same way there are only 2^64 64-bit integers

panel 2: this means floating point numbers have to be spread out

you can imagine them all spaced out on a number line, like this: (picture of a bunch of lines, with small gaps between them. The gaps are smaller on the left and bigger on the right)

panel 3: the gaps start small.

the next 64-bit float after 1.0 is 1 point (lots of 0s) 2

the gap between these two floats is 0 point (lots of 0s) 2, or 2^-52

gaps are always a power of 2

panel 4: the gaps get bigger as the numbers get bigger

the next 64-bit float after 1000000000000000000 is that number plus 16384.

so the gap is 16384, or 2^14!

panel 5: the gaps make calculations inaccurate

when you do math on floating point numbers, often you have to round the result to the nearest float

usually this doesn’t make a big difference, but small mistakes can add up

panel 6: this inaccuracy is inevitable

if you want math to be fast, you have to store the numbers in a fixed number of bits, like 64 bits. So you’re always going to have accuracy issues.

Saturday Morning Comics!

Want another comic like this in your email every Saturday? Sign up here!

I'll send you one of my favourite comics from my archives every Saturday.
© Julia Evans 2024 | All rights reserved (see the FAQ for notes about licensing)