guile-user
[Top][All Lists]

## Re: Divide by zero or how to overload a function

 From: Keith Wright Subject: Re: Divide by zero or how to overload a function Date: Mon, 22 Oct 2001 14:36:29 -0400

```> From: address@hidden
>
> On Mon, Oct 22, 2001 at 12:08:45PM -0400, Brett Viren wrote:
> >
> > Currently, if one of those functions divide by zero I catch the error
> > (via evaluating the function with scm_catch()) and just ignore the
> > result.  However, the users want their functions to return (and plot)
> > zero in the case of divide by zero error.

The users are just a little confused.  The proper thing to do is to
catch the error just before plotting, and plot a vertical line.
Doing further arithmetic on an undefined value is useless, the
"correct" result could still be anything.  0/0 = 5 + 0/0 = cos(1/0)

> > I don't want the users to have to explicitly check for divide by zero
> > in their Scheme functions, but rather handle it implicitly.

If being equal to zero at certain points that would otherwise be
undefined is part of the definition of the function, then the user
should say that; otherwise don't put words in eir mouth.
Maybe somebody wants to define cos(1/0) = 0, because that's
the average value of cos(1/x) as x->0.  (it would be 1
under the luser proposal).

> > Is it possible to reference the real "/" inside this

Brett Viren wrote:

guile> (define real-/ /)
guile> (define (/ a b) (+ 1 (real-/ a b)))
guile> (list (/ 25 5) (real-/ 25 5)) ==> (6 5)

The Scheme works, and even real-/ continues to work after
the definition of the new / (the original example did not
show that).  If you don't want to clutter the global name
space with real-/, you can do it this way:

guile>  (define / (let ((real-/ /))
(lambda (a b) (+ 1 (real-/ a b)))))
guile> (/ 25 5)  ==>  6

This works because the let binding is done before the new value
of / is set!, so the new value is a procedure with the value of
real-/ already determined.  For maximal enigmatographic effect:

(define / (let ((/ /)) (lambda (a b) (+ 1 (/ a b)))))

>  Ralf Mattes
>
> > Or is there any other good way to turn a divide by zero into a zero?

There is never a good way to do a wrong thing.
But there are lots of fun ways.

--