Precise calculations

  • Thread starter Ladvánszky Károly
  • Start date
L

Ladvánszky Károly

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

Thanks for any help,

Károly
 
B

Ben Finney

A small example to show the impreciseness coming from the floating
point arithmetics:

Issues with representing floating point numbers in binary computers are
discussed here with respect to Python:

said:
Could anybody help me on how to do precise calculations with Python
the way hand-held calculators do?

Use rounding, e.g. as implemented by the str() function. From the above
URL:

"Python's builtin str() function produces only 12 significant
digits, and you may wish to use that instead. It's unusual for
eval(str(x)) to reproduce x, but the output may be more pleasant to
look at:
0.1

It's important to realize that this is, in a real sense, an
illusion: the value in the machine is not exactly 1/10, you're
simply rounding the display of the true machine value."
 
B

Ben Finney

Use rounding, e.g. as implemented by the str() function.

Apologies; str() doesn't do rounding per se, it shows the float values
with fewer significant digits without changing the internal value.
 
A

Alex Martelli

Ben Finney wrote:
...
Use rounding, e.g. as implemented by the str() function. From the above

Use str if you really want to do like hand-held calculators, i.e., hide some
digits. If you want really high precision in binary floating point, the
gmpy extension module may be useful; if in decimal floating point, there is
a class Decimal that's being worked on to eventually provide that, too.


Alex
 
B

Bengt Richter

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,169
Messages
2,570,920
Members
47,463
Latest member
FinleyMoye

Latest Threads

Top