How to get the correct accuracy with big integer division in python
Asked Answered
L

1

5

I have a big integer below, as 'max'. How come the value dividing max by '27' is not equivalent to just completely omiting the first number '27'. Technically they should be equal, but in python they are not. How can I get the same answer by dividing the max value with '27', in this example?

max = 27*37*47*30*17*6*20*17*21*43*5*49*49*50*20*42*45*1*22*44
no27 = 37*47*30*17*6*20*17*21*43*5*49*49*50*20*42*45*1*22*44
div27 = (max/27)
modno27 = no27%40
moddiv27 = div27%40

The values printed are:

no27 = 35882855955274315680000000
div27 = 3.5882855955274316e+25
modno27 = 0
moddiv27 = 8.0
Logistician answered 10/3, 2017 at 3:36 Comment(0)
S
13

Assuming this is Python 3, you used true division, which computes float results, but float (based on C double) has representation limitations that Python ints do not (above ~2**53, it can't represent every integer value).

When you know the number is evenly divisible, use // to preserve int-ness. If it's not evenly divisible, you'll round down, e.g. 5 // 3 == 1. If that's unacceptable, you can use divmod to compute both quotient and remainder at once (so no information is lost) or the fractions.Fraction type or decimal.Decimal type (with appropriate precision) to get more precise results in a single result type.

Spree answered 10/3, 2017 at 3:43 Comment(1)
Thanks, that makes a lot of sense. Thank you for the thorough explanation of what was going on.Logistician

© 2022 - 2024 — McMap. All rights reserved.