From: larry
Subject: return infinities from division
Date: 
Message-ID: <7b8f89d6.0203041619.6850d0b1@posting.google.com>
I want to be able to do the following as illustrated in the this example:
(mapcar #'/ '((+ 3 3) (* 2 4)) '((+ 3 (/ 4 (- 4 4))) '(4))

and have (infinity 2) returned.

That is if any expression the second list evaluates to 0 or infinity
then infinity should be returned from the attempted division.

This is not a homework assignment. It just an self imposed exercise.
I guess that given an expression in list2 I could check whether a division
sign occured in that expression. If it didn't I could go ahead and evaluate
the expression. If it evaluated to 0 then I could return infinity otherwise
do the division. But if the expression did have a division sign I would have
to check the denominator,etc,etc. But is there an easier way?

From: Barry Margolin
Subject: Re: return infinities from division
Date: 
Message-ID: <oT6h8.4$HT3.74@paloalto-snr2.gtei.net>
In article <····························@posting.google.com>,
larry <··········@hotmail.com> wrote:
>I want to be able to do the following as illustrated in the this example:
>(mapcar #'/ '((+ 3 3) (* 2 4)) '((+ 3 (/ 4 (- 4 4))) '(4))

Regardless of the infinity issue, this can't possibly work.  Since you
quote the lists, the arithmetic in them won't be executed.

>and have (infinity 2) returned.
>
>That is if any expression the second list evaluates to 0 or infinity
>then infinity should be returned from the attempted division.
>
>This is not a homework assignment. It just an self imposed exercise.
>I guess that given an expression in list2 I could check whether a division
>sign occured in that expression. If it didn't I could go ahead and evaluate
>the expression. If it evaluated to 0 then I could return infinity otherwise
>do the division. But if the expression did have a division sign I would have
>to check the denominator,etc,etc. But is there an easier way?

(defun /-with-infinity (arg1 &rest more-args)
  (handler-case (apply #'/ arg1 more-args)
    (division-by-zero () 'infinity)))

(mapcar #'/-with-infinity ...)

This will handle cases where one of the elements of the second list is 0,
but it won't handle infinities computed within the argument calculations.

If your implementation supports IEEE floating point, it probably has a way
to enable use of NaN's for infinities and other invalid calculations.

-- 
Barry Margolin, ······@genuity.net
Genuity, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
From: David Golden
Subject: Re: return infinities from division
Date: 
Message-ID: <CQah8.6323$D6.17947@news.iol.ie>
larry wrote:

> I want to be able to do the following as illustrated in the this example:
> (mapcar #'/ '((+ 3 3) (* 2 4)) '((+ 3 (/ 4 (- 4 4))) '(4))
> 
> and have (infinity 2) returned.
> 
> That is if any expression the second list evaluates to 0 or infinity
> then infinity should be returned from the attempted division.
> 

One thing to note regardless of the problems with the code above:

I'm sorry if you're actually experimenting with something arcane and
don't care, but in maths, things divided by zero are not necessarily 
"infinity".  They are what mathematicians call "undefined", or sometimes 
they're "indeterminate" (0/0) and your computer will probably call them 
"NaN".  

While things divided by infinity are commonly taken to be 0,
in general, things are a LOT more complicated than 
anything / 0 == infinity

I mean, what about 0 / 0, for starters?
 
At secondary-school-maths level,  L'Hopital's rule can help with certain 
divisions by zero for some fairly strictly limited cases (if the numerator 
and denominator of an expression both go to zero in some limit).

See http://mathforum.org/dr.math/faq/faq.divideby0.html

-- 
Don't eat yellow snow.
From: Christopher C. Stacy
Subject: Re: return infinities from division
Date: 
Message-ID: <uwuwqthgx.fsf@theworld.com>
>>>>> On Tue, 05 Mar 2002 21:33:22 GMT, David Golden ("David") writes:
 David> larry wrote:
 >> I want to be able to do the following as illustrated in the this example:
 >> (mapcar #'/ '((+ 3 3) (* 2 4)) '((+ 3 (/ 4 (- 4 4))) '(4))
 >> and have (infinity 2) returned.


 David> but in maths, things divided by zero are not necessarily
 David> "infinity".  They are what mathematicians call "undefined", or
 David> sometimes they're "indeterminate" (0/0) and your computer will
 David> probably call them "NaN".

APL would say, "DOMAIN ERROR".
From: Martti Halminen
Subject: Re: return infinities from division
Date: 
Message-ID: <3C852A1E.CD9467DE@kolumbus.fi>
larry wrote:
> 
> I want to be able to do the following as illustrated in the this example:
> (mapcar #'/ '((+ 3 3) (* 2 4)) '((+ 3 (/ 4 (- 4 4))) '(4))
> 
> and have (infinity 2) returned.
> 
> That is if any expression the second list evaluates to 0 or infinity
> then infinity should be returned from the attempted division.

Regardless of any infinity issues, you'd be unlikely to get a 2 -element
list as an answer: mapcar stops when the shortest list ends.

--
From: Barry Margolin
Subject: Re: return infinities from division
Date: 
Message-ID: <fKah8.19$pc1.108@paloalto-snr1.gtei.net>
In article <·················@kolumbus.fi>,
Martti Halminen  <···············@kolumbus.fi> wrote:
>larry wrote:
>> 
>> I want to be able to do the following as illustrated in the this example:
>> (mapcar #'/ '((+ 3 3) (* 2 4)) '((+ 3 (/ 4 (- 4 4))) '(4))
>> 
>> and have (infinity 2) returned.
>> 
>> That is if any expression the second list evaluates to 0 or infinity
>> then infinity should be returned from the attempted division.
>
>Regardless of any infinity issues, you'd be unlikely to get a 2 -element
>list as an answer: mapcar stops when the shortest list ends.

But list arguments have two elements:

  ((+ 3 3) (* 2 4))
   1       2

  ((+ 3 (/ 4 (- 4 4))) '(4))
   1                   2

However, he's missing the close parenthesis for the mapcar function call
expression.

-- 
Barry Margolin, ······@genuity.net
Genuity, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
From: Coby Beck
Subject: Re: return infinities from division
Date: 
Message-ID: <N%eh8.24852$eb.1287015@news3.calgary.shaw.ca>
"larry" <··········@hotmail.com> wrote in message
·································@posting.google.com...
> I want to be able to do the following as illustrated in the this example:
> (mapcar #'/ '((+ 3 3) (* 2 4)) '((+ 3 (/ 4 (- 4 4))) '(4))
>
> and have (infinity 2) returned.
>

Firstly, you don't want the quotes.  As is, you are passing two lists to /
and it wants numbers.  So you probably mean:
(mapcar #'/ (list (+ 3 3) (* 2 4)) (list (+ 3 (/ 4 (- 4 4))) (list 4)))

This has an inconsistency with what you want returned in that your last list
has only one element so mapcar will return a list with one element.

But you could define something of your own like
(defun div (n1 n2)
    (cond ((eq n1 :infinity) :infinity)
          ((eq n2 :infinity) 0)
          ((equal n2 0) :infinity)
          (t (/ n1 n2))))

If you want to use the / character then I can see two choices.  One, define
your own package and do the above this way:
(defpackage :my-package (:use :common-lisp)
               (:shadow "/"))

(in-package :my-package)

(defun / (n1 n2)
    (cond ((eq n1 :infinity) :infinity)
          ((eq n2 :infinity) 0)
          ((equal n2 0) :infinity)
          (t (common-lisp:/ n1 n2))))

This has the disadvantage of taking only two operands.  A little more work
will fix that.

The other choice would be a macro like with-safe-division or
something that traps division-by-zero errors and invokes a restart that
returns :infinity, likewise you would want a handler for an error due to
non-numeric args to common-lisp:/ and find out if the problem was an
:infinity arg, which position it was in, and return 0 or :infinity.  But
that seems awfully messy...

--
Coby Beck
(remove #\Space "coby 101 @ bigpond . com")






> That is if any expression the second list evaluates to 0 or infinity
> then infinity should be returned from the attempted division.
>
> This is not a homework assignment. It just an self imposed exercise.
> I guess that given an expression in list2 I could check whether a division
> sign occured in that expression. If it didn't I could go ahead and
evaluate
> the expression. If it evaluated to 0 then I could return infinity
otherwise
> do the division. But if the expression did have a division sign I would
have
> to check the denominator,etc,etc. But is there an easier way?