You are using the wrong approach. Using a direct float division limits your ability to calculate the decimals, because integers can be converted to floats based on the underlying libraries, as mentioned in documentation, and by default float would not save numbers to arbitrary precision (2000 in your case):
When passing in a string, values for NaN and Infinity may be returned, depending on the underlying C library. Float accepts the strings nan, inf and -inf for NaN and positive or negative infinity. The case and a leading + are ignored as well as a leading - is ignored for NaN. Float always represents NaN and infinity as nan, inf or -inf.
In your case (as also in mine), that limit happens to be 2**1024
. Go to your python prompt, and try running the following:
>>> float(2**1024)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OverflowError: long int too large to convert to float
>>> float(2**1023)
8.98846567431158e+307
The above number basically corresponds to the value that c
would have had in your code above, in the end of the 310th iteration, and given that c is defined as float, it would throw the error.
Basically, this means that any float
number equal to or bigger than 2 **1024
will be converted to nan
or inf
, and hence your existing code won't run.
Also, note that floating point calculations have limitations anyway, so your answer would not have been correct for such high precision.
So instead, I suggest you calculate the remainder in each turn on dividing by the denominator, b, as used in the following:
a = 3
b = 857
result = 0
if a:
for x in range(0, 2000 , 1):
a = a*10
result += a / b
a = a%b
>>> print result
9056