Upgrade MPFR from 2.4.1 to 2.4.2-p3 on the vendor branch.
[dragonfly.git] / contrib / mpfr / log10.c
1 /* mpfr_log10 -- logarithm in base 10.
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 r=log10(a)
27
28     r=log10(a)=log(a)/log(10)
29  */
30
31 int
32 mpfr_log10 (mpfr_ptr r, mpfr_srcptr a, mp_rnd_t rnd_mode)
33 {
34   int inexact;
35   MPFR_SAVE_EXPO_DECL (expo);
36
37   /* If a is NaN, the result is NaN */
38   if (MPFR_UNLIKELY (MPFR_IS_SINGULAR (a)))
39     {
40       if (MPFR_IS_NAN (a))
41         {
42           MPFR_SET_NAN (r);
43           MPFR_RET_NAN;
44         }
45       /* check for infinity before zero */
46       else if (MPFR_IS_INF (a))
47         {
48           if (MPFR_IS_NEG (a))
49             /* log10(-Inf) = NaN */
50             {
51               MPFR_SET_NAN (r);
52               MPFR_RET_NAN;
53             }
54           else /* log10(+Inf) = +Inf */
55             {
56               MPFR_SET_INF (r);
57               MPFR_SET_POS (r);
58               MPFR_RET (0); /* exact */
59             }
60         }
61       else /* a = 0 */
62         {
63           MPFR_ASSERTD (MPFR_IS_ZERO (a));
64           MPFR_SET_INF (r);
65           MPFR_SET_NEG (r);
66           MPFR_RET (0); /* log10(0) is an exact -infinity */
67         }
68     }
69
70   /* If a is negative, the result is NaN */
71   if (MPFR_UNLIKELY (MPFR_IS_NEG (a)))
72     {
73       MPFR_SET_NAN (r);
74       MPFR_RET_NAN;
75     }
76
77   /* If a is 1, the result is 0 */
78   if (mpfr_cmp_ui (a, 1) == 0)
79     {
80       MPFR_SET_ZERO (r);
81       MPFR_SET_POS (r);
82       MPFR_RET (0); /* result is exact */
83     }
84
85   MPFR_SAVE_EXPO_MARK (expo);
86
87   /* General case */
88   {
89     /* Declaration of the intermediary variable */
90     mpfr_t t, tt;
91     MPFR_ZIV_DECL (loop);
92     /* Declaration of the size variable */
93     mp_prec_t Ny = MPFR_PREC(r);   /* Precision of output variable */
94     mp_prec_t Nt;        /* Precision of the intermediary variable */
95     mp_exp_t  err;                           /* Precision of error */
96
97     /* compute the precision of intermediary variable */
98     /* the optimal number of bits : see algorithms.tex */
99     Nt = Ny + 4 + MPFR_INT_CEIL_LOG2 (Ny);
100
101     /* initialise of intermediary variables */
102     mpfr_init2 (t, Nt);
103     mpfr_init2 (tt, Nt);
104
105     /* First computation of log10 */
106     MPFR_ZIV_INIT (loop, Nt);
107     for (;;)
108       {
109         /* compute log10 */
110         mpfr_set_ui (t, 10, GMP_RNDN);   /* 10 */
111         mpfr_log (t, t, GMP_RNDD);       /* log(10) */
112         mpfr_log (tt, a, GMP_RNDN);      /* log(a) */
113         mpfr_div (t, tt, t, GMP_RNDN);   /* log(a)/log(10) */
114
115         /* estimation of the error */
116         err = Nt - 4;
117         if (MPFR_LIKELY (MPFR_CAN_ROUND (t, err, Ny, rnd_mode)))
118           break;
119
120         /* log10(10^n) is exact:
121            FIXME: Can we have 10^n exactly representable as a mpfr_t
122            but n can't fit an unsigned long? */
123         if (MPFR_IS_POS (t)
124             && mpfr_integer_p (t) && mpfr_fits_ulong_p (t, GMP_RNDN)
125             && !mpfr_ui_pow_ui (tt, 10, mpfr_get_ui (t, GMP_RNDN), GMP_RNDN)
126             && mpfr_cmp (a, tt) == 0)
127           break;
128
129         /* actualisation of the precision */
130         MPFR_ZIV_NEXT (loop, Nt);
131         mpfr_set_prec (t, Nt);
132         mpfr_set_prec (tt, Nt);
133       }
134     MPFR_ZIV_FREE (loop);
135
136     inexact = mpfr_set (r, t, rnd_mode);
137
138     mpfr_clear (t);
139     mpfr_clear (tt);
140   }
141
142   MPFR_SAVE_EXPO_FREE (expo);
143   return mpfr_check_range (r, inexact, rnd_mode);
144 }