xref: /minix3/lib/libm/complex/cephes_subr.c (revision 2fe8fb192fe7e8720e3e7a77f928da545e872a6a)
1*2fe8fb19SBen Gras /* $NetBSD: cephes_subr.c,v 1.1 2007/08/20 16:01:33 drochner Exp $ */
2*2fe8fb19SBen Gras 
3*2fe8fb19SBen Gras /*-
4*2fe8fb19SBen Gras  * Copyright (c) 2007 The NetBSD Foundation, Inc.
5*2fe8fb19SBen Gras  * All rights reserved.
6*2fe8fb19SBen Gras  *
7*2fe8fb19SBen Gras  * This code is derived from software written by Stephen L. Moshier.
8*2fe8fb19SBen Gras  * It is redistributed by the NetBSD Foundation by permission of the author.
9*2fe8fb19SBen Gras  *
10*2fe8fb19SBen Gras  * Redistribution and use in source and binary forms, with or without
11*2fe8fb19SBen Gras  * modification, are permitted provided that the following conditions
12*2fe8fb19SBen Gras  * are met:
13*2fe8fb19SBen Gras  * 1. Redistributions of source code must retain the above copyright
14*2fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer.
15*2fe8fb19SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
16*2fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer in the
17*2fe8fb19SBen Gras  *    documentation and/or other materials provided with the distribution.
18*2fe8fb19SBen Gras  *
19*2fe8fb19SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20*2fe8fb19SBen Gras  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21*2fe8fb19SBen Gras  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22*2fe8fb19SBen Gras  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23*2fe8fb19SBen Gras  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24*2fe8fb19SBen Gras  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25*2fe8fb19SBen Gras  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26*2fe8fb19SBen Gras  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27*2fe8fb19SBen Gras  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28*2fe8fb19SBen Gras  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29*2fe8fb19SBen Gras  * POSSIBILITY OF SUCH DAMAGE.
30*2fe8fb19SBen Gras  */
31*2fe8fb19SBen Gras 
32*2fe8fb19SBen Gras #include "../src/namespace.h"
33*2fe8fb19SBen Gras #include <complex.h>
34*2fe8fb19SBen Gras #include <math.h>
35*2fe8fb19SBen Gras #include "cephes_subr.h"
36*2fe8fb19SBen Gras 
37*2fe8fb19SBen Gras /* calculate cosh and sinh */
38*2fe8fb19SBen Gras 
39*2fe8fb19SBen Gras void
_cchsh(double x,double * c,double * s)40*2fe8fb19SBen Gras _cchsh(double x, double *c, double *s)
41*2fe8fb19SBen Gras {
42*2fe8fb19SBen Gras 	double e, ei;
43*2fe8fb19SBen Gras 
44*2fe8fb19SBen Gras 	if (fabs(x) <= 0.5) {
45*2fe8fb19SBen Gras 		*c = cosh(x);
46*2fe8fb19SBen Gras 		*s = sinh(x);
47*2fe8fb19SBen Gras 	} else {
48*2fe8fb19SBen Gras 		e = exp(x);
49*2fe8fb19SBen Gras 		ei = 0.5 / e;
50*2fe8fb19SBen Gras 		e = 0.5 * e;
51*2fe8fb19SBen Gras 		*s = e - ei;
52*2fe8fb19SBen Gras 		*c = e + ei;
53*2fe8fb19SBen Gras 	}
54*2fe8fb19SBen Gras }
55*2fe8fb19SBen Gras 
56*2fe8fb19SBen Gras /* Program to subtract nearest integer multiple of PI */
57*2fe8fb19SBen Gras 
58*2fe8fb19SBen Gras /* extended precision value of PI: */
59*2fe8fb19SBen Gras static const double DP1 = 3.14159265160560607910E0;
60*2fe8fb19SBen Gras static const double DP2 = 1.98418714791870343106E-9;
61*2fe8fb19SBen Gras static const double DP3 = 1.14423774522196636802E-17;
62*2fe8fb19SBen Gras #define MACHEP 1.1e-16
63*2fe8fb19SBen Gras 
64*2fe8fb19SBen Gras double
_redupi(double x)65*2fe8fb19SBen Gras _redupi(double x)
66*2fe8fb19SBen Gras {
67*2fe8fb19SBen Gras 	double t;
68*2fe8fb19SBen Gras 	long i;
69*2fe8fb19SBen Gras 
70*2fe8fb19SBen Gras 	t = x / M_PI;
71*2fe8fb19SBen Gras 	if (t >= 0.0)
72*2fe8fb19SBen Gras 		t += 0.5;
73*2fe8fb19SBen Gras 	else
74*2fe8fb19SBen Gras 		t -= 0.5;
75*2fe8fb19SBen Gras 
76*2fe8fb19SBen Gras 	i = t;	/* the multiple */
77*2fe8fb19SBen Gras 	t = i;
78*2fe8fb19SBen Gras 	t = ((x - t * DP1) - t * DP2) - t * DP3;
79*2fe8fb19SBen Gras 	return t;
80*2fe8fb19SBen Gras }
81*2fe8fb19SBen Gras 
82*2fe8fb19SBen Gras /* Taylor series expansion for cosh(2y) - cos(2x) */
83*2fe8fb19SBen Gras 
84*2fe8fb19SBen Gras double
_ctans(double complex z)85*2fe8fb19SBen Gras _ctans(double complex z)
86*2fe8fb19SBen Gras {
87*2fe8fb19SBen Gras 	double f, x, x2, y, y2, rn, t;
88*2fe8fb19SBen Gras 	double d;
89*2fe8fb19SBen Gras 
90*2fe8fb19SBen Gras 	x = fabs(2.0 * creal(z));
91*2fe8fb19SBen Gras 	y = fabs(2.0 * cimag(z));
92*2fe8fb19SBen Gras 
93*2fe8fb19SBen Gras 	x = _redupi(x);
94*2fe8fb19SBen Gras 
95*2fe8fb19SBen Gras 	x = x * x;
96*2fe8fb19SBen Gras 	y = y * y;
97*2fe8fb19SBen Gras 	x2 = 1.0;
98*2fe8fb19SBen Gras 	y2 = 1.0;
99*2fe8fb19SBen Gras 	f = 1.0;
100*2fe8fb19SBen Gras 	rn = 0.0;
101*2fe8fb19SBen Gras 	d = 0.0;
102*2fe8fb19SBen Gras 	do {
103*2fe8fb19SBen Gras 		rn += 1.0;
104*2fe8fb19SBen Gras 		f *= rn;
105*2fe8fb19SBen Gras 		rn += 1.0;
106*2fe8fb19SBen Gras 		f *= rn;
107*2fe8fb19SBen Gras 		x2 *= x;
108*2fe8fb19SBen Gras 		y2 *= y;
109*2fe8fb19SBen Gras 		t = y2 + x2;
110*2fe8fb19SBen Gras 		t /= f;
111*2fe8fb19SBen Gras 		d += t;
112*2fe8fb19SBen Gras 
113*2fe8fb19SBen Gras 		rn += 1.0;
114*2fe8fb19SBen Gras 		f *= rn;
115*2fe8fb19SBen Gras 		rn += 1.0;
116*2fe8fb19SBen Gras 		f *= rn;
117*2fe8fb19SBen Gras 		x2 *= x;
118*2fe8fb19SBen Gras 		y2 *= y;
119*2fe8fb19SBen Gras 		t = y2 - x2;
120*2fe8fb19SBen Gras 		t /= f;
121*2fe8fb19SBen Gras 		d += t;
122*2fe8fb19SBen Gras 	} while (fabs(t/d) > MACHEP);
123*2fe8fb19SBen Gras 	return d;
124*2fe8fb19SBen Gras }
125