summaryrefslogtreecommitdiff
path: root/lib/libm/ctanf.c
blob: 168abac26b004b36832f569537bde581ac001f1d (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
56
57
58
59
#include "__complex.h" // for redupif, MACHEPF

#include <complex.h> // for cimagf, crealf, complex, I, ctanf
#include <math.h>    // for fabsf, HUGE_VALF, cosf, coshf, sinf, sinhf

static float _ctansf(float complex z)
{
	float f, x, x2, y, y2, rn, t, d;

	x = fabsf(2.0f * crealf(z));
	y = fabsf(2.0f * cimagf(z));
	x = redupif(x);
	x = x * x;
	y = y * y;

	x2 = 1.0f;
	y2 = 1.0f;
	f = 1.0f;
	rn = 0.0f;
	d = 0.0f;

	do {
		rn += 1.0f;
		f *= rn;
		rn += 1.0f;
		f *= rn;
		x2 *= x;
		y2 *= y;
		t = y2 + x2;
		t /= f;
		d += t;

		rn += 1.0f;
		f *= rn;
		rn += 1.0f;
		f *= rn;
		x2 *= x;
		y2 *= y;
		t = y2 - x2;
		t /= f;
		d += t;
	} while (fabsf(t / d) > MACHEPF);

	return d;
}

float complex ctanf(float complex z)
{
	float f = cosf(2.0f * crealf(z)) + coshf(2.0f * cimagf(z));

	if (fabsf(f) < 0.25f)
		f = _ctansf(z);

	if (f == 0.0f) {
		return HUGE_VALF + HUGE_VALF * I;
	}

	return sinf(2.0f * crealf(z)) / f + (sinhf(2.0f * cimagf(z)) / f) * I;
}