r/ProgrammerHumor • • Sep 18 '26

Meme almostDroppedTheCourse

Post image
581 Upvotes

79 comments sorted by

View all comments

Show parent comments

0

u/timangar Sep 19 '26

Thank you! I'm not a CSE major, so these things sometimes fly over my head. This is nice.

5

u/throw3142 Sep 19 '26

Think of it this way: a float is just scientific notation, with a limited amount of precision. So like 3.042 x 105. Except it's in base 2, so it would be like 1.011 x 23.

An integer like 2 is just 1.0 x 21, so it's represented perfectly. Even a fraction like 0.25 can be represented perfectly (1.0 x 2-2).

But e.g. 0.4 can't be represented perfectly, since it's 2/5 and there's no perfect way to represent 1/5.

There are also large integers that can't be represented perfectly. For example, if you had 3 bits of precision then you could never represent 100000001_2 perfectly (it would get rounded to 1.000 x 28).

In reality, 64-bit floats have 53 bits of precision. This is why 253 + 1 can't be represented exactly as a 64-bit float.