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!

floating point is weird

floating point 10.0 is not the same as the integer 10

10 (64-bit integer): 0x000000000000000a

10.0 (64-bit float): 0x4024000000000000

(what’s this 4024 doing???)

computer integers work almost exactly the way you’d expect

1 + 2 - 3 = 0

but floating point numbers don’t:

(0.1 + 0.2) - 0.3 = 0.0000000000000000555

checking for float equality is dangerous

if x == 0.3: bad!

(0.1 + 0.2) is not equal to 0.3!

Instead, check if x is very close to 0.3, something like this:

if abs(x 0.3) 0.0000001:

in floating point, very large integers get rounded

For example: 10000000000000001.0 == 10000000000000000.0

(16 zeros)

(try comparing those 2 numbers in your favourite language! they’re the same!)

(x + y) + z is not the same as x + (y + z)

For example: (9007199254740992.0+ 1.0) 1.0 = 9007199254740991.0

(the math term for this problem is “floating point addition isn’t associative”)

some intuition for precision

32-bit floats have about 8 digits of precision

64-bit floats have about 16 digits of precision

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)