Hi,
I am using Lucid Common Lisp 4.0.1 and I have run into a very annoying problem regarding double precision real numbers. Consider this example:
> (let ((x 0.0d0)
(y 0.1d0))
(dotimes (i 10)
(setf x (+ x y))
(print x)))
0.1
0.2
0.30000000000000005
0.4
0.5
0.6
0.7
0.7999999999999999
0.8999999999999999
0.9999999999999999
NIL
The same happens if I use single-precision real-numbers (0.0 and 0.1). Can anyone tell me the reason for this problem and how I can avoid it? So far, the only solution I have found is to truncate all numbers to a certain number of decimal digits...
Thank you,
Alberto Riva
···@ipvaim.unipv.it
Department of Computer and Systems Science
University of Pavia
Italy
In article <··········@mirage.unipv.it> ···@ipvaimed1.unipv.it writes:
>I am using Lucid Common Lisp 4.0.1 and I have run into a very annoying
>problem regarding double precision real numbers. Consider this example:
Your problems will exist in any language, although some I/O libraries don't
show as many decimal places so you may not notice it. .1 cannot be
represented precisely in binary floating point (it's a repeating fraction),
so errors are bound to creep in.
--
Barry Margolin
System Manager, Thinking Machines Corp.
······@think.com {uunet,harvard}!think!barmar
In article <··········@mirage.unipv.it>, ···@ipvaimed1.unipv.it (Alberto Riva) writes:
|>
|> I am using Lucid Common Lisp 4.0.1 and I have run into a very annoying problem
|>regarding double precision real numbers. Consider this example:
|> > (let ((x 0.0d0)
|> (y 0.1d0))
|> (dotimes (i 10)
|> (setf x (+ x y))
|> (print x)))
|>
|> 0.1
|> 0.2
|> 0.30000000000000005
|> 0.4
|> 0.5
|> 0.6
|> 0.7
|> 0.7999999999999999
|> 0.8999999999999999
|> 0.9999999999999999
|> NIL
It's a simple issue of precision that is not a problem.
If you want to get EXACT values in terms of tenths, use RATIO's :-)
(let ((x 0) (y 1/10))
(dotimes (i 10)
(setf x (+ x y))
(print x)))
1/10
1/5
3/10
2/5
1/2
3/5
7/10
4/5
9/10
--
-- Kelly Murray (···@prl.ufl.edu)
University of Florida Parallel Research Lab :: 96-node KSR1, 64-node nCUBE
-------------------------------------------------------------------------