From owner-freebsd-standards@FreeBSD.ORG Wed Oct 26 19:01:56 2005 Return-Path: X-Original-To: freebsd-standards@FreeBSD.ORG Delivered-To: freebsd-standards@FreeBSD.ORG Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 7668716A41F for ; Wed, 26 Oct 2005 19:01:56 +0000 (GMT) (envelope-from das@FreeBSD.ORG) Received: from VARK.MIT.EDU (VARK.MIT.EDU [18.95.3.179]) by mx1.FreeBSD.org (Postfix) with ESMTP id D95B543D46 for ; Wed, 26 Oct 2005 19:01:55 +0000 (GMT) (envelope-from das@FreeBSD.ORG) Received: from VARK.MIT.EDU (localhost [127.0.0.1]) by VARK.MIT.EDU (8.13.3/8.13.1) with ESMTP id j9QJ0FNF052816; Wed, 26 Oct 2005 15:00:15 -0400 (EDT) (envelope-from das@FreeBSD.ORG) Received: (from das@localhost) by VARK.MIT.EDU (8.13.3/8.13.1/Submit) id j9QJ0FPX052815; Wed, 26 Oct 2005 15:00:15 -0400 (EDT) (envelope-from das@FreeBSD.ORG) Date: Wed, 26 Oct 2005 15:00:15 -0400 From: David Schultz To: Steve Kargl Message-ID: <20051026190015.GA52635@VARK.MIT.EDU> Mail-Followup-To: Steve Kargl , freebsd-standards@FreeBSD.ORG References: <20050929195552.GA14982@troutmask.apl.washington.edu> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20050929195552.GA14982@troutmask.apl.washington.edu> Cc: freebsd-standards@FreeBSD.ORG Subject: Re: complex.h math functions X-BeenThere: freebsd-standards@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Standards compliance List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 26 Oct 2005 19:01:56 -0000 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