=================================================================== RCS file: /home/cvs/OpenXM_contrib/gmp/mpfr/Attic/log2.c,v retrieving revision 1.1 retrieving revision 1.1.1.2 diff -u -p -r1.1 -r1.1.1.2 --- OpenXM_contrib/gmp/mpfr/Attic/log2.c 2000/09/09 14:12:19 1.1 +++ OpenXM_contrib/gmp/mpfr/Attic/log2.c 2003/08/25 16:06:08 1.1.1.2 @@ -1,102 +1,142 @@ -/* mpfr_log2 -- compute natural logarithm of 2 +/* mpfr_log2 -- log base 2 -Copyright (C) 1999 PolKA project, Inria Lorraine and Loria +Copyright 2001, 2002 Free Software Foundation, Inc. This file is part of the MPFR Library. The MPFR Library is free software; you can redistribute it and/or modify -it under the terms of the GNU Library General Public License as published by -the Free Software Foundation; either version 2 of the License, or (at your +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. The MPFR Library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public +or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. -You should have received a copy of the GNU Library General Public License +You should have received a copy of the GNU Lesser General Public License along with the MPFR Library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include -#include #include "gmp.h" #include "gmp-impl.h" -#include "longlong.h" #include "mpfr.h" +#include "mpfr-impl.h" -mpfr_t __mpfr_log2; /* stored value of log(2) with rnd_mode=GMP_RNDZ */ -int __mpfr_log2_prec=0; /* precision of stored value */ + /* The computation of r=log2(a) -/* set x to log(2) rounded to precision PREC(x) with direction rnd_mode + r=log2(a)=log(a)/log(2) + */ - use formula log(2) = sum(1/k/2^k, k=1..infinity) +int +mpfr_log2 (mpfr_ptr r, mpfr_srcptr a, mp_rnd_t rnd_mode) +{ + int inexact = 0; - whence 2^N*log(2) = S(N) + R(N) + /* If a is NaN, the result is NaN */ + if (MPFR_IS_NAN(a)) + { + MPFR_SET_NAN(r); + MPFR_RET_NAN; + } - where S(N) = sum(2^(N-k)/k, k=1..N-1) - and R(N) = sum(1/k/2^(k-N), k=N..infinity) < 2/N + MPFR_CLEAR_NAN(r); - Let S'(N) = sum(floor(2^(N-k)/k), k=1..N-1) + /* check for infinity before zero */ + if (MPFR_IS_INF(a)) + { + if (MPFR_SIGN(a) < 0) /* log(-Inf) = NaN */ + { + MPFR_SET_NAN(r); + MPFR_RET_NAN; + } + else /* log(+Inf) = +Inf */ + { + MPFR_SET_INF(r); + MPFR_SET_POS(r); + MPFR_RET(0); + } + } - Then 2^N*log(2)-S'(N) <= N-1+2/N <= N for N>=2. -*/ -void -#if __STDC__ -mpfr_log2(mpfr_ptr x, unsigned char rnd_mode) -#else -mpfr_log2(x, rnd_mode) mpfr_ptr x; unsigned char rnd_mode; -#endif -{ - int N, oldN, k, precx; mpz_t s, t, u; + /* Now we can clear the flags without damage even if r == a */ + MPFR_CLEAR_INF(r); - precx = PREC(x); + if (MPFR_IS_ZERO(a)) + { + MPFR_SET_INF(r); + MPFR_SET_NEG(r); + MPFR_RET(0); /* log2(0) is an exact -infinity */ + } - /* has stored value enough precision ? */ - if (precx <= __mpfr_log2_prec) { - if (rnd_mode==GMP_RNDZ || rnd_mode==GMP_RNDD || - mpfr_can_round(__mpfr_log2, __mpfr_log2_prec, GMP_RNDZ, rnd_mode, precx)) - { - mpfr_set(x, __mpfr_log2, rnd_mode); return; - } - } + /* If a is negative, the result is NaN */ + if (MPFR_SIGN(a) < 0) + { + MPFR_SET_NAN(r); + MPFR_RET_NAN; + } - /* need to recompute */ - N=2; - do { - oldN = N; - N = precx + (int)ceil(log((double)N)/log(2.0)); - } while (N != oldN); - mpz_init_set_ui(s,0); - mpz_init(u); - mpz_init_set_ui(t,1); -#if 0 - /* use log(2) = sum(1/k/2^k, k=1..infinity) */ - mpz_mul_2exp(t, t, N); - for (k=1;k