-3

When using python, either 2.7.17 or 3.6.9, a strange behaviour occurs when using the value "1e11." For both versions:

>>> 1.0e-11*1.0e11

0.9999999999999999

>>> 1.0e-10*1.0e10

1.0


>>> 1.0e-12*1.0e12

1.0

>>> 1.0e-13*1.0e13

1.0

etc.

>>> 1.0-(1.0e-11*1.0e11)

1.1102230246251565e-16

Whilst, for all other exponents [EDIT: under 20], the result is correctly 0.0. Clearly there are some floating point precision issues going on, but why isn't it consistent between exponents 10, 11 and 12. What's going on here?

TVandMe
  • 3
  • 2
  • 4
    "why isn't it consistent between exponents 10, 11 and 12" - why *would* it be consistent? – user2357112 Jun 19 '20 at 01:47
  • Does this answer your question? [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – phuclv Jun 19 '20 at 04:03
  • Yes, both the previous comments are pretty valid and helpful. I guess it's just coincidence that all other small exponents can be represented this way, but 11 can't. Thanks for your resources. – TVandMe Jun 20 '20 at 05:43

1 Answers1

0

Floating-point numbers are represented in computer hardware as base 2 (binary) fractions.

Unfortunately, most decimal fractions cannot be represented exactly as binary fractions. A consequence is that, in general, the decimal floating-point numbers you enter are only approximated by the binary floating-point numbers actually stored in the machine.

If you’re in a situation where you care which way your decimal halfway-cases are rounded, you should consider using the decimal module. Incidentally, the decimal module also provides a nice way to “see” the exact value that’s stored in any particular Python float

As that says near the end, “there are no easy answers.” Still, don’t be unduly wary of floating-point! The errors in Python float operations are inherited from the floating-point hardware, and on most machines are on the order of no more than 1 part in 2**53 per operation. That’s more than adequate for most tasks, but you do need to keep in mind that it’s not decimal arithmetic, and that every float operation can suffer a new rounding error.

Community
  • 1
  • 1
Jason Yang
  • 11,284
  • 2
  • 9
  • 23