Could anybody help me on how to do precise calculations with Python the way
hand-held calculators do?
A small example to show the impreciseness coming from the floating point
arithmetics:
(100000000000000.4*3)*100 gives 30000000000000124.0
By coincidence I just posted a module (very alpha code, see "prePEP: Decimal data type" thread)
that does exact decimal floating point, so you can do better than hand calculators ;-)
First, let's use it to show what the full floating point bits ('all') show when converted
to an exact representation:
ED('100000000000000.40625')
^
+--the _actual_ floating point value represented
ED('300000000000001.21875')
^
+-- an exact multiple by 3
ED('300000000000001.25')
^
+--the exact value of the inexact floating point product
ED('30000000000000125')
^
+--times 100 exactly
ED('30000000000000124')
^
+--the result of the additional floating point multiply
Now doing the math starting with an exact string literal for your starting value:
ED('100000000000000.4')
^
+--value is exact
ED('300000000000001.2')
^
+--product is exact (the right operand of 3 is converted like ED(3) which is exact
since integer is exact.
ED('30000000000000120')
^
+--similarly with the 100 factor, which is converted to exact before multiplying as well.
Now try this on your calculator (note the binomial thing):
ED('1.003006010015021028036045055063069073075075073069063055045036028021015010006003001e81')
;-)
Regards,
Bengt Richter