summaryrefslogtreecommitdiff
path: root/lib/libm/ctan.c
blob: 8be23bb77755daad855c825bf92914017f374f5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "__complex.h" // for redupi, MACHEP

#include <complex.h> // for cimag, creal, complex, I, ctan
#include <math.h>    // for fabs, HUGE_VAL, cos, cosh, sin, sinh

double _ctans(double complex z)
{
	double f, x, x2, y, y2, rn, t;
	double d;
	x = fabs(2.0 * creal(z));
	y = fabs(2.0 * cimag(z));
	x = redupi(x);
	x = x * x;
	y = y * y;
	x2 = 1.0;
	y2 = 1.0;
	f = 1.0;
	rn = 0.0;
	d = 0.0;
	do {
		rn += 1.0;
		f *= rn;
		rn += 1.0;
		f *= rn;
		x2 *= x;
		y2 *= y;
		t = y2 + x2;
		t /= f;
		d += t;
		rn += 1.0;
		f *= rn;
		rn += 1.0;
		f *= rn;
		x2 *= x;
		y2 *= y;
		t = y2 - x2;
		t /= f;
		d += t;
	} while (fabs(t / d) > MACHEP);
	return d;
}

double complex ctan(double complex z)
{
	double d = cos(2.0 * creal(z)) + cosh(2.0 * cimag(z));

	if (fabs(d) < 0.25)
		d = _ctans(z);

	if (d == 0.0) {
		return HUGE_VAL + HUGE_VAL * (double complex)I;
	}

	return sin(2.0 * creal(z)) / d + (sinh(2.0 * cimag(z)) / d) * (double complex)I;
}