This is the mail archive of the
guile@sourceware.cygnus.com
mailing list for the Guile project.
Re: abs / magnitude
- To: clark dot mcgrew at sunysb dot edu
- Subject: Re: abs / magnitude
- From: Dirk Herrmann <dirk at ida dot ing dot tu-bs dot de>
- Date: Thu, 11 May 2000 17:05:01 +0200 (MEST)
- cc: guile at sourceware dot cygnus dot com
On Thu, 11 May 2000, Clark McGrew wrote:
> Based on my physics oriented background the absolute value is defined
> for both real and complex numbers. R5RS says that abs must handle
> real numbers, but it doesn't seem to exclude handling complex numbers.
> Is there a significant efficiency argument for changing abs?
No, there's no efficiency difference, since the implementations of
both are equal for all types, except for complex numbers. And, as you
say and as Greg Troxel pointed out to me, R5RS should be rather
interpreted to leave the issue undefined whether abs may take complex
arguments. However, he also said that allowing complex args, for the same
reason, may lead to compatibility problems, since other schemes may handle
things differently.
Either way is not a big problem, though:
* If abs does not take complex arguments, but you want it to, you can
simply put (define abs magnitude) into your code.
* In contrast, making (eq? abs magnitude) the default, you could simply do
(define (abs x) (if (complex? x) (error) (magnitude x)))
If the second solution was the default, we could simply remove the code
for scm_abs from numbers.[ch].
Some more arguments might be in favor of the first solution:
* As the example above shows, emulating (eq? abs magnitude) is simpler and
faster, since the type checks are performed on the C level.
* It may make sense to have two separate generic functions abs and
magnitude. This is not possible if they share the same code (at least
with the current implementation of generic dispatch in numbers.c).
(Mikael may object if I'm talking rubbish here).
Best regards
Dirk Herrmann