Precise calculations

Ben Finney bignose-hates-spam at and-benfinney-does-too.id.au
Fri Nov 7 16:36:07 EST 2003


On Fri, 07 Nov 2003 13:17:44 GMT, Ladvánszky Károly wrote:
> 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:

    <http://www.python.org/doc/current/tut/node14.html>

> 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:

        >>> print str(0.1)
        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."

-- 
 \     "I know when I'm going to die, because my birth certificate has |
  `\                            an expiration date."  -- Steven Wright |
_o__)                                                                  |
Ben Finney <http://bignose.squidly.org/>




More information about the Python-list mailing list