this post was submitted on 07 Sep 2022
56 points (93.8% liked)
Programmer Humor
32356 readers
1316 users here now
Post funny things about programming here! (Or just rant about your favourite programming language.)
Rules:
- Posts must be relevant to programming, programmers, or computer science.
- No NSFW content.
- Jokes must be in good taste. No hate speech, bigotry, etc.
founded 5 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
IIRC it's called absorption. To understand, here's a simplified explanation the way floating-point numbers are stored (according to IEEE 754):
2 ^ (exponent - 127) * 1.mantissa
. The 127 is called a bias and it's used to make possible to represent negative exponents.1.mantissa
meaning that for example, if your mantissa is1000000...
, which would be binary for0.5
, you're multiplying with1.5
. Most (except for very small ones) numbers are stored in a normalized way, which means that1 <= 1.mantissa < 2
, that's why we calculate like this and don't store the leading1.
in the mantissa bits.Now, back to
2**53
(as a decimal, that's9007199254740992.0
). It will be stored as:0 10110100 00000000000000000000000
Suppose you're trying to store the next-bigger number that's possible to store. You do:0 10110100 00000000000000000000001
But instead of9007199254740993.0
, this is representing the number9007200328482816.0
. There is not enough precision in the mantissa to represent an increment by one.I don't remember how exactly this works, but any number that you're storing as float gets rounded to the nearest representable value.
2**53 + 1
will be represented by the same bit-string as2**53
, since it's impossible to represent2**53 + 1
exactly with the given precision (it's not possible with double-precision either, the next representable number in double-precision would've been2**53 + 2
). So when the comparison happens, the two numbers are no longer distinguishable.Here's a good website to play around with this sort of thing without doing the calculations by hand: https://float.exposed Wikipedia has some general information about how IEEE 754 floats work, too, if you want to read more.
Disclaimer that this is based on my knowledge of C programming, I never programmed anything meaningful in JavaScript, so I don't know whether the way JavaScript does it is exactly the same or whether it differs.