xref: /netbsd-src/external/lgpl3/mpfr/dist/src/tanh.c (revision ec6772edaf0cdcb5f52a48f4aca5e33a8fb8ecfd)
1 /* mpfr_tanh -- hyperbolic tangent
2 
3 Copyright 2001-2023 Free Software Foundation, Inc.
4 Contributed by the AriC and Caramba projects, INRIA.
5 
6 This file is part of the GNU MPFR Library.
7 
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.
12 
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.
17 
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 https://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
21 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
22 
23 #define MPFR_NEED_LONGLONG_H
24 #include "mpfr-impl.h"
25 
26 int
mpfr_tanh(mpfr_ptr y,mpfr_srcptr xt,mpfr_rnd_t rnd_mode)27 mpfr_tanh (mpfr_ptr y, mpfr_srcptr xt, mpfr_rnd_t rnd_mode)
28 {
29   /****** Declaration ******/
30   mpfr_t x;
31   int inexact;
32   MPFR_SAVE_EXPO_DECL (expo);
33 
34   MPFR_LOG_FUNC
35     (("x[%Pd]=%.*Rg rnd=%d", mpfr_get_prec (xt), mpfr_log_prec, xt, rnd_mode),
36      ("y[%Pd]=%.*Rg inexact=%d",
37       mpfr_get_prec (y), mpfr_log_prec, y, inexact));
38 
39   /* Special value checking */
40   if (MPFR_UNLIKELY (MPFR_IS_SINGULAR (xt)))
41     {
42       if (MPFR_IS_NAN (xt))
43         {
44           MPFR_SET_NAN (y);
45           MPFR_RET_NAN;
46         }
47       else if (MPFR_IS_INF (xt))
48         {
49           /* tanh(inf) = 1 && tanh(-inf) = -1 */
50           return mpfr_set_si (y, MPFR_INT_SIGN (xt), rnd_mode);
51         }
52       else /* tanh (0) = 0 and xt is zero */
53         {
54           MPFR_ASSERTD (MPFR_IS_ZERO(xt));
55           MPFR_SET_ZERO (y);
56           MPFR_SET_SAME_SIGN (y, xt);
57           MPFR_RET (0);
58         }
59     }
60 
61   /* tanh(x) = x - x^3/3 + ... so the error is < 2^(3*EXP(x)-1) */
62   MPFR_FAST_COMPUTE_IF_SMALL_INPUT (y, xt, -2 * MPFR_GET_EXP (xt), 1, 0,
63                                     rnd_mode, {});
64 
65   MPFR_TMP_INIT_ABS (x, xt);
66 
67   MPFR_SAVE_EXPO_MARK (expo);
68 
69   /* General case */
70   {
71     /* Declaration of the intermediary variable */
72     mpfr_t t, te;
73     mpfr_exp_t d;
74 
75     /* Declaration of the size variable */
76     mpfr_prec_t Ny = MPFR_PREC(y);   /* target precision */
77     mpfr_prec_t Nt;                  /* working precision */
78     long int err;                  /* error */
79     int sign = MPFR_SIGN (xt);
80     MPFR_ZIV_DECL (loop);
81     MPFR_GROUP_DECL (group);
82 
83     /* First check for BIG overflow of exp(2*x):
84        For x > 0, exp(2*x) > 2^(2*x)
85        If 2 ^(2*x) > 2^emax or x>emax/2, there is an overflow */
86     if (MPFR_UNLIKELY (mpfr_cmp_si (x, __gmpfr_emax/2) >= 0)) {
87       /* initialize of intermediary variables
88          since 'set_one' label assumes the variables have been
89          initialize */
90       MPFR_GROUP_INIT_2 (group, MPFR_PREC_MIN, t, te);
91       goto set_one;
92     }
93 
94     /* Compute the precision of intermediary variable */
95     /* The optimal number of bits: see algorithms.tex */
96     Nt = Ny + MPFR_INT_CEIL_LOG2 (Ny) + 4;
97     /* if x is small, there will be a cancellation in exp(2x)-1 */
98     if (MPFR_GET_EXP (x) < 0)
99       Nt += -MPFR_GET_EXP (x);
100 
101     /* The error analysis in algorithms.tex assumes that x is exactly
102        representable with working precision Nt.
103        FIXME: adapt the error analysis for the case Nt < PREC(x). */
104     if (Nt < MPFR_PREC(x))
105       Nt = MPFR_PREC(x);
106 
107     /* initialize of intermediary variable */
108     MPFR_GROUP_INIT_2 (group, Nt, t, te);
109 
110     MPFR_ZIV_INIT (loop, Nt);
111     for (;;)
112       {
113         /* tanh = (exp(2x)-1)/(exp(2x)+1) */
114         inexact = mpfr_mul_2ui (te, x, 1, MPFR_RNDN);  /* 2x */
115         MPFR_ASSERTD(inexact == 0); /* see FIXME above */
116         /* since x > 0, we can only have an overflow */
117         mpfr_exp (te, te, MPFR_RNDN);        /* exp(2x) */
118         if (MPFR_UNLIKELY (MPFR_IS_INF (te)))
119           {
120           set_one:
121             inexact = MPFR_FROM_SIGN_TO_INT (sign);
122             mpfr_set4 (y, __gmpfr_one, MPFR_RNDN, sign);
123             if (MPFR_IS_LIKE_RNDZ (rnd_mode, MPFR_IS_NEG_SIGN (sign)))
124               {
125                 inexact = -inexact;
126                 mpfr_nexttozero (y);
127               }
128             break;
129           }
130         d = MPFR_GET_EXP (te);               /* For Error calculation */
131         mpfr_add_ui (t, te, 1, MPFR_RNDD);   /* exp(2x) + 1 */
132         mpfr_sub_ui (te, te, 1, MPFR_RNDU);  /* exp(2x) - 1 */
133         d = d - MPFR_GET_EXP (te);
134         mpfr_div (t, te, t, MPFR_RNDN);      /* (exp(2x)-1)/(exp(2x)+1) */
135 
136         /* Calculation of the error, see algorithms.tex; the current value
137            of d is k in algorithms.tex. */
138         d = MAX(3, d + 1);  /* d = exponent in 2^(max(3,k+1)) */
139         err = Nt - (d + 1);
140 
141         /* The inequality is the condition max(3,k+1) <= floor(p/2). */
142         if (MPFR_LIKELY (d <= Nt / 2 &&
143                          MPFR_CAN_ROUND (t, err, Ny, rnd_mode)))
144           {
145             inexact = mpfr_set4 (y, t, rnd_mode, sign);
146             break;
147           }
148 
149         /* if t=1, we still can round since |sinh(x)| < 1 */
150         if (MPFR_GET_EXP (t) == 1)
151           goto set_one;
152 
153         /* Actualisation of the precision */
154         MPFR_ZIV_NEXT (loop, Nt);
155         MPFR_GROUP_REPREC_2 (group, Nt, t, te);
156       }
157     MPFR_ZIV_FREE (loop);
158     MPFR_GROUP_CLEAR (group);
159   }
160   MPFR_SAVE_EXPO_FREE (expo);
161   inexact = mpfr_check_range (y, inexact, rnd_mode);
162 
163   return inexact;
164 }
165 
166