1 /* mpfr_tan -- tangent of a floating-point number
3 Copyright 2001-2015 Free Software Foundation, Inc.
4 Contributed by the AriC and Caramel projects, INRIA.
6 This file is part of the GNU MPFR Library.
8 The GNU MPFR Library is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or (at your
11 option) any later version.
13 The GNU MPFR Library is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16 License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with the GNU MPFR Library; see the file COPYING.LESSER. If not, see
20 http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
21 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
23 #define MPFR_NEED_LONGLONG_H
24 #include "mpfr-impl.h"
26 /* computes tan(x) = sign(x)*sqrt(1/cos(x)^2-1) */
28 mpfr_tan (mpfr_ptr y
, mpfr_srcptr x
, mpfr_rnd_t rnd_mode
)
34 MPFR_SAVE_EXPO_DECL (expo
);
35 MPFR_GROUP_DECL (group
);
38 (("x[%Pu]=%.*Rg rnd=%d", mpfr_get_prec (x
), mpfr_log_prec
, x
, rnd_mode
),
39 ("y[%Pu]=%.*Rg inexact=%d",
40 mpfr_get_prec (y
), mpfr_log_prec
, y
, inexact
));
42 if (MPFR_UNLIKELY(MPFR_IS_SINGULAR(x
)))
44 if (MPFR_IS_NAN(x
) || MPFR_IS_INF(x
))
51 MPFR_ASSERTD(MPFR_IS_ZERO(x
));
53 MPFR_SET_SAME_SIGN(y
, x
);
58 /* tan(x) = x + x^3/3 + ... so the error is < 2^(3*EXP(x)-1) */
59 MPFR_FAST_COMPUTE_IF_SMALL_INPUT (y
, x
, -2 * MPFR_GET_EXP (x
), 1, 1,
62 MPFR_SAVE_EXPO_MARK (expo
);
64 /* Compute initial precision */
65 precy
= MPFR_PREC (y
);
66 m
= precy
+ MPFR_INT_CEIL_LOG2 (precy
) + 13;
67 MPFR_ASSERTD (m
>= 2); /* needed for the error analysis in algorithms.tex */
69 MPFR_GROUP_INIT_2 (group
, m
, s
, c
);
70 MPFR_ZIV_INIT (loop
, m
);
73 /* The only way to get an overflow is to get ~ Pi/2
74 But the result will be ~ 2^Prec(y). */
75 mpfr_sin_cos (s
, c
, x
, MPFR_RNDN
); /* err <= 1/2 ulp on s and c */
76 mpfr_div (c
, s
, c
, MPFR_RNDN
); /* err <= 4 ulps */
77 MPFR_ASSERTD (!MPFR_IS_SINGULAR (c
));
78 if (MPFR_LIKELY (MPFR_CAN_ROUND (c
, m
- 2, precy
, rnd_mode
)))
80 MPFR_ZIV_NEXT (loop
, m
);
81 MPFR_GROUP_REPREC_2 (group
, m
, s
, c
);
84 inexact
= mpfr_set (y
, c
, rnd_mode
);
85 MPFR_GROUP_CLEAR (group
);
87 MPFR_SAVE_EXPO_FREE (expo
);
88 return mpfr_check_range (y
, inexact
, rnd_mode
);