2016-06-28 16:06:42 +00:00
|
|
|
/* Complex tangent function for a complex float type.
|
2019-01-01 00:11:28 +00:00
|
|
|
Copyright (C) 1997-2019 Free Software Foundation, Inc.
|
2016-07-01 16:00:21 +00:00
|
|
|
This file is part of the GNU C Library.
|
|
|
|
Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
|
|
|
|
|
|
|
|
The GNU C Library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
The GNU C Library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with the GNU C Library; if not, see
|
|
|
|
<http://www.gnu.org/licenses/>. */
|
|
|
|
|
|
|
|
#include <complex.h>
|
|
|
|
#include <fenv.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <math_private.h>
|
2018-05-10 00:53:04 +00:00
|
|
|
#include <math-underflow.h>
|
2016-07-01 16:00:21 +00:00
|
|
|
#include <float.h>
|
|
|
|
|
2016-06-28 16:06:42 +00:00
|
|
|
CFLOAT
|
|
|
|
M_DECL_FUNC (__ctan) (CFLOAT x)
|
2016-07-01 16:00:21 +00:00
|
|
|
{
|
2016-06-28 16:06:42 +00:00
|
|
|
CFLOAT res;
|
2016-07-01 16:00:21 +00:00
|
|
|
|
|
|
|
if (__glibc_unlikely (!isfinite (__real__ x) || !isfinite (__imag__ x)))
|
|
|
|
{
|
|
|
|
if (isinf (__imag__ x))
|
|
|
|
{
|
2016-06-28 16:06:42 +00:00
|
|
|
if (isfinite (__real__ x) && M_FABS (__real__ x) > 1)
|
2016-07-01 16:00:21 +00:00
|
|
|
{
|
2016-06-28 16:06:42 +00:00
|
|
|
FLOAT sinrx, cosrx;
|
|
|
|
M_SINCOS (__real__ x, &sinrx, &cosrx);
|
|
|
|
__real__ res = M_COPYSIGN (0, sinrx * cosrx);
|
2016-07-01 16:00:21 +00:00
|
|
|
}
|
|
|
|
else
|
2016-06-28 16:06:42 +00:00
|
|
|
__real__ res = M_COPYSIGN (0, __real__ x);
|
|
|
|
__imag__ res = M_COPYSIGN (1, __imag__ x);
|
2016-07-01 16:00:21 +00:00
|
|
|
}
|
2016-06-28 16:06:42 +00:00
|
|
|
else if (__real__ x == 0)
|
2016-07-01 16:00:21 +00:00
|
|
|
{
|
|
|
|
res = x;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-06-28 16:06:42 +00:00
|
|
|
__real__ res = M_NAN;
|
2017-12-07 16:21:00 +00:00
|
|
|
if (__imag__ x == 0)
|
|
|
|
__imag__ res = __imag__ x;
|
|
|
|
else
|
|
|
|
__imag__ res = M_NAN;
|
2016-07-01 16:00:21 +00:00
|
|
|
|
|
|
|
if (isinf (__real__ x))
|
|
|
|
feraiseexcept (FE_INVALID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-06-28 16:06:42 +00:00
|
|
|
FLOAT sinrx, cosrx;
|
|
|
|
FLOAT den;
|
|
|
|
const int t = (int) ((M_MAX_EXP - 1) * M_MLIT (M_LN2) / 2);
|
2016-07-01 16:00:21 +00:00
|
|
|
|
|
|
|
/* tan(x+iy) = (sin(2x) + i*sinh(2y))/(cos(2x) + cosh(2y))
|
|
|
|
= (sin(x)*cos(x) + i*sinh(y)*cosh(y)/(cos(x)^2 + sinh(y)^2). */
|
|
|
|
|
2016-06-28 16:06:42 +00:00
|
|
|
if (__glibc_likely (M_FABS (__real__ x) > M_MIN))
|
2016-07-01 16:00:21 +00:00
|
|
|
{
|
2016-06-28 16:06:42 +00:00
|
|
|
M_SINCOS (__real__ x, &sinrx, &cosrx);
|
2016-07-01 16:00:21 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sinrx = __real__ x;
|
2016-06-28 16:06:42 +00:00
|
|
|
cosrx = 1;
|
2016-07-01 16:00:21 +00:00
|
|
|
}
|
|
|
|
|
2016-06-28 16:06:42 +00:00
|
|
|
if (M_FABS (__imag__ x) > t)
|
2016-07-01 16:00:21 +00:00
|
|
|
{
|
|
|
|
/* Avoid intermediate overflow when the real part of the
|
|
|
|
result may be subnormal. Ignoring negligible terms, the
|
|
|
|
imaginary part is +/- 1, the real part is
|
|
|
|
sin(x)*cos(x)/sinh(y)^2 = 4*sin(x)*cos(x)/exp(2y). */
|
2016-06-28 16:06:42 +00:00
|
|
|
FLOAT exp_2t = M_EXP (2 * t);
|
2016-07-01 16:00:21 +00:00
|
|
|
|
2016-06-28 16:06:42 +00:00
|
|
|
__imag__ res = M_COPYSIGN (1, __imag__ x);
|
2016-07-01 16:00:21 +00:00
|
|
|
__real__ res = 4 * sinrx * cosrx;
|
2016-06-28 16:06:42 +00:00
|
|
|
__imag__ x = M_FABS (__imag__ x);
|
2016-07-01 16:00:21 +00:00
|
|
|
__imag__ x -= t;
|
|
|
|
__real__ res /= exp_2t;
|
|
|
|
if (__imag__ x > t)
|
|
|
|
{
|
|
|
|
/* Underflow (original imaginary part of x has absolute
|
|
|
|
value > 2t). */
|
|
|
|
__real__ res /= exp_2t;
|
|
|
|
}
|
|
|
|
else
|
2016-06-28 16:06:42 +00:00
|
|
|
__real__ res /= M_EXP (2 * __imag__ x);
|
2016-07-01 16:00:21 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-06-28 16:06:42 +00:00
|
|
|
FLOAT sinhix, coshix;
|
|
|
|
if (M_FABS (__imag__ x) > M_MIN)
|
2016-07-01 16:00:21 +00:00
|
|
|
{
|
2016-06-28 16:06:42 +00:00
|
|
|
sinhix = M_SINH (__imag__ x);
|
|
|
|
coshix = M_COSH (__imag__ x);
|
2016-07-01 16:00:21 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sinhix = __imag__ x;
|
2016-06-28 16:06:42 +00:00
|
|
|
coshix = 1;
|
2016-07-01 16:00:21 +00:00
|
|
|
}
|
|
|
|
|
2016-06-28 16:06:42 +00:00
|
|
|
if (M_FABS (sinhix) > M_FABS (cosrx) * M_EPSILON)
|
2016-07-01 16:00:21 +00:00
|
|
|
den = cosrx * cosrx + sinhix * sinhix;
|
|
|
|
else
|
|
|
|
den = cosrx * cosrx;
|
|
|
|
__real__ res = sinrx * cosrx / den;
|
|
|
|
__imag__ res = sinhix * coshix / den;
|
|
|
|
}
|
|
|
|
math_check_force_underflow_complex (res);
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
2016-06-28 16:06:42 +00:00
|
|
|
|
|
|
|
declare_mgen_alias (__ctan, ctan)
|