Search code examples
pythonfloating-pointprecisioncalculationbignum

Python 3 float problems?


I was trying to do the follow calculation in python 3:

https://i.sstatic.net/f5y7z.png

add with:

https://i.sstatic.net/Txoya.png

that should return 0 (images were made in wolframalpha).

When I try to do the same calculation in python:

pow((2*3*5),28)-29*math.floor(pow((2*3*5),28)/29)-1

it returns 4303955653455607115022335.

the value of each part is:

228767924549610000000000000000000000000000 correct.

-228767924549609995696044346544392884977665 incorrect.

How can I fix this?


Solution

  • You are using floating point division / which has a limited precision, so your large result gets rounded.

    Instead, you can get what you want with integer, floor division //, which has an integer result with unlimited precision:

    import math
    pow((2*3*5),28)-29*(pow((2*3*5),28)//29)-1
    # 0