1 /* mpfr_asinh -- inverse hyperbolic sine
3 Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
4 Contributed by the Arenaire and Cacao 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 2.1 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.LIB. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
21 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
, mp_rnd_t rnd_mode
)
35 mpfr_t t
; /* auxiliary variables */
37 MPFR_SAVE_EXPO_DECL (expo
);
40 MPFR_LOG_FUNC (("x[%#R]=%R rnd=%d", x
, x
, rnd_mode
),
41 ("y[%#R]=%R inexact=%d", y
, y
, inexact
));
43 if (MPFR_UNLIKELY (MPFR_IS_SINGULAR (x
)))
50 else if (MPFR_IS_INF (x
))
53 MPFR_SET_SAME_SIGN (y
, x
);
56 else /* x is necessarily 0 */
58 MPFR_ASSERTD (MPFR_IS_ZERO (x
));
59 MPFR_SET_ZERO (y
); /* asinh(0) = 0 */
60 MPFR_SET_SAME_SIGN (y
, x
);
65 /* asinh(x) = x - x^3/6 + ... so the error is < 2^(3*EXP(x)-2) */
66 MPFR_FAST_COMPUTE_IF_SMALL_INPUT (y
, x
, -2 * MPFR_GET_EXP (x
), 2, 0,
69 Ny
= MPFR_PREC (y
); /* Precision of output variable */
71 signx
= MPFR_SIGN (x
);
72 neg
= MPFR_IS_NEG (x
);
76 /* compute the precision of intermediary variable */
77 /* the optimal number of bits : see algorithms.tex */
78 Nt
= Ny
+ 4 + MPFR_INT_CEIL_LOG2 (Ny
);
80 MPFR_SAVE_EXPO_MARK (expo
);
82 /* initialize intermediary variables */
85 /* First computation of asinh */
86 MPFR_ZIV_INIT (loop
, Nt
);
90 mpfr_mul (t
, x
, x
, GMP_RNDD
); /* x^2 */
91 mpfr_add_ui (t
, t
, 1, GMP_RNDD
); /* x^2+1 */
92 mpfr_sqrt (t
, t
, GMP_RNDN
); /* sqrt(x^2+1) */
93 (neg
? mpfr_sub
: mpfr_add
) (t
, t
, x
, GMP_RNDN
); /* sqrt(x^2+1)+x */
94 mpfr_log (t
, t
, GMP_RNDN
); /* ln(sqrt(x^2+1)+x)*/
96 if (MPFR_LIKELY (MPFR_IS_PURE_FP (t
)))
98 /* error estimate -- see algorithms.tex */
99 err
= Nt
- (MAX (4 - MPFR_GET_EXP (t
), 0) + 1);
100 if (MPFR_LIKELY (MPFR_IS_ZERO (t
)
101 || MPFR_CAN_ROUND (t
, err
, Ny
, rnd_mode
)))
105 /* actualisation of the precision */
106 MPFR_ZIV_NEXT (loop
, Nt
);
107 mpfr_set_prec (t
, Nt
);
109 MPFR_ZIV_FREE (loop
);
111 inexact
= mpfr_set4 (y
, t
, rnd_mode
, signx
);
115 MPFR_SAVE_EXPO_FREE (expo
);
116 return mpfr_check_range (y
, inexact
, rnd_mode
);