1/*	$OpenBSD: s_casinf.c,v 1.5 2016/09/12 19:47:02 guenther 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/*							casinf()
19 *
20 *	Complex circular arc sine
21 *
22 *
23 *
24 * SYNOPSIS:
25 *
26 * void casinf();
27 * cmplxf z, w;
28 *
29 * casinf( &z, &w );
30 *
31 *
32 *
33 * DESCRIPTION:
34 *
35 * Inverse complex sine:
36 *
37 *                               2
38 * w = -i clog( iz + csqrt( 1 - z ) ).
39 *
40 *
41 * ACCURACY:
42 *
43 *                      Relative error:
44 * arithmetic   domain     # trials      peak         rms
45 *    IEEE      -10,+10     30000       1.1e-5      1.5e-6
46 * Larger relative error can be observed for z near zero.
47 *
48 */
49
50#include <complex.h>
51#include <math.h>
52
53float complex
54casinf(float complex z)
55{
56	float complex w;
57	float x, y;
58	static float complex ca, ct, zz, z2;
59	/*
60	float cn, n;
61	static float a, b, s, t, u, v, y2;
62	static cmplxf sum;
63	*/
64
65	x = crealf(z);
66	y = cimagf(z);
67
68#if 0
69	if(y == 0.0f) {
70		if(fabsf(x) > 1.0f) {
71			w = (float)M_PI_2 + 0.0f * I;
72			/*mtherr( "casinf", DOMAIN );*/
73		}
74		else {
75			w = asinf (x) + 0.0f * I;
76		}
77		return (w);
78	}
79#endif
80
81	/* Power series expansion */
82	/*
83	b = cabsf(z);
84	if(b < 0.125) {
85		z2.r = (x - y) * (x + y);
86		z2.i = 2.0 * x * y;
87
88		cn = 1.0;
89		n = 1.0;
90		ca.r = x;
91		ca.i = y;
92		sum.r = x;
93		sum.i = y;
94		do {
95			ct.r = z2.r * ca.r  -  z2.i * ca.i;
96			ct.i = z2.r * ca.i  +  z2.i * ca.r;
97			ca.r = ct.r;
98			ca.i = ct.i;
99
100			cn *= n;
101			n += 1.0;
102			cn /= n;
103			n += 1.0;
104			b = cn/n;
105
106			ct.r *= b;
107			ct.i *= b;
108			sum.r += ct.r;
109			sum.i += ct.i;
110			b = fabsf(ct.r) + fabsf(ct.i);
111		}
112		while(b > MACHEPF);
113		w->r = sum.r;
114		w->i = sum.i;
115		return;
116	}
117	*/
118
119
120	ca = x + y * I;
121	ct = ca * I;	/* iz */
122	/* sqrt( 1 - z*z) */
123	/* cmul( &ca, &ca, &zz ) */
124	/*x * x  -  y * y */
125	zz = (x - y) * (x + y) + (2.0f * x * y) * I;
126	zz = 1.0f - crealf(zz) - cimagf(zz) * I;
127	z2 = csqrtf (zz);
128
129	zz = ct + z2;
130	zz = clogf (zz);
131	/* multiply by 1/i = -i */
132	w = zz * (-1.0f * I);
133	return (w);
134}
135DEF_STD(casinf);
136