xref: /openbsd/lib/libm/src/s_casin.c (revision cca36db2)
1 /*	$OpenBSD: s_casin.c,v 1.2 2011/07/08 19:25:31 martynas Exp $	*/
2 /*
3  * Copyright (c) 2008 Stephen L. Moshier <steve@moshier.net>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 /* LINTLIBRARY */
19 
20 /*							casin()
21  *
22  *	Complex circular arc sine
23  *
24  *
25  *
26  * SYNOPSIS:
27  *
28  * double complex casin();
29  * double complex z, w;
30  *
31  * w = casin (z);
32  *
33  *
34  *
35  * DESCRIPTION:
36  *
37  * Inverse complex sine:
38  *
39  *                               2
40  * w = -i clog( iz + csqrt( 1 - z ) ).
41  *
42  * casin(z) = -i casinh(iz)
43  *
44  * ACCURACY:
45  *
46  *                      Relative error:
47  * arithmetic   domain     # trials      peak         rms
48  *    DEC       -10,+10     10100       2.1e-15     3.4e-16
49  *    IEEE      -10,+10     30000       2.2e-14     2.7e-15
50  * Larger relative error can be observed for z near zero.
51  * Also tested by csin(casin(z)) = z.
52  */
53 
54 #include <sys/cdefs.h>
55 #include <complex.h>
56 #include <float.h>
57 #include <math.h>
58 
59 double complex
60 casin(double complex z)
61 {
62 	double complex w;
63 	static double complex ca, ct, zz, z2;
64 	double x, y;
65 
66 	x = creal (z);
67 	y = cimag (z);
68 
69 	if (y == 0.0) {
70 		if (fabs(x) > 1.0) {
71 			w = M_PI_2 + 0.0 * I;
72 			/*mtherr ("casin", DOMAIN);*/
73 		}
74 		else {
75 			w = asin (x) + 0.0 * I;
76 		}
77 		return (w);
78 	}
79 
80 	/* Power series expansion */
81 	/*
82 	b = cabs(z);
83 	if( b < 0.125 ) {
84 		z2.r = (x - y) * (x + y);
85 		z2.i = 2.0 * x * y;
86 
87 		cn = 1.0;
88 		n = 1.0;
89 		ca.r = x;
90 		ca.i = y;
91 		sum.r = x;
92 		sum.i = y;
93 		do {
94 			ct.r = z2.r * ca.r  -  z2.i * ca.i;
95 			ct.i = z2.r * ca.i  +  z2.i * ca.r;
96 			ca.r = ct.r;
97 			ca.i = ct.i;
98 
99 			cn *= n;
100 			n += 1.0;
101 			cn /= n;
102 			n += 1.0;
103 			b = cn/n;
104 
105 			ct.r *= b;
106 			ct.i *= b;
107 			sum.r += ct.r;
108 			sum.i += ct.i;
109 			b = fabs(ct.r) + fabs(ct.i);
110 		}
111 		while( b > MACHEP );
112 		w->r = sum.r;
113 		w->i = sum.i;
114 		return;
115 	}
116 	*/
117 
118 	ca = x + y * I;
119 	ct = ca * I;
120 	/* sqrt( 1 - z*z) */
121 	/* cmul( &ca, &ca, &zz ) */
122 	/*x * x  -  y * y */
123 	zz = (x - y) * (x + y) + (2.0 * x * y) * I;
124 
125 	zz = 1.0 - creal(zz) - cimag(zz) * I;
126 	z2 = csqrt (zz);
127 
128 	zz = ct + z2;
129 	zz = clog (zz);
130 	/* multiply by 1/i = -i */
131 	w = zz * (-1.0 * I);
132 	return (w);
133 }
134 
135 #if	LDBL_MANT_DIG == 53
136 #ifdef	lint
137 /* PROTOLIB1 */
138 long double complex casinl(long double complex);
139 #else	/* lint */
140 __weak_alias(casinl, casin);
141 #endif	/* lint */
142 #endif	/* LDBL_MANT_DIG == 53 */
143