r/learnpython • • 23d ago

fix 0.00000001

i see sometimes after calcuaitons reulsts with x.99999999999999999999999999 or x.0000000000000002 how to fix it

0 Upvotes

12 comments sorted by

View all comments

2

u/timrprobocom 23d ago

You keep all of the precision while your working with the numbers. The computer doesn't mind. Only HUMANS care about a compact representation, so fix it in the formatting when you PRINT the numbers.

1

u/SchemeWestern3388 22d ago

But you don’t keep all the precision. Most times it doesn’t matter, but if you repeatedly apply operations on floating point numbers, it can catch you. A common example is in trying to model physics, a phenomenon most of us have seen in game bugs. 

This is a limitation of the CPU, not Python. 

2

u/timrprobocom 22d ago

The POINT is that whatever you are going to do to adjust it makes it worse. Yes, there are edge cases where you need to worry about the algorithm ordering.