]> git.ipfire.org Git - thirdparty/glibc.git/blame - math/s_ctan_template.c
Do not include fenv_private.h in math_private.h.
[thirdparty/glibc.git] / math / s_ctan_template.c
CommitLineData
d5602ceb 1/* Complex tangent function for a complex float type.
688903eb 2 Copyright (C) 1997-2018 Free Software Foundation, Inc.
f6d3a72e
PM
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
5
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
19
20#include <complex.h>
21#include <fenv.h>
22#include <math.h>
23#include <math_private.h>
70e2ba33 24#include <fenv_private.h>
8f5b00d3 25#include <math-underflow.h>
f6d3a72e
PM
26#include <float.h>
27
d5602ceb
PM
28CFLOAT
29M_DECL_FUNC (__ctan) (CFLOAT x)
f6d3a72e 30{
d5602ceb 31 CFLOAT res;
f6d3a72e
PM
32
33 if (__glibc_unlikely (!isfinite (__real__ x) || !isfinite (__imag__ x)))
34 {
35 if (isinf (__imag__ x))
36 {
d5602ceb 37 if (isfinite (__real__ x) && M_FABS (__real__ x) > 1)
f6d3a72e 38 {
d5602ceb
PM
39 FLOAT sinrx, cosrx;
40 M_SINCOS (__real__ x, &sinrx, &cosrx);
41 __real__ res = M_COPYSIGN (0, sinrx * cosrx);
f6d3a72e
PM
42 }
43 else
d5602ceb
PM
44 __real__ res = M_COPYSIGN (0, __real__ x);
45 __imag__ res = M_COPYSIGN (1, __imag__ x);
f6d3a72e 46 }
d5602ceb 47 else if (__real__ x == 0)
f6d3a72e
PM
48 {
49 res = x;
50 }
51 else
52 {
d5602ceb 53 __real__ res = M_NAN;
d15e83c5
JM
54 if (__imag__ x == 0)
55 __imag__ res = __imag__ x;
56 else
57 __imag__ res = M_NAN;
f6d3a72e
PM
58
59 if (isinf (__real__ x))
60 feraiseexcept (FE_INVALID);
61 }
62 }
63 else
64 {
d5602ceb
PM
65 FLOAT sinrx, cosrx;
66 FLOAT den;
67 const int t = (int) ((M_MAX_EXP - 1) * M_MLIT (M_LN2) / 2);
f6d3a72e
PM
68
69 /* tan(x+iy) = (sin(2x) + i*sinh(2y))/(cos(2x) + cosh(2y))
70 = (sin(x)*cos(x) + i*sinh(y)*cosh(y)/(cos(x)^2 + sinh(y)^2). */
71
d5602ceb 72 if (__glibc_likely (M_FABS (__real__ x) > M_MIN))
f6d3a72e 73 {
d5602ceb 74 M_SINCOS (__real__ x, &sinrx, &cosrx);
f6d3a72e
PM
75 }
76 else
77 {
78 sinrx = __real__ x;
d5602ceb 79 cosrx = 1;
f6d3a72e
PM
80 }
81
d5602ceb 82 if (M_FABS (__imag__ x) > t)
f6d3a72e
PM
83 {
84 /* Avoid intermediate overflow when the real part of the
85 result may be subnormal. Ignoring negligible terms, the
86 imaginary part is +/- 1, the real part is
87 sin(x)*cos(x)/sinh(y)^2 = 4*sin(x)*cos(x)/exp(2y). */
d5602ceb 88 FLOAT exp_2t = M_EXP (2 * t);
f6d3a72e 89
d5602ceb 90 __imag__ res = M_COPYSIGN (1, __imag__ x);
f6d3a72e 91 __real__ res = 4 * sinrx * cosrx;
d5602ceb 92 __imag__ x = M_FABS (__imag__ x);
f6d3a72e
PM
93 __imag__ x -= t;
94 __real__ res /= exp_2t;
95 if (__imag__ x > t)
96 {
97 /* Underflow (original imaginary part of x has absolute
98 value > 2t). */
99 __real__ res /= exp_2t;
100 }
101 else
d5602ceb 102 __real__ res /= M_EXP (2 * __imag__ x);
f6d3a72e
PM
103 }
104 else
105 {
d5602ceb
PM
106 FLOAT sinhix, coshix;
107 if (M_FABS (__imag__ x) > M_MIN)
f6d3a72e 108 {
d5602ceb
PM
109 sinhix = M_SINH (__imag__ x);
110 coshix = M_COSH (__imag__ x);
f6d3a72e
PM
111 }
112 else
113 {
114 sinhix = __imag__ x;
d5602ceb 115 coshix = 1;
f6d3a72e
PM
116 }
117
d5602ceb 118 if (M_FABS (sinhix) > M_FABS (cosrx) * M_EPSILON)
f6d3a72e
PM
119 den = cosrx * cosrx + sinhix * sinhix;
120 else
121 den = cosrx * cosrx;
122 __real__ res = sinrx * cosrx / den;
123 __imag__ res = sinhix * coshix / den;
124 }
125 math_check_force_underflow_complex (res);
126 }
127
128 return res;
129}
d5602ceb
PM
130
131declare_mgen_alias (__ctan, ctan)