[BACK]Return to log10.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib / gmp / mpfr

Annotation of OpenXM_contrib/gmp/mpfr/log10.c, Revision 1.1.1.1

1.1       ohara       1: /* mpfr_log10 -- logarithm in base 10.
                      2:
                      3: Copyright 2001, 2002 Free Software Foundation, Inc.
                      4:
                      5: This file is part of the MPFR Library.
                      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., 59 Temple Place - Suite 330, Boston,
                     20: MA 02111-1307, USA. */
                     21:
                     22: #include <stdio.h>
                     23: #include "gmp.h"
                     24: #include "gmp-impl.h"
                     25: #include "mpfr.h"
                     26: #include "mpfr-impl.h"
                     27:
                     28:  /* The computation of r=log10(a)
                     29:
                     30:     r=log10(a)=log(a)/log(10)
                     31:  */
                     32:
                     33: int
                     34: mpfr_log10 (mpfr_ptr r, mpfr_srcptr a, mp_rnd_t rnd_mode)
                     35: {
                     36:   int inexact = 0;
                     37:
                     38:   /* If a is NaN, the result is NaN */
                     39:   if (MPFR_IS_NAN(a))
                     40:     {
                     41:       MPFR_SET_NAN(r);
                     42:       MPFR_RET_NAN;
                     43:     }
                     44:
                     45:   MPFR_CLEAR_NAN(r);
                     46:
                     47:   /* check for infinity before zero */
                     48:   if (MPFR_IS_INF(a))
                     49:     {
                     50:       if (MPFR_SIGN(a) < 0) /* log10(-Inf) = NaN */
                     51:         {
                     52:           MPFR_SET_NAN(r);
                     53:           MPFR_RET_NAN;
                     54:         }
                     55:       else /* log10(+Inf) = +Inf */
                     56:         {
                     57:           MPFR_SET_INF(r);
                     58:           MPFR_SET_POS(r);
                     59:           MPFR_RET(0); /* exact */
                     60:         }
                     61:     }
                     62:
                     63:   /* Now we can clear the flags without damage even if r == a */
                     64:   MPFR_CLEAR_INF(r);
                     65:
                     66:   if (MPFR_IS_ZERO(a))
                     67:     {
                     68:       MPFR_SET_INF(r);
                     69:       MPFR_SET_NEG(r);
                     70:       MPFR_RET(0); /* log10(0) is an exact -infinity */
                     71:     }
                     72:
                     73:   /* If a is negative, the result is NaN */
                     74:   if (MPFR_SIGN(a) < 0)
                     75:     {
                     76:       MPFR_SET_NAN(r);
                     77:       MPFR_RET_NAN;
                     78:     }
                     79:
                     80:   /* If a is 1, the result is 0 */
                     81:   if (mpfr_cmp_ui (a, 1) == 0)
                     82:     {
                     83:       MPFR_SET_ZERO(r);
                     84:       MPFR_SET_POS(r);
                     85:       MPFR_RET(0); /* result is exact */
                     86:     }
                     87:
                     88:   /* General case */
                     89:   {
                     90:     /* Declaration of the intermediary variable */
                     91:     mpfr_t t, tt;
                     92:     int ok;
                     93:
                     94:     /* Declaration of the size variable */
                     95:     mp_prec_t Nx = MPFR_PREC(a);   /* Precision of input variable */
                     96:     mp_prec_t Ny = MPFR_PREC(r);   /* Precision of output variable */
                     97:
                     98:     mp_prec_t Nt;   /* Precision of the intermediary variable */
                     99:     long int err;  /* Precision of error */
                    100:
                    101:     /* compute the precision of intermediary variable */
                    102:     Nt = MAX(Nx, Ny);
                    103:     /* the optimal number of bits : see algorithms.ps */
                    104:     Nt = Nt + 4+ _mpfr_ceil_log2 (Nt);
                    105:
                    106:     /* initialise of intermediary variables */
                    107:     mpfr_init (t);
                    108:     mpfr_init (tt);
                    109:
                    110:
                    111:     /* First computation of log10 */
                    112:     do {
                    113:
                    114:       /* reactualisation of the precision */
                    115:       mpfr_set_prec (t, Nt);
                    116:       mpfr_set_prec (tt, Nt);
                    117:
                    118:       /* compute log10 */
                    119:       mpfr_set_ui (t, 10, GMP_RNDN);   /* 10 */
                    120:       mpfr_log (t, t, GMP_RNDD);       /* log(10) */
                    121:       mpfr_log (tt, a, GMP_RNDN);      /* log(a) */
                    122:       mpfr_div (t, tt, t, GMP_RNDN);   /* log(a)/log(10) */
                    123:
                    124:       /* estimation of the error */
                    125:       err = Nt - 4;
                    126:
                    127:       ok = mpfr_can_round (t, err, GMP_RNDN, rnd_mode, Ny);
                    128:
                    129:       /* log10(10^n) is exact */
                    130:       if ((MPFR_SIGN(t) > 0) && mpfr_isinteger(t))
                    131:         if (mpfr_ui_pow_ui (tt, 10, (unsigned long int) mpfr_get_d1 (t) + 0.5,
                    132:                             GMP_RNDN) == 0)
                    133:           if (mpfr_cmp (a, tt) == 0)
                    134:             ok = 1;
                    135:
                    136:       /* actualisation of the precision */
                    137:       Nt += 10;
                    138:     } while ((err < 0) || !ok);
                    139:
                    140:     inexact = mpfr_set (r, t, rnd_mode);
                    141:
                    142:     mpfr_clear (t);
                    143:     mpfr_clear (tt);
                    144:   }
                    145:
                    146:   return inexact;
                    147: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>