From: Robert Hartsuiker
Subject: Why is x not equal to (- 1 (- 1 x)) ?
Date: 
Message-ID: <45534@mimsy.umd.edu>
I am new to this newgroup, sorry if I break any of your (un)written
conventions.
I have a problem with Ibuki Common Lisp.
The problem is this:  I am calculating a value x as follows

	(setq y (- 1 (- 1 x)))

Now, of course, I expect to get x again. Instead I get x + delta in which 
delta is a very small constant (1E-17). This happens when x is a rational
number smaller than 0.1. This poses a problem in the program I am 
writing, because (= y x) returns nil instead of t.  

Is this a bug or a "documented feature"? What do you think of my solution,
a new test for equality, which I call ==?

(defun == (a b)
	(let ((precision 1E-8))
	 	(or (= a b)
		    (< (abs (- a b)) precision)
		)
	)
)

Help, suggestions, comments welcome. 

Rob
········@cs.umd.edu
From: Barry Margolin
Subject: Re: Why is x not equal to (- 1 (- 1 x)) ?
Date: 
Message-ID: <klsfoqINN8n7@early-bird.think.com>
In article <·····@mimsy.umd.edu> ········@cs.umd.edu (Robert Hartsuiker) writes:
>The problem is this:  I am calculating a value x as follows
>
>	(setq y (- 1 (- 1 x)))
>
>Now, of course, I expect to get x again. Instead I get x + delta in which 
>delta is a very small constant (1E-17). This happens when x is a rational
>number smaller than 0.1. This poses a problem in the program I am 
>writing, because (= y x) returns nil instead of t.  

If X is a *rational* number, you should not have this problem; Common Lisp
requires rational arithmetic to be exact.  However, if X is actually a
*floating point* number (as you imply by your use of exponential notation,
which CL will only use for floats) then it is quite easy for this to
happen.  Floating point arithmetic has inherent inaccuracies due to
roundoff errors.  Many decimal fractions (such as .1) end up as repeating
fractions in binary, so loss of precision is inevitable.

If you want exact answers, don't use floating point.  For more information,
see a text on numerical analysis.  There's some good stuff in Knuth's
"Seminumerical Algorithms".

-- 
Barry Margolin, Thinking Machines Corp.

······@think.com
{uunet,harvard}!think!barmar