guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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: Tue, 23 Oct 2001 01:29:46 -0400

> From: Brett Viren <address@hidden>
> Cc: address@hidden
> 
> Keith Wright writes:
>  > 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.
> 
> This would also be okay, and even a preferable solution, but how?
> Currently I evaluate the user functions via this method:

       <C++>

> Maybe this isn't the best way to do things, but how might I catch just
> divide-by-zero in order to plot the vertical line but have all other
> errors handled with this generical warning?

(use-modules (ice-9 format))

(define (plot f)
   (catch #t (lambda()(for-each show (map (careful f) (grid -5 5))))
             (lambda (key p form args data)
                (format #t "Your function blows because: ~S~%" key)
                (if p (format #t "in procedure ~S~%" p))
                (apply format (cons form args)))))

(define (grid lo hi)
   (if (> lo hi) '() (cons lo (grid (+ lo 1) hi))))

(define (show v)
  (if v
      (format #t "~V_*~%" (round (* 10 v)))
      (format #t "--------------------~%")))

(define (careful f)
   (lambda (x)
      (catch 'numerical-overflow
         (lambda()(f x))
         (lambda xxx #f))))

(plot (lambda(x)(/ 1 (abs x))))

  *
  *
   *
     *
          *
--------------------
          *
     *
   *
  *
  *

(plot (lambda(x) (sqrt x y z)))
Your function blows because: wrong-number-of-args
"Wrong number of arguments to #<procedure sqrt (z)>"



Now here's a question for the wizards.
Why does (/ 0) have a different result from (/ 1 0)?

guile> (/ 1 0)
standard input:203:1: In procedure / in expression (/ 1 0):
standard input:203:1: Numerical overflow
ABORT: (numerical-overflow)
guile> (/ 0)
+#.#

-- 
     -- Keith Wright  <address@hidden>

Programmer in Chief, Free Computer Shop <http://www.free-comp-shop.com>
         ---  Food, Shelter, Source code.  ---



reply via email to

[Prev in Thread] Current Thread [Next in Thread]