summaryrefslogtreecommitdiff
path: root/src/tanu.c
blob: 1d9710a8b1d56db4f6b9d082ebeac0b92d967b93 (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/* mpfr_tanu -- tanu(x) = tan(2*pi*x/u)

Copyright 2020 Free Software Foundation, Inc.
Contributed by the AriC and Caramba projects, INRIA.

This file is part of the GNU MPFR Library.

The GNU MPFR 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 3 of the License, or (at your
option) any later version.

The GNU MPFR 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 MPFR Library; see the file COPYING.LESSER.  If not, see
https://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */

#define MPFR_NEED_LONGLONG_H
#include "mpfr-impl.h"

/* FIXME[VL]: Implement the range reduction in this function.
   That's the whole point of tanu compared to tan. */

/* put in y the corrected-rounded value of tan(2*pi*x/u) */
int
mpfr_tanu (mpfr_ptr y, mpfr_srcptr x, unsigned long u, mpfr_rnd_t rnd_mode)
{
  mpfr_prec_t precy, prec;
  mpfr_exp_t expx, expt, err;
  mpfr_t t;
  int inexact = 0, nloops = 0, underflow = 0;
  MPFR_ZIV_DECL (loop);
  MPFR_SAVE_EXPO_DECL (expo);

  MPFR_LOG_FUNC (
    ("x[%Pu]=%*.Rg rnd=%d", mpfr_get_prec (x), mpfr_log_prec, x, rnd_mode),
    ("y[%Pu]=%*.Rg inexact=%d", mpfr_get_prec (y), mpfr_log_prec, y,
     inexact));

  if (u == 0 || MPFR_UNLIKELY (MPFR_IS_SINGULAR (x)))
    {
      /* for u=0, return NaN */
      if (u == 0 || MPFR_IS_NAN (x) || MPFR_IS_INF (x))
        {
          MPFR_SET_NAN (y);
          MPFR_RET_NAN;
        }
      else /* x is zero */
        {
          MPFR_ASSERTD (MPFR_IS_ZERO (x));
          MPFR_SET_ZERO (y);
          MPFR_SET_SAME_SIGN (y, x);
          MPFR_RET (0);
        }
    }

  MPFR_SAVE_EXPO_MARK (expo);

  precy = MPFR_PREC (y);
  expx = MPFR_GET_EXP (x);
  /* for x large, since argument reduction is expensive, we want to avoid
     any failure in Ziv's strategy, thus we take into account expx too */
  prec = precy + MPFR_INT_CEIL_LOG2 (MAX(precy,expx)) + 8;
  MPFR_ASSERTD(prec >= 2);
  mpfr_init2 (t, prec);
  MPFR_ZIV_INIT (loop, prec);
  for (;;)
    {
      int inex;
      nloops ++;
      /* We first compute an approximation t of 2*pi*x/u, then call tan(t).
         If t = 2*pi*x/u + s, then
         |tan(t) - tan(2*pi*x/u)| = |s| * (1 + tan(v)^2) where v is in the
         interval [t, t+s]. If we ensure that |t| >= |2*pi*x/u|, since tan() is
         increasing, we can bound tan(v)^2 by tan(t)^2. */
      mpfr_set_prec (t, prec);
      mpfr_const_pi (t, MPFR_RNDU); /* t = pi * (1 + theta1) where
                                       |theta1| <= 2^(1-prec) */
      mpfr_mul_2ui (t, t, 1, MPFR_RNDN); /* t = 2*pi * (1 + theta1) */
      mpfr_mul (t, t, x, MPFR_RNDA);     /* t = 2*pi*x * (1 + theta2)^2 where
                                            |theta2| <= 2^(1-prec) */
      inex = mpfr_div_ui (t, t, u, MPFR_RNDN);
      /* t = 2*pi*x/u * (1 + theta3)^3 where |theta3| <= 2^(1-prec) */
      /* if t is zero here, it means the division by u underflows, then
         tan(t) also underflows, since |tan(x)| <= |x|. */
      if (MPFR_UNLIKELY (MPFR_IS_ZERO (t)))
        {
          inexact = mpfr_underflow (y, rnd_mode, MPFR_SIGN(t));
          MPFR_SAVE_EXPO_UPDATE_FLAGS (expo, MPFR_FLAGS_INEXACT
                                       | MPFR_FLAGS_UNDERFLOW);
          underflow = 1;
          goto end;
        }
      /* emulate mpfr_div_ui (t, t, u, MPFR_RNDA) above, so that t is rounded
         away from zero */
      if (MPFR_SIGN(t) > 0 && inex < 0)
        mpfr_nextabove (t);
      else if (MPFR_SIGN(t) < 0 && inex > 0)
        mpfr_nextbelow (t);
      expt = MPFR_GET_EXP (t);
      /* since prec >= 3, |(1 + theta3)^3 - 1| <= 4*theta3 <= 2^(3-prec)
         thus |s| = |t - 2*pi*x/u| <= |t| * 2^(3-prec) */
      mpfr_tan (t, t, MPFR_RNDA);
      {
        /* compute an upper bound for 1+tan(t)^2 */
        mpfr_t z;
        mpfr_init2 (z, 64);
        mpfr_sqr (z, t, MPFR_RNDU);
        mpfr_add_ui (z, z, 1, MPFR_RNDU);
        expt += MPFR_GET_EXP (z);
        /* now |t - tan(2*pi*x/u)| <= ulp(t) + 2^(expt + 3 - prec) */
        mpfr_clear (z);
      }
      /* t cannot be zero here, since we excluded t=0 before, which is the
         only exact case where tan(t)=0, and we round away from zero */
      err = expt + 3 - prec;
      expt = MPFR_GET_EXP (t); /* new exponent of t */
      /* the total error is bounded by 2^err + ulp(t) = 2^err + 2^(expt-prec)
         thus if err <= expt-prec, it is bounded by 2^(expt-prec+1),
         otherwise it is bounded by 2^(err+1). */
      err = (err <= expt - prec) ? expt - prec + 1 : err + 1;
      /* normalize err for mpfr_can_round */
      err = expt - err;
      if (MPFR_CAN_ROUND (t, err, precy, rnd_mode))
        break;
      /* Check exact cases only after the first level of Ziv' strategy, to
         avoid slowing down the average case. Exact cases are when 2*pi*x/u
         is a multiple of pi/4, i.e., x/u a multiple of 1/8:
         (a) x/u = {0,1/2} mod 1: return +0 or -0
         (b) x/u = {1/4,3/4} mod 1: return +Inf or -Inf
         (c) x/u = {1/8,3/8,5/8,7/8} mod 1: return 1 or -1 */
      if (nloops == 1)
        {
          inexact = mpfr_div_ui (t, x, u, MPFR_RNDA);
          mpfr_mul_2ui (t, t, 3, MPFR_RNDA);
          if (inexact == 0 && mpfr_integer_p (t))
            {
              mpz_t z;
              unsigned long mod8;
              mpz_init (z);
              inexact = mpfr_get_z (z, t, MPFR_RNDZ);
              MPFR_ASSERTN(inexact == 0);
              mod8 = mpz_fdiv_ui (z, 8);
              mpz_clear (z);
              if (mod8 == 0 || mod8 == 4) /* case (a) */
                mpfr_set_zero (y, ((mod8 == 0) ? +1 : -1) * MPFR_SIGN (x));
              else if (mod8 == 2 || mod8 == 6) /* case (b) */
                {
                  mpfr_set_inf (y, (mod8 == 2) ? +1 : -1);
                  MPFR_SAVE_EXPO_UPDATE_FLAGS (expo, MPFR_FLAGS_DIVBY0);
                }
              else /* case (c) */
                {
                  if (mod8 == 1 || mod8 == 5)
                    mpfr_set_ui (y, 1, rnd_mode);
                  else
                    mpfr_set_si (y, -1, rnd_mode);
                }
              goto end;
            }
        }
      MPFR_ZIV_NEXT (loop, prec);
    }
  MPFR_ZIV_FREE (loop);

  inexact = mpfr_set (y, t, rnd_mode);

 end:
  mpfr_clear (t);
  MPFR_SAVE_EXPO_FREE (expo);
  return underflow ? inexact : mpfr_check_range (y, inexact, rnd_mode);
}