For example, 2.0**53 == 2.0**53 + 1
returns True
. I understand it's something related to floating numbers representation, but can someone explain?
Asked
Active
Viewed 65 times
2

Davis Herring
- 36,443
- 4
- 48
- 76

Yahav
- 211
- 3
- 4
-
2I think this question is more specific than the target duplicate... – juanpa.arrivillaga Feb 02 '18 at 01:15
-
@juanpa.arrivillaga it may be more specific, but the target answers this question completely. – Wayne Werner Feb 02 '18 at 01:23
-
1@WayneWerner: No, it does not answer the question completely. The top answer starts “Binary floating point math is like this,” which provides no information at all. Aside from pointing to external references with links that may break, it says nothing about what the capabilities of the common 32-bit and 64-bit formats are, so it does not answer why 2**53 is a threshold for the behavior OP asks about. It is lazy and inconsiderate to promiscuously mark so many questions as duplicates of that one, and Stack Overflow is deficient in ways to correct the matter. – Eric Postpischil Feb 02 '18 at 01:36
-
@EricPostpischil What's missing in the explanations found in the answers on the other question? The OP can contradict me, but I don't see that the question is specifically about `2.0**53`, just that the OP has a general lack of knowledge about how floating point actually works. It seems like [this answer](https://stackoverflow.com/a/588027/344286) alone answers the OP's question just fine. And if there's something that you know of that *isn't* well explained in any other answers on the original question - adding your answer *there* is what SO is for. – Wayne Werner Feb 02 '18 at 17:53
-
@WayneWerner: The question **is** about 2**53. It may not be specifically about 2**53 as a number mathematically, but there is a transition there due to the floating-point format, so the reason why the transition is at that value rather than another is relevant to understanding floating-point. The answer you point to does not answer the OP’s question. It, like so many others, is a gross statement that floating-point approximates. It says essentially nothing about the details. Floating-point arithmetic has a detailed specification, and there is a good deal of information about its mechanics… – Eric Postpischil Feb 02 '18 at 17:58
-
… that is useful to understand and convey to others. Nobody answers C question by saying “C is like this” and linking to some summary of C (not even the standard). Why is floating-point given short shrift? Aside from the specification, there are textbooks written about it, there are theorems, there are specific techniques and algorithms, there are ways to calculate error bounds, ways to avoid errors, and more. The answer to floating-point questions is not “Floating-point is inaccurate; give up.” – Eric Postpischil Feb 02 '18 at 18:01
-
The answers at [that question](https://stackoverflow.com/questions/588004/is-floating-point-math-broken/588027#588027) are low quality. I will happy enter better answers, and have. But the path to getting high-quality answers to replace old low-quality answers with too many up-votes is to get people to let new answers grow and to stop closing off questions that are not answered by that other one. – Eric Postpischil Feb 02 '18 at 18:03
-
@EricPostpischil if you feel that strongly about it, then I highly suggest creating a meta discussion around it. It's entirely possible that we could create a new, better question with better answers, but arguing that point here on a random question is probably a terrible place for it. I agree with you that that question is probably not a great example of a good question - but without a *better* question, then we can't close *that* one as a duplicate of the better question (which is totally a thing that we can do... we just need a better dupe target). – Wayne Werner Feb 03 '18 at 03:59
2 Answers
2
There are only so many bits of precision in a floating-point number. For Python’s float
(also known as “double”), that number is 53. The bit required to store the 1 in your example is just off the end, so it gets dropped. (It’s actually rounding, so sometimes it rounds up: 2.**53+3==2.**53+4
.)

Davis Herring
- 36,443
- 4
- 48
- 76
1
A simple float
does not have enough precision to represent a difference of 1 between two numbers of that magnitude. You can test the limits yourself, if you want: write a loop to test 2.0**N == 2.0**N + 1
for values of N
in range(16, 55)
. You'll find out just how many bits of precision are in a float
.
For an amusing result, also try adding 1 to 2.0**53 a gazillion times. You'll see how the value doesn't change with those small increments.

Prune
- 76,765
- 14
- 60
- 81
-
1Of course it never changes: the question established that (each) such addition doesn’t produce a distinct value for the next to build on. – Davis Herring Feb 02 '18 at 01:20