I'm getting a result from the = function which, after milling through
CLtL2 and Allegro's Help files, I still don't understand. I am giving =
the results of two expressions which evaluate to 1/5 and 0.2. Yet it
returns nil.
Results from the stepper:
4: (= (SLOPE V1 V2) (SLOPE V1 V3))
5: (SLOPE V1 V2)
6: V1
6: returns: #<VERTEX #xEB8354>
6: V2
6: returns: #<VERTEX #xEB8390>
5: returns: 1/5
5: (SLOPE V1 V3)
6: V1
6: returns: #<VERTEX #xEB8354>
6: V3
6: returns: #<VERTEX #xEB8720>
5: returns: 0.2
4: returns: NIL
Yet, when evaluated in the toploop window, (= 1/5 0.2) returns T.
I don't understand this.
The slope function is defined as:
(defmethod slope ((p1 Point) (p2 Point))
(if (zerop (- (x p2) (x p1))) most-positive-single-float
(/ (- (y p2) (y p1)) (- (x p2) (x p1)))))
and Vertex is a subclass of Point, with no method defined for Vertices.
Coercing the result of slope to 'float doesn't work either.
Thanks in advance for your help..........
Kevin
Kevin Mayall <·······@uwaterloo.ca> wrote in article
<·············@uwaterloo.ca>...
> I'm getting a result from the = function which, after milling through
> CLtL2 and Allegro's Help files, I still don't understand. I am giving =
> the results of two expressions which evaluate to 1/5 and 0.2. Yet it
> returns nil.
>
This is likely a floating point roundoff error. Try something like this
instead:
(defconstant *tolerance* 0.0000001) ; or whatever you need for precision
Then, do your equality testing like this:
(< (abs (- (slope v1 v2) (slope v1 v3)) ) *tolerance*)
This is pretty simplistic, and if you have efficiency concerns, you may be
able to do something better, but it should solve your problem.
Randy
--
Randy Coulman, M.Sc., President Randco Software Corporation
······@eagle.wbm.ca 2530 Hanover Avenue
Phone: +1-306-343-3380 Saskatoon, SK S7J 1G1
Fax: +1-306-343-3341 http://www.wbm.ca/users/randco
In article <·············@uwaterloo.ca>,
Kevin Mayall <·······@uwaterloo.ca> wrote:
>I'm getting a result from the = function which, after milling through
>CLtL2 and Allegro's Help files, I still don't understand. I am giving =
>the results of two expressions which evaluate to 1/5 and 0.2. Yet it
>returns nil.
Remember that floating point values are usually an approximation. And two
different floating point values may print the same, because they differ
only in the low-order bit or two; the printer will often display .2+/-epsilon
as .2, rather than .20000000001 or .1999999999.
It's generally a bad idea to compare floats for precise equality.
--
Barry Margolin
BBN Corporation, Cambridge, MA
······@bbnplanet.com
(BBN customers, call (800) 632-7638 option 1 for support)