e vs exp()? / financial applications

Fernando Pérez fperez528 at yahoo.com
Mon Sep 23 21:43:22 EDT 2002


Terry Reedy wrote:

> Although w appears to be exactly represented, it must not be.  Cute
> example.
> 

In this case it's not a problem of the exactness of w's representation, but 
the exactness of the intermediate results. It turns out that basic properties 
of common arithmetic operations, such as the associativity of addition, are 
NOT valid in floating point:

(a+b)+c != a+(b+c)

in a computer. It may appear to be true in many cases, but it is not in 
general, as the above example showed. A classic textbook example:

In [29]: eps = 1e-16

In [30]: (1+eps)+eps
Out[30]: 1.0

In [31]: 1+(eps+eps)
Out[31]: 1.0000000000000002

Any decent text on numerical analysis discusses this at length. While for 
'common' day to day things one can gloss over this, there are many instances 
where it will bite you hard.

Cheers,

f.



More information about the Python-list mailing list