Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
miscalculation on "100*e-6"
#1
I am trying to make a calculation: 100*e-6, but got wrong answer: 9.999999999999999e-05.
I also tried: 100*0.000001
All of them are not reasonable.
100*1e-6
Out[103]: 9.999999999999999e-05

100*0.000001
Out[104]: 9.999999999999999e-05
But 100*1e-5 is correct.
100*1e-5
Out[105]: 0.001
Reply
#2
Many numbers cannot be exactly represented in the computer's floating point representation. So it stores as close as it can get.

You might be better off using format to display the number rounded to some decimal points.

>>> format(100*1e-6, "f")
'0.000100'
>>> format(100*1e-6, ".17f")
'0.00010000000000000'
>>> format(100*1e-6, ".20f")
'0.00009999999999999999'
or if you prefer exponential format...
>>> format(100*1e-6, "e")
'1.000000e-04'
>>> format(100*1e-6, ".11e")
'1.00000000000e-04'
>>> format(100*1e-6, ".15e")
'9.999999999999999e-05'
For background, https://docs.python.org/3.8/tutorial/floatingpoint.html
Reply
#3
If you want to get into this in depth: https://en.wikipedia.org/wiki/Floating-point_arithmetic
Reply
#4
I appreciate your reply to my problem. Your code and background are very useful for me.
Although I am still puzzled with "format(100*1e-6, ".15e") -->9.999999999999999e-05"...

(Jun-11-2020, 08:19 AM)bowlofred Wrote: Many numbers cannot be exactly represented in the computer's floating point representation. So it stores as close as it can get.

You might be better off using format to display the number rounded to some decimal points.

>>> format(100*1e-6, "f")
'0.000100'
>>> format(100*1e-6, ".17f")
'0.00010000000000000'
>>> format(100*1e-6, ".20f")
'0.00009999999999999999'
or if you prefer exponential format...
>>> format(100*1e-6, "e")
'1.000000e-04'
>>> format(100*1e-6, ".11e")
'1.00000000000e-04'
>>> format(100*1e-6, ".15e")
'9.999999999999999e-05'
For background, https://docs.python.org/3.8/tutorial/floatingpoint.html
Reply
#5
The floating-point is a representation in binary system, which cannot represent all decimals.
It's mathematically proofed.

Decimal could help, but keep in mind, that it's much slower than floating-point arithmetic.

from decimal import Decimal

print(Decimal(100) * Decimal("1e-6"))
print(Decimal(100e6) * Decimal("1e-6"))
Output:
Decimal('0.000100') Decimal('100.000000')
If you need floating-point with higher precision and fast, then try mpmath.
This will not eliminate the error, but you can get closer to a result.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020