Math errors in python

Johan Ur Riise johan at riise-data.no
Sun Sep 19 20:08:54 EDT 2004


aleaxit at yahoo.com (Alex Martelli) writes:

> Paul Rubin <http://phr.cx@NOSPAM.invalid> wrote:
> 
> > aleaxit at yahoo.com (Alex Martelli) writes:
> > > Yes, but applying rational arithmetic by default might slow some
> > > computations far too much for beginners' liking!
> > 
> > I dunno, lots of Lisp dialects do rational arithmetic by default.
> 
> And...?  What fractions of beginners get exposed to Lisp as their first
> language just love the resulting precision/speed tradeoff...?  I think
> Paul Graham's "Worse is Better" article applies quite well here...

There is not much of a precision/speed tradoff in Common Lisp, you can
use fractional numbers (which give you exact results with operations
+, -, * and /) internally and round them off to decimal before
display. With the OP's example:

(+ 1210/100 830/100)
102/5

(coerce * 'float)
20.4

Integers can have unlimited number of digits, but the precision of
floats and reals are still limited to what the hardware can do, so if
you want to display for instance 2/3 with lots of decimals, you have 
to multiply it first and insert the decimal point yourself, like in

(format t ".~d" (round (* 2/3 10000000000000000000)))
.6666666666666666667

Of course, long integers (bignums) are slower than short (fixnums), but
with automatic conversion to and fro, you pay the penalty only when
you need it.  





More information about the Python-list mailing list