0

Python 3. I have this command ->

int(29930125722068957199361/65537)

I get 456690506463050752, but answer should be 456690506463050753

Oh and this happens only in Python 3+ , not when executing direct in Python prompt

Stubborn
  • 290
  • 2
  • 17

1 Answers1

1

This is probably because of floating point error. Floating point numbers have a limited size in memory and can only be so accurate for some values.

Instead, use the // (floor division) operator for an accurate result:

print(29930125722068957199361 // 65537)
# 456690506463050753
iz_
  • 15,923
  • 3
  • 25
  • 40
  • But the numbers being divided are integers. And since this is a case of being exactly divisible, where is the floating point aspect coming from? Other than that division is always a fp result in Python 3+. And if that was it, still my cast to int or a math.floor should bring it back to the proper int value right? – Stubborn Mar 21 '19 at 02:07
  • 1
    @Stubborn You are right, division results are always floating point. For example, `type(1 / 1)` is `float`. Anyway, when the floating point aspect comes in, you are already losing precision that cannot be "regained" through conversion to `int` or `math.floor`. – iz_ Mar 21 '19 at 02:09