Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 26 Oct 2005 15:00:15 -0400
From:      David Schultz <das@FreeBSD.ORG>
To:        Steve Kargl <sgk@troutmask.apl.washington.edu>
Cc:        freebsd-standards@FreeBSD.ORG
Subject:   Re: complex.h math functions
Message-ID:  <20051026190015.GA52635@VARK.MIT.EDU>
In-Reply-To: <20050929195552.GA14982@troutmask.apl.washington.edu>
References:  <20050929195552.GA14982@troutmask.apl.washington.edu>

next in thread | previous in thread | raw e-mail | index | archive | help
On Thu, Sep 29, 2005, Steve Kargl wrote:
> Is it permissible to implement the complex.h math
> functions in terms of functions in math.h.  For
> example, if z = x + I * y, then
> 
> cos(z) = cos(x) * cosh(y) - I sin(x) * sinh(y)
> 
> This can be (naively?) implemented as
> 
> double complex
> ccos(double complex z)
> {
>    double x, y;
>    x = creal(z);
>    y = cimag(y);
>    return (cosh(y) * (cos(x) - I * sin(x) * tanh(y)));
> }
> 
> I don't own a copy of C99, so I can't easily check
> the wording of the standard.

So I unfortunately don't have time to look into this right now,
but I have a few brief comments.  Yes, all of the complex.h
functions can be implemented in terms of ordinary math.h functions
that operate on the real and imaginary parts.  However, with the
exception of carg() and the functions already implemented in
FreeBSD, implementing them this way could result in significant
errors in corner cases.  (For instance, underflow or overflow
might occur at points near the real or imaginary axes.)  On the
other hand, doing better than this is no easy task.

[1] describes for complex arcsine and arccosine what the
troublesome cases are and how to do better.  (You don't need to
use all their funny `optimizations' involving exception handling.)
That said, it's unclear whether we want to go through that much
effort.  I believe IBM is the only vendor to have put together
reasonable implementations of the complex math functions.  The GNU
C library just uses trivial implementations of the complex math
functions in terms of real math functions.  In at least one case,
the implementation is completely bogus, which gives you an idea of
how much people are using this stuff at this point...

In any case, I'm not up enough on this stuff to even remember what
a complex arctangent is supposed to look like, nor do I have time
to look into it right now.  It's fine with me if you want to
implement the remaining complex.h functions in terms of simple
routines that use the real math routines that already exist.  It
would probably be a good idea to add a big XXX in the source
pointing out that we could do better if someone has the inclination.

--David


[1] http://portal.acm.org/citation.cfm?id=275324



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20051026190015.GA52635>