Math errors in python
Johan Ur Riise
johan at riise-data.no
Mon Sep 20 02:08:54 CEST 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)
(coerce * 'float)
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)))
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