1
/* mpfr_acos -- arc-cosinus of a floating-point number
3
Copyright 2001, 2002, 2003, 2004, 2005 Free Software Foundation.
5
This file is part of the MPFR Library, and was contributed by Mathieu Dutour.
7
The MPFR Library is free software; you can redistribute it and/or modify
8
it under the terms of the GNU Lesser General Public License as published by
9
the Free Software Foundation; either version 2.1 of the License, or (at your
10
option) any later version.
12
The MPFR Library is distributed in the hope that it will be useful, but
13
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15
License for more details.
17
You should have received a copy of the GNU Lesser General Public License
18
along with the MPFR Library; see the file COPYING.LIB. If not, write to
19
the Free Software Foundation, Inc., 51 Franklin Place, Fifth Floor, Boston,
20
MA 02110-1301, USA. */
22
#include "mpfr-impl.h"
25
mpfr_acos (mpfr_ptr acos, mpfr_srcptr x, mp_rnd_t rnd_mode)
30
int sign, compared, inexact;
31
MPFR_SAVE_EXPO_DECL (expo);
34
MPFR_LOG_FUNC (("x[%#R]=%R rnd=%d", x, x, rnd_mode),
35
("acos[%#R]=%R inexact=%d", acos, acos, inexact));
38
if (MPFR_UNLIKELY (MPFR_IS_SINGULAR (x)))
40
if (MPFR_IS_NAN (x) || MPFR_IS_INF (x))
45
else /* necessarily x=0 */
47
MPFR_ASSERTD(MPFR_IS_ZERO(x));
49
inexact = mpfr_const_pi (acos, rnd_mode);
50
mpfr_div_2ui (acos, acos, 1, rnd_mode); /* exact */
57
mpfr_init2 (xp, MPFR_PREC (x));
58
mpfr_abs (xp, x, GMP_RNDN); /* Exact */
60
compared = mpfr_cmp_ui (xp, 1);
62
if (MPFR_UNLIKELY (compared >= 0))
65
if (compared > 0) /* acos(x) = NaN for x > 1 */
72
if (MPFR_IS_POS_SIGN (sign)) /* acos(+1) = 0 */
73
return mpfr_set_ui (acos, 0, rnd_mode);
74
else /* acos(-1) = Pi */
75
return mpfr_const_pi (acos, rnd_mode);
79
MPFR_SAVE_EXPO_MARK (expo);
81
/* Compute the supplement */
82
mpfr_ui_sub (xp, 1, xp, GMP_RNDD);
83
if (MPFR_IS_POS_SIGN (sign))
84
supplement = 2 - 2 * MPFR_GET_EXP (xp);
86
supplement = 2 - MPFR_GET_EXP (xp);
89
prec = MPFR_PREC (acos) + 10 + supplement;
91
/* If x ~ 2^-N, acos(x) ~ PI/2 - x - x^3/6
92
If Prec < 2*N, we can't round since x^3/6 won't be counted. */
93
if (MPFR_PREC (acos) >= MPFR_PREC (x)
94
&& (mp_exp_t) prec <= -2*MPFR_GET_EXP (x) + 5)
95
prec = (mpfr_uexp_t) (-2*MPFR_GET_EXP (x)) + 5;
97
mpfr_init2 (tmp, prec);
98
mpfr_init2 (arcc, prec);
100
MPFR_ZIV_INIT (loop, prec);
103
/* acos(x) = Pi/2 - asin(x) = Pi/2 - atan(x/sqrt(1-x^2)) */
104
mpfr_sqr (tmp, x, GMP_RNDN);
105
mpfr_ui_sub (tmp, 1, tmp, GMP_RNDN);
106
mpfr_sqrt (tmp, tmp, GMP_RNDN);
107
mpfr_div (tmp, x, tmp, GMP_RNDN);
108
mpfr_atan (arcc, tmp, GMP_RNDN);
109
mpfr_const_pi (tmp, GMP_RNDN);
110
mpfr_div_2ui (tmp, tmp, 1, GMP_RNDN);
111
mpfr_sub (arcc, tmp, arcc, GMP_RNDN);
113
if (MPFR_LIKELY (MPFR_CAN_ROUND (arcc, prec-supplement,
114
MPFR_PREC (acos), rnd_mode)))
116
MPFR_ZIV_NEXT (loop, prec);
117
mpfr_set_prec (tmp, prec);
118
mpfr_set_prec (arcc, prec);
120
MPFR_ZIV_FREE (loop);
122
inexact = mpfr_set (acos, arcc, rnd_mode);
126
MPFR_SAVE_EXPO_FREE (expo);
127
return mpfr_check_range (acos, inexact, rnd_mode);