~ubuntu-branches/debian/sid/genius/sid

« back to all changes in this revision

Viewing changes to mpfr/acos.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2006-08-21 12:57:45 UTC
  • Revision ID: james.westby@ubuntu.com-20060821125745-sl9ks8v7fq324bdf
Tags: upstream-0.7.6.1
ImportĀ upstreamĀ versionĀ 0.7.6.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* mpfr_acos -- arc-cosinus of a floating-point number
 
2
 
 
3
Copyright 2001, 2002, 2003, 2004, 2005 Free Software Foundation.
 
4
 
 
5
This file is part of the MPFR Library, and was contributed by Mathieu Dutour.
 
6
 
 
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.
 
11
 
 
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.
 
16
 
 
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. */
 
21
 
 
22
#include "mpfr-impl.h"
 
23
 
 
24
int
 
25
mpfr_acos (mpfr_ptr acos, mpfr_srcptr x, mp_rnd_t rnd_mode)
 
26
{
 
27
  mpfr_t xp, arcc, tmp;
 
28
  mp_exp_t supplement;
 
29
  mp_prec_t prec;
 
30
  int sign, compared, inexact;
 
31
  MPFR_SAVE_EXPO_DECL (expo);
 
32
  MPFR_ZIV_DECL (loop);
 
33
 
 
34
  MPFR_LOG_FUNC (("x[%#R]=%R rnd=%d", x, x, rnd_mode),
 
35
                 ("acos[%#R]=%R inexact=%d", acos, acos, inexact));
 
36
 
 
37
  /* Singular cases */
 
38
  if (MPFR_UNLIKELY (MPFR_IS_SINGULAR (x)))
 
39
    {
 
40
      if (MPFR_IS_NAN (x) || MPFR_IS_INF (x))
 
41
        {
 
42
          MPFR_SET_NAN (acos);
 
43
          MPFR_RET_NAN;
 
44
        }
 
45
      else /* necessarily x=0 */
 
46
        {
 
47
          MPFR_ASSERTD(MPFR_IS_ZERO(x));
 
48
          /* acos(0)=Pi/2 */
 
49
          inexact = mpfr_const_pi (acos, rnd_mode);
 
50
          mpfr_div_2ui (acos, acos, 1, rnd_mode); /* exact */
 
51
          MPFR_RET (inexact);
 
52
        }
 
53
    }
 
54
 
 
55
  /* Set x_p=|x| */
 
56
  sign = MPFR_SIGN (x);
 
57
  mpfr_init2 (xp, MPFR_PREC (x));
 
58
  mpfr_abs (xp, x, GMP_RNDN); /* Exact */
 
59
 
 
60
  compared = mpfr_cmp_ui (xp, 1);
 
61
 
 
62
  if (MPFR_UNLIKELY (compared >= 0))
 
63
    {
 
64
      mpfr_clear (xp);
 
65
      if (compared > 0) /* acos(x) = NaN for x > 1 */
 
66
        {
 
67
          MPFR_SET_NAN(acos);
 
68
          MPFR_RET_NAN;
 
69
        }
 
70
      else
 
71
        {
 
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);
 
76
        }
 
77
    }
 
78
 
 
79
  MPFR_SAVE_EXPO_MARK (expo);
 
80
 
 
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);
 
85
  else
 
86
    supplement = 2 - MPFR_GET_EXP (xp);
 
87
  mpfr_clear (xp);
 
88
 
 
89
  prec = MPFR_PREC (acos) + 10 + supplement;
 
90
 
 
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;
 
96
 
 
97
  mpfr_init2 (tmp, prec);
 
98
  mpfr_init2 (arcc, prec);
 
99
 
 
100
  MPFR_ZIV_INIT (loop, prec);
 
101
  for (;;)
 
102
    {
 
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);
 
112
 
 
113
      if (MPFR_LIKELY (MPFR_CAN_ROUND (arcc, prec-supplement,
 
114
                                       MPFR_PREC (acos), rnd_mode)))
 
115
        break;
 
116
      MPFR_ZIV_NEXT (loop, prec);
 
117
      mpfr_set_prec (tmp, prec);
 
118
      mpfr_set_prec (arcc, prec);
 
119
    }
 
120
  MPFR_ZIV_FREE (loop);
 
121
 
 
122
  inexact = mpfr_set (acos, arcc, rnd_mode);
 
123
  mpfr_clear (tmp);
 
124
  mpfr_clear (arcc);
 
125
 
 
126
  MPFR_SAVE_EXPO_FREE (expo);
 
127
  return mpfr_check_range (acos, inexact, rnd_mode);
 
128
}