Decimal() instead of float?

Robert Kern robert.kern at gmail.com
Fri Nov 17 23:13:21 CET 2006


Michael B. Trausch wrote:
> Perhaps you should not make assumptions; I am sure that you have heard
> what they do at some point before.  While *some* of the error doesn't
> propagate as expected (which is actually a problem in itself—equations
> no longer make sense if they are not mathematically balanced!) some
> does.  It is unpredictable and can't be tolerated when the numbers must
> come out exactly.

Okay, whatever your requirements are, Fredrik is certainly right in that you
don't know what you're talking about with respect to floating point arithmetic.
Please read the paper "What Every Computer Scientist Should Know About
Floating-Point Arithmetic":

  http://docs.sun.com/source/806-3568/ncg_goldberg.html

You would also do well to get a book on basic numerical analysis. If you have
any transcendental functions involved (and if you are computing distances
between geographical coordinates, you certainly will), you will encounter
numbers that are irrational; that is, they *cannot* be expressed exactly in any
finite form. Decimal() and GMP are *arbitrary* precision data types, not infinite.

I admit, I am curious now about the application that you think requires these
exact results. What operations are you actually performing? Surely there's a
square root or trig function in there somewhere.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco




More information about the Python-list mailing list