0

I've got this error recently:

>>> (2**53+1) - int(float(2**53+1))
1
>>> (2**53) - int(float(2**53))
0

Can someone help me understand why exactly int(float(2**53)) == int(float(2**53+1)) would be true? i.e. Why I get this 1 error?

enter image description here

Just added an answer to Is floating point math broken? if anyone is interested. Thanks to @a_guest

costargc
  • 516
  • 5
  • 14
  • [https://docs.python.org/2/tutorial/floatingpoint.html](https://docs.python.org/2/tutorial/floatingpoint.html) – cwalvoort Oct 05 '19 at 20:39
  • I can see that if I remove the `int()` it works... what I don't understand is that it works for up to `**52` but fails for `**53` – costargc Oct 05 '19 at 20:41
  • Floating point numbers have limited precision. This applies even for integer values. – user2357112 Oct 05 '19 at 20:53
  • `float` doesn't have enough precision to distinguish between `2**53` and `2**53+1`; they are both equal to the `float` value of `2**53`. Look at the actual values of `2**53 + 1` and `float(2**53 + 1)`. – chepner Oct 05 '19 at 20:53
  • I've tried to remove the int() and remove the float()... and they work. Error only shows when I combine both. – costargc Oct 05 '19 at 21:17
  • @Daniel, I saw your answers/question... but what I want to know here why is the break point `2**53 +1` and why it only happens when I combine int(float()). – costargc Oct 05 '19 at 21:21
  • 1
    @costargc [Double precision floating point numbers](https://en.wikipedia.org/wiki/Double-precision_floating-point_format) have a significand precision of 53. Quoting the article: "Between `2**52 == 4_503_599_627_370_496` and `2**53 == 9_007_199_254_740_992` the representable numbers are exactly the integers. For the next range, from `2**53` to `2**54`, everything is multiplied by `2`, so the representable numbers are the even ones, etc. Conversely, for the previous range from `2**51` to `2**52`, the spacing is `0.5`, etc." – a_guest Oct 05 '19 at 21:31
  • 1
    If you don't cast to `int`, the l.h.s. will be implicitly converted to `float` and hence suffer from the same resolution problem. I.e. if you did `(2**53+1) - float(2**53+1)` then implicitly the l.h.s. `(2**53+1)` would be converted to `float(2**53+1)` and obviously that's the same as the r.h.s. However `(2**53+1)` is an `int` and can grow arbitrarily while `int(float(...))` evaluates to `2**53` (since that is the internal value at which the r.h.s. number is stored, since `float(2**53) == float(2**53 + 1)`). – a_guest Oct 05 '19 at 21:34
  • @a_guest... I've just added what you sent me as a response to the question [Is floating point math broken?](https://stackoverflow.com/a/58252575/7541700). Thank you again now it clicked on why this happened! – costargc Oct 05 '19 at 21:53

0 Answers0