From: ·······@uwaterloo.ca
Subject: shadowing
Date: 
Message-ID: <33B014E8.4E3C@uwaterloo.ca>
(I asked how to do this a while back - thanks for the advice BTW - and
had it working, but for some reason it's not working now).

I have functions for comparing the equality of floating-point numbers,
and I've put them in a package.  Essentially, I want to be using my =
function instead of CL:=, but without having to say my-package:= all the
time.  I can't quite see what's wrong with the code though.  This is
what's loaded from a file:

(defpackage "FP-EPSILON"
  (:nicknames "FP")
  (:use "COMMON-LISP" "COMMON-LISP-USER")
  (:shadow "=")
  (:export "="))

(in-package "FP-EPSILON")

(defvar *epsilon-tolerance* (* 1e6 long-float-epsilon)
  "Number used to compare floating point values for equality.")

(defgeneric = (a b)
  (:documentation "= function using epsilon value."))

(defmethod = ((a Float) (b Float))
   (CL:<= (abs (CL:- a b))
    (* *epsilon-tolerance* (max (abs a) (abs b)))))

(defmethod = ((a Float) b)
   (= a (float b)))

(defmethod = (a (b Float))
   (= (float a) b))

(defmethod = ((a t) b)
   (CL:= a b))

Then I tried it like this:

> (sqrt 2)
1.4142135623731
> (= (sqrt 2) 1.4142135623731)
NIL
> (fp:= (sqrt 2) 1.4142135623731)
T

Surely if fp:= were shadowing =, then = would be 
the same as fp:=.

Confused.........

	Kev

From: Thomas A. Russ
Subject: Re: shadowing
Date: 
Message-ID: <ymivi32yc1f.fsf@sevak.isi.edu>
In article <·············@uwaterloo.ca> ·······@uwaterloo.ca writes:

 > (I asked how to do this a while back - thanks for the advice BTW - and
 > had it working, but for some reason it's not working now).
 > 
 > (defpackage "FP-EPSILON"
 >   (:nicknames "FP")
 >   (:use "COMMON-LISP" "COMMON-LISP-USER")
 >   (:shadow "=")
 >   (:export "="))
 > 
 > (in-package "FP-EPSILON")
 >
 > [Code snipped]
 >
 >> (sqrt 2)
 > 1.4142135623731
 >> (= (sqrt 2) 1.4142135623731)
 > NIL
 >> (fp:= (sqrt 2) 1.4142135623731)
 > T
 > 
 > Surely if fp:= were shadowing =, then = would be 
 > the same as fp:=.

Sounds like there is some bug in your Lisp system (which one? ACL4W?).
One possibility is that the interpreter is doing some type of special
case on the "=" and not really looking it up.  One way to check that
would be to write a function and compile it.  Then run the compiled
function:

(defun test ()
 (list "="    (= (sqrt 2) 1.4142135623731)
       "FP:=" (fp:= (sqrt 2) 1.4142135623731)))

(compile 'test)
(test) ==> ....

Another thing to consider is that you might want to force everything
into double-float or long-float for your comparison.  Alternately you
might want to handle the various cases separately.  Not all lisp systems
use the same default size for floats.

For instance, on ACL 4.3 (Unix), the default float format is
single-float, not double-float.



-- 
Thomas A. Russ,  USC/Information Sciences Institute          ···@isi.edu    
From: Tim Bradshaw
Subject: Re: shadowing
Date: 
Message-ID: <ey3yb7xo936.fsf@staffa.aiai.ed.ac.uk>
* kmayall  wrote:

> (defpackage "FP-EPSILON"
>   (:nicknames "FP")
>   (:use "COMMON-LISP" "COMMON-LISP-USER")
>   (:shadow "=")
>   (:export "="))

> (in-package "FP-EPSILON")

I think that at some point below you must have moved to a different
package -- at least, this works for me if I'm in FP.

>> (= (sqrt 2) 1.4142135623731)

>> (fp:= (sqrt 2) 1.4142135623731)

> Surely if fp:= were shadowing =, then = would be 
> the same as fp:=.

If you're not in the FP package, and you do a (USE-PACKAGE "FP") you
should get an error because of a symbol clash with FP:= and CL:=.
Shadowing the symbol will only affect the FP package itself: if you
want it to shadow in other packages you have to SHADOWING-IMPORT it,
there is no SHADOWING-EXPORT.  This is a reasonable requirement, as
you don't want some package you use randomly clobbering bits of Lisp
you might rely on, unless you ask it to!

--tim
From: Rainer Joswig
Subject: Re: shadowing
Date: 
Message-ID: <joswig-ya023180003006970004030001@news.lavielle.com>
In article <·············@uwaterloo.ca>, ·······@uwaterloo.ca wrote:

> Then I tried it like this:
> 
> > (sqrt 2)
> 1.4142135623731
> > (= (sqrt 2) 1.4142135623731)
> NIL
> > (fp:= (sqrt 2) 1.4142135623731)
> T
> 
> Surely if fp:= were shadowing =, then = would be 
> the same as fp:=.
> 
> Confused.........
> 
>         Kev

Works for me (MCL 4.1).

? (in-package :fp)
#<Package "FP-EPSILON">
? (= (sqrt 2) 1.4142135623731)
T
? (fp:= (sqrt 2) 1.4142135623731)
T
? (describe '=)
Symbol: =
Function
EXTERNAL in package: #<Package "FP-EPSILON">
Print name: "="
Value: #<Unbound>
Function: #<STANDARD-GENERIC-FUNCTION = #x281CF4E>
Arglist: (A B)
Plist: NIL
? 

Did you test it in the correct package?

-- 
http://www.lavielle.com/~joswig/
From: ·······@uwaterloo.ca
Subject: Re: shadowing
Date: 
Message-ID: <33ba4203.1284356@news.uwaterloo.ca>
On Mon, 30 Jun 1997 00:04:03 +0200, ······@lavielle.com (Rainer
Joswig) wrote:
>Works for me (MCL 4.1).
>
>? (in-package :fp)
>#<Package "FP-EPSILON">

Yes, I wasn't in the correct package.  Thanks to all who corrected me
there.  I had an (in-package :fp) form at the top of my file, but of
course, that only lasted until the file was loaded.  I hadn't made it
the current package in the main window.

Cheers........................

	Kev