Search code examples
pythonscientific-notationlargenumber

How to suppress scientific notation in python large number calculation


In python, when a large number(integer or float) is calculated, it seems that the number will be store in scientific notation and therefore become inaccurate. e.g.

>>> rst = 15
>>> dst = [10, 14, 10, 14, 10, 14, 10, 14, 10, 14, 10, 14, 10, 14]
>>> [float(dst[i])*float(rst)**float(i) for i in range(len(dst))]

[10.0, 210.0, 2250.0, 47250.0, 506250.0, 10631250.0, 113906250.0, 2392031250.0, 25628906250.0, 538207031250.0, 5766503906250.0, 121096582031250.0, 1297463378906250.0, 2.724673095703125e+16]
>>> # please note the last element is stored in scientific notation
>>> # but if calculate 14*15**13 (same as how the last element is calculated)
>>> 14*15**13
>>> 27246730957031250
>>> # result is fine
>>> 14*15**13 == dst[-1]
>>> False

how should I suppress scientific notation in python large number calculation


Solution

  • please note the last element is stored in scientific notation
    but if calculate 14*15**13 (same as how the last element is calculated)
    

    Last element is float, whilst result of 14**15**13 is int. If you are working solely with ints (and do not divide) you will get int in which case no e-notation is used.