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

Annotation of OpenXM_contrib/gmp/mpfr/log2.c, Revision 1.1.1.2

1.1.1.2 ! ohara       1: /* mpfr_log2 -- log base 2
1.1       maekawa     2:
1.1.1.2 ! ohara       3: Copyright 2001, 2002 Free Software Foundation, Inc.
1.1       maekawa     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
1.1.1.2 ! ohara       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
1.1       maekawa    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
1.1.1.2 ! ohara      14: or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
1.1       maekawa    15: License for more details.
                     16:
1.1.1.2 ! ohara      17: You should have received a copy of the GNU Lesser General Public License
1.1       maekawa    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"
1.1.1.2 ! ohara      26: #include "mpfr-impl.h"
1.1       maekawa    27:
1.1.1.2 ! ohara      28:  /* The computation of r=log2(a)
1.1       maekawa    29:
1.1.1.2 ! ohara      30:     r=log2(a)=log(a)/log(2)
        !            31:  */
1.1       maekawa    32:
1.1.1.2 ! ohara      33: int
        !            34: mpfr_log2 (mpfr_ptr r, mpfr_srcptr a, mp_rnd_t rnd_mode)
1.1       maekawa    35: {
1.1.1.2 ! ohara      36:   int inexact = 0;
1.1       maekawa    37:
1.1.1.2 ! ohara      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) /* log(-Inf) = NaN */
        !            51:         {
        !            52:           MPFR_SET_NAN(r);
        !            53:           MPFR_RET_NAN;
        !            54:         }
        !            55:       else /* log(+Inf) = +Inf */
        !            56:         {
        !            57:           MPFR_SET_INF(r);
        !            58:           MPFR_SET_POS(r);
        !            59:           MPFR_RET(0);
        !            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); /* log2(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); /* only "normal" case where the result is exact */
        !            86:     }
        !            87:
        !            88:   /* If a is integer, log2(a) is exact*/
        !            89:   if (mpfr_cmp_ui_2exp(a,1,MPFR_EXP(a)-1) == 0)
        !            90:       return mpfr_set_si(r,MPFR_EXP(a)-1,rnd_mode);
        !            91:
        !            92:
        !            93:  /* General case */
        !            94:   {
        !            95:     /* Declaration of the intermediary variable */
        !            96:     mpfr_t t, tt;
        !            97:
        !            98:     /* Declaration of the size variable */
        !            99:     mp_prec_t Nx = MPFR_PREC(a);   /* Precision of input variable */
        !           100:     mp_prec_t Ny = MPFR_PREC(r);   /* Precision of input variable */
        !           101:
        !           102:     mp_prec_t Nt;   /* Precision of the intermediary variable */
        !           103:     long int err;  /* Precision of error */
        !           104:
        !           105:
        !           106:     /* compute the precision of intermediary variable */
        !           107:     Nt=MAX(Nx,Ny);
        !           108:     /* the optimal number of bits : see algorithms.ps */
        !           109:     Nt=Nt+3+_mpfr_ceil_log2(Nt);
        !           110:
        !           111:     /* initialise of intermediary      variable */
        !           112:     mpfr_init(t);
        !           113:     mpfr_init(tt);
        !           114:
        !           115:
        !           116:     /* First computation of log2 */
        !           117:     do {
        !           118:
        !           119:       /* reactualisation of the precision */
        !           120:       mpfr_set_prec(t,Nt);
        !           121:       mpfr_set_prec(tt,Nt);
        !           122:
        !           123:       /* compute log2 */
        !           124:       mpfr_const_log2(t,GMP_RNDD); /* log(2) */
        !           125:       mpfr_log(tt,a,GMP_RNDN);     /* log(a) */
        !           126:       mpfr_div(t,tt,t,GMP_RNDN); /* log(a)/log(2) */
        !           127:
        !           128:
        !           129:       /* estimation of the error */
        !           130:       err=Nt-3;
        !           131:
        !           132:       /* actualisation of the precision */
        !           133:       Nt += 10;
        !           134:     } while ((err<0) || !mpfr_can_round(t,err,GMP_RNDN,rnd_mode,Ny));
        !           135:
        !           136:       inexact = mpfr_set(r,t,rnd_mode);
1.1       maekawa   137:
1.1.1.2 ! ohara     138:       mpfr_clear(t);
        !           139:       mpfr_clear(tt);
1.1       maekawa   140:   }
1.1.1.2 ! ohara     141:   return inexact;
1.1       maekawa   142: }

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