1/* mpfr_tan -- tangent of a floating-point number
2
3Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
4Contributed by the AriC and Caramel projects, INRIA.
5
6This file is part of the GNU MPFR Library.
7
8The GNU MPFR Library is free software; you can redistribute it and/or modify
9it under the terms of the GNU Lesser General Public License as published by
10the Free Software Foundation; either version 3 of the License, or (at your
11option) any later version.
12
13The GNU MPFR Library is distributed in the hope that it will be useful, but
14WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
16License for more details.
17
18You should have received a copy of the GNU Lesser General Public License
19along with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
20http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
2151 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
22
23#define MPFR_NEED_LONGLONG_H
24#include "mpfr-impl.h"
25
26/* computes tan(x) = sign(x)*sqrt(1/cos(x)^2-1) */
27int
28mpfr_tan (mpfr_ptr y, mpfr_srcptr x, mpfr_rnd_t rnd_mode)
29{
30  mpfr_prec_t precy, m;
31  int inexact;
32  mpfr_t s, c;
33  MPFR_ZIV_DECL (loop);
34  MPFR_SAVE_EXPO_DECL (expo);
35  MPFR_GROUP_DECL (group);
36
37  MPFR_LOG_FUNC
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));
41
42  if (MPFR_UNLIKELY(MPFR_IS_SINGULAR(x)))
43    {
44      if (MPFR_IS_NAN(x) || MPFR_IS_INF(x))
45        {
46          MPFR_SET_NAN(y);
47          MPFR_RET_NAN;
48        }
49      else /* x is zero */
50        {
51          MPFR_ASSERTD(MPFR_IS_ZERO(x));
52          MPFR_SET_ZERO(y);
53          MPFR_SET_SAME_SIGN(y, x);
54          MPFR_RET(0);
55        }
56    }
57
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,
60                                    rnd_mode, {});
61
62  MPFR_SAVE_EXPO_MARK (expo);
63
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 */
68
69  MPFR_GROUP_INIT_2 (group, m, s, c);
70  MPFR_ZIV_INIT (loop, m);
71  for (;;)
72    {
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)))
79        break;
80      MPFR_ZIV_NEXT (loop, m);
81      MPFR_GROUP_REPREC_2 (group, m, s, c);
82    }
83  MPFR_ZIV_FREE (loop);
84  inexact = mpfr_set (y, c, rnd_mode);
85  MPFR_GROUP_CLEAR (group);
86
87  MPFR_SAVE_EXPO_FREE (expo);
88  return mpfr_check_range (y, inexact, rnd_mode);
89}
90