1 /* mpfr_asinh -- inverse hyperbolic sine
3 Copyright 2001-2016 Free Software Foundation, Inc.
4 Contributed by the AriC and Caramba 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 /* The computation of asinh is done by *
27 * asinh = ln(x + sqrt(x^2 + 1)) */
30 mpfr_asinh (mpfr_ptr y
, mpfr_srcptr x
, mpfr_rnd_t rnd_mode
)
35 mpfr_t t
; /* auxiliary variables */
37 MPFR_SAVE_EXPO_DECL (expo
);
41 ("x[%Pu]=%.*Rg rnd=%d", mpfr_get_prec (x
), mpfr_log_prec
, x
, rnd_mode
),
42 ("y[%Pu]=%.*Rg inexact=%d", mpfr_get_prec (y
), mpfr_log_prec
, y
,
45 if (MPFR_UNLIKELY (MPFR_IS_SINGULAR (x
)))
52 else if (MPFR_IS_INF (x
))
55 MPFR_SET_SAME_SIGN (y
, x
);
58 else /* x is necessarily 0 */
60 MPFR_ASSERTD (MPFR_IS_ZERO (x
));
61 MPFR_SET_ZERO (y
); /* asinh(0) = 0 */
62 MPFR_SET_SAME_SIGN (y
, x
);
67 /* asinh(x) = x - x^3/6 + ... so the error is < 2^(3*EXP(x)-2) */
68 MPFR_FAST_COMPUTE_IF_SMALL_INPUT (y
, x
, -2 * MPFR_GET_EXP (x
), 2, 0,
71 Ny
= MPFR_PREC (y
); /* Precision of output variable */
73 signx
= MPFR_SIGN (x
);
74 neg
= MPFR_IS_NEG (x
);
78 /* compute the precision of intermediary variable */
79 /* the optimal number of bits : see algorithms.tex */
80 Nt
= Ny
+ 4 + MPFR_INT_CEIL_LOG2 (Ny
);
82 MPFR_SAVE_EXPO_MARK (expo
);
84 /* initialize intermediary variables */
87 /* First computation of asinh */
88 MPFR_ZIV_INIT (loop
, Nt
);
92 mpfr_mul (t
, x
, x
, MPFR_RNDD
); /* x^2 */
93 mpfr_add_ui (t
, t
, 1, MPFR_RNDD
); /* x^2+1 */
94 mpfr_sqrt (t
, t
, MPFR_RNDN
); /* sqrt(x^2+1) */
95 (neg
? mpfr_sub
: mpfr_add
) (t
, t
, x
, MPFR_RNDN
); /* sqrt(x^2+1)+x */
96 mpfr_log (t
, t
, MPFR_RNDN
); /* ln(sqrt(x^2+1)+x)*/
98 if (MPFR_LIKELY (MPFR_IS_PURE_FP (t
)))
100 /* error estimate -- see algorithms.tex */
101 err
= Nt
- (MAX (4 - MPFR_GET_EXP (t
), 0) + 1);
102 if (MPFR_LIKELY (MPFR_IS_ZERO (t
)
103 || MPFR_CAN_ROUND (t
, err
, Ny
, rnd_mode
)))
107 /* actualisation of the precision */
108 MPFR_ZIV_NEXT (loop
, Nt
);
109 mpfr_set_prec (t
, Nt
);
111 MPFR_ZIV_FREE (loop
);
113 inexact
= mpfr_set4 (y
, t
, rnd_mode
, signx
);
117 MPFR_SAVE_EXPO_FREE (expo
);
118 return mpfr_check_range (y
, inexact
, rnd_mode
);