complex.h math functions
Steve Kargl
sgk at troutmask.apl.washington.edu
Thu Oct 6 14:26:15 PDT 2005
On Wed, Oct 05, 2005 at 08:16:03PM +1000, Bruce Evans wrote:
> On Tue, 4 Oct 2005, Steve Kargl wrote:
>
> >On Tue, Oct 04, 2005 at 07:31:43PM +1000, Bruce Evans wrote:
> >>On Sun, 2 Oct 2005, Steve Kargl wrote:
> >
> >>I converted to ccoshf() for testing it and comparing with a simpler
> >>implementation that uses the formula, then converted back. There are
>
> >So which implementation do you prefer: your simpler version or
> >your fixed version of my first cut at ccosh?
>
> If course I prefer the simpler version :-). I think version that handles
> all the exceptional cases explicitly is useful mainly for bootstrapping.
I've implemented some inline functions (see below) for the assignments of
the real and imaginary parts. After replacing
creal(f) = cosh(x) * cos(y);
cimag(f) = sinh(x) * sin(y);
return (f);
in your simpler function by
return (cmplx(cosh(x) * cos(y), sinh(x) * sin(y));
your test program is generating a large number of
z = 0x7f800000+I0x7fa00000 inf+Inan
ccosh(z) = 0x7f800000+I0x7fe00000 inf+Inan
ccosh1(z) = 0x7fe00000+I0x7fe00000 nan+Inan
err = +0x600000+I+.0
where ccosh1 is your simpler function and ccosh is your fixed
version of my ccosh (both have been converted to use the inline
functions). n869.pdf says
ccosh(inf+Inan) = inf+Inan
> >>Handle x == 0, y finite. There are 2 unobvious details:
> >>- getting the sign right when y == 0...
> >>
> >>% + creal(f) = cos(y);
> >>% + cimag(f) = x * con;
> >>% + return f;
> >
> >Wow. I'm used to programming in Fortran and would never have
> >written "creal(f) = ...". This looks like your assigning a
> >value to a function. (For those in the peanut gallery that
> >snicker at the mention of Fortran, please see the Fortran 2003
> >standard.)
Apparently, my version of gcc does not like the above
code. How did you compile your s_ccosh1.c?
> >If we go the macro route, do you want it (them?) in math_private.h
> >or complex.h? If creal(f) appears on the LHS, is it a generic
> >reference so that type is forced to match the RHS? Is
> >
> >#define CMPLX((z),(x),(y)) {creal((z)) = (x), cimag((z)) = (y)}
> >
> >acceptable?
>
> I think it should go in math_private.h only and be an inline function.
--- /mnt1/src/lib/msun/src/math_private.h Fri Feb 4 12:05:39 2005
+++ ../math_private.h Thu Oct 6 14:06:37 2005
@@ -155,6 +155,43 @@
} while (0)
/*
+ * Inline functions that can be used to construct complex values.
+ */
+
+static __inline float complex
+cmplxf(float __x, float __y)
+{
+ float *__ptr;
+ float complex __z;
+ __ptr = (float *) &__z;
+ *__ptr++ = __x;
+ *__ptr = __y;
+ return (__z);
+}
+
+static __inline double complex
+cmplx(double __x, double __y)
+{
+ double *__ptr;
+ double complex __z;
+ __ptr = (double *) &__z;
+ *__ptr++ = __x;
+ *__ptr = __y;
+ return (__z);
+}
+
+static __inline long double complex
+cmplxl(long double __x, long double __y)
+{
+ long double *__ptr;
+ long double complex __z;
+ __ptr = (long double *) &__z;
+ *__ptr++ = __x;
+ *__ptr = __y;
+ return (__z);
+}
+
+/*
* ieee style elementary functions
*
* We rename functions here to improve other sources' diffability
--
Steve
More information about the freebsd-standards
mailing list