Merge branch 'vendor/GCC44'
[dragonfly.git] / contrib / mpfr / atanh.c
1 /* mpfr_atanh -- Inverse Hyperbolic Tangente
2
3 Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
4 Contributed by the Arenaire and Cacao 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 2.1 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.LIB.  If not, write to
20 the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
21 MA 02110-1301, USA. */
22
23 #define MPFR_NEED_LONGLONG_H
24 #include "mpfr-impl.h"
25
26  /* The computation of atanh is done by
27        atanh= 1/2*ln(x+1)-1/2*ln(1-x)   */
28
29 int
30 mpfr_atanh (mpfr_ptr y, mpfr_srcptr xt , mp_rnd_t rnd_mode)
31 {
32   int inexact;
33   mpfr_t x, t, te;
34   mp_prec_t Nx, Ny, Nt;
35   mp_exp_t err;
36   MPFR_ZIV_DECL (loop);
37   MPFR_SAVE_EXPO_DECL (expo);
38
39   MPFR_LOG_FUNC (("x[%#R]=%R rnd=%d", xt, xt, rnd_mode),
40                  ("y[%#R]=%R inexact=%d", y, y, inexact));
41
42   /* Special cases */
43   if (MPFR_UNLIKELY (MPFR_IS_SINGULAR (xt)))
44     {
45       /* atanh(NaN) = NaN, and atanh(+/-Inf) = NaN since tanh gives a result
46          between -1 and 1 */
47       if (MPFR_IS_NAN (xt) || MPFR_IS_INF (xt))
48         {
49           MPFR_SET_NAN (y);
50           MPFR_RET_NAN;
51         }
52       else /* necessarily xt is 0 */
53         {
54           MPFR_ASSERTD (MPFR_IS_ZERO (xt));
55           MPFR_SET_ZERO (y);   /* atanh(0) = 0 */
56           MPFR_SET_SAME_SIGN (y,xt);
57           MPFR_RET (0);
58         }
59     }
60
61   /* atanh (x) = NaN as soon as |x| > 1, and arctanh(+/-1) = +/-Inf */
62   if (MPFR_UNLIKELY (MPFR_EXP (xt) > 0))
63     {
64       if (MPFR_EXP (xt) == 1 && mpfr_powerof2_raw (xt))
65         {
66           MPFR_SET_INF (y);
67           MPFR_SET_SAME_SIGN (y, xt);
68           MPFR_RET (0);
69         }
70       MPFR_SET_NAN (y);
71       MPFR_RET_NAN;
72     }
73
74   /* atanh(x) = x + x^3/3 + ... so the error is < 2^(3*EXP(x)-1) */
75   MPFR_FAST_COMPUTE_IF_SMALL_INPUT (y, xt, -2 * MPFR_GET_EXP (xt), 1, 1,
76                                     rnd_mode, {});
77
78   MPFR_SAVE_EXPO_MARK (expo);
79
80   /* Compute initial precision */
81   Nx = MPFR_PREC (xt);
82   MPFR_TMP_INIT_ABS (x, xt);
83   Ny = MPFR_PREC (y);
84   Nt = MAX (Nx, Ny);
85   /* the optimal number of bits : see algorithms.ps */
86   Nt = Nt + MPFR_INT_CEIL_LOG2 (Nt) + 4;
87
88   /* initialise of intermediary variable */
89   mpfr_init2 (t, Nt);
90   mpfr_init2 (te, Nt);
91
92   /* First computation of cosh */
93   MPFR_ZIV_INIT (loop, Nt);
94   for (;;)
95     {
96       /* compute atanh */
97       mpfr_ui_sub (te, 1, x, GMP_RNDU);   /* (1-xt)*/
98       mpfr_add_ui (t,  x, 1, GMP_RNDD);   /* (xt+1)*/
99       mpfr_div (t, t, te, GMP_RNDN);      /* (1+xt)/(1-xt)*/
100       mpfr_log (t, t, GMP_RNDN);          /* ln((1+xt)/(1-xt))*/
101       mpfr_div_2ui (t, t, 1, GMP_RNDN);   /* (1/2)*ln((1+xt)/(1-xt))*/
102
103       /* error estimate: see algorithms.tex */
104       /* FIXME: this does not correspond to the value in algorithms.tex!!! */
105       /* err=Nt-__gmpfr_ceil_log2(1+5*pow(2,1-MPFR_EXP(t)));*/
106       err = Nt - (MAX (4 - MPFR_GET_EXP (t), 0) + 1);
107
108       if (MPFR_LIKELY (MPFR_IS_ZERO (t)
109                        || MPFR_CAN_ROUND (t, err, Ny, rnd_mode)))
110         break;
111
112       /* reactualisation of the precision */
113       MPFR_ZIV_NEXT (loop, Nt);
114       mpfr_set_prec (t, Nt);
115       mpfr_set_prec (te, Nt);
116     }
117   MPFR_ZIV_FREE (loop);
118
119   inexact = mpfr_set4 (y, t, rnd_mode, MPFR_SIGN (xt));
120
121   mpfr_clear(t);
122   mpfr_clear(te);
123
124   MPFR_SAVE_EXPO_FREE (expo);
125   return mpfr_check_range (y, inexact, rnd_mode);
126 }
127