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

Annotation of OpenXM_contrib/gmp/mpfr/log1p.c, Revision 1.1

1.1     ! ohara       1: /* mpfr_log1p -- Compute log(1+x)
        !             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 "gmp.h"
        !            23: #include "gmp-impl.h"
        !            24: #include "mpfr.h"
        !            25: #include "mpfr-impl.h"
        !            26:
        !            27:  /* The computation of log1p is done by
        !            28:
        !            29:     log1p(x)=log(1+x)
        !            30:  */
        !            31:
        !            32: int
        !            33: mpfr_log1p (mpfr_ptr y, mpfr_srcptr x, mp_rnd_t rnd_mode)
        !            34: {
        !            35:   int comp, inexact = 0;
        !            36:
        !            37:   if (MPFR_IS_NAN(x))
        !            38:     {
        !            39:       MPFR_SET_NAN(y);
        !            40:       MPFR_RET_NAN;
        !            41:     }
        !            42:
        !            43:   MPFR_CLEAR_NAN(y);
        !            44:
        !            45:   /* check for inf or -inf (result is not defined) */
        !            46:   if (MPFR_IS_INF(x))
        !            47:     {
        !            48:       if (MPFR_SIGN(x) > 0)
        !            49:         {
        !            50:           MPFR_SET_INF(y);
        !            51:           MPFR_SET_POS(y);
        !            52:           MPFR_RET(0);
        !            53:         }
        !            54:       else
        !            55:         {
        !            56:           MPFR_SET_NAN(y);
        !            57:           MPFR_RET_NAN;
        !            58:         }
        !            59:     }
        !            60:
        !            61:   comp = mpfr_cmp_si(x,-1);
        !            62:   /* x<-1 undefined */
        !            63:   if (comp < 0)
        !            64:     {
        !            65:       MPFR_SET_NAN(y);
        !            66:       MPFR_RET_NAN;
        !            67:     }
        !            68:   /* x=0: log1p(-1)=-inf (division by zero) */
        !            69:   if (comp == 0)
        !            70:     {
        !            71:       MPFR_SET_INF(y);
        !            72:       MPFR_SET_POS(y);
        !            73:       MPFR_RET_NAN;
        !            74:     }
        !            75:
        !            76:   MPFR_CLEAR_INF(y);
        !            77:
        !            78:   if (MPFR_IS_ZERO(x))
        !            79:     {
        !            80:       MPFR_SET_ZERO(y);   /* log1p(+/- 0) = +/- 0 */
        !            81:       MPFR_SET_SAME_SIGN(y, x);
        !            82:       MPFR_RET(0);
        !            83:     }
        !            84:
        !            85:   /* General case */
        !            86:   {
        !            87:     /* Declaration of the intermediary variable */
        !            88:     mpfr_t t;
        !            89:
        !            90:     /* Declaration of the size variable */
        !            91:     mp_prec_t Nx = MPFR_PREC(x);   /* Precision of input variable */
        !            92:     mp_prec_t Ny = MPFR_PREC(y);   /* Precision of input variable */
        !            93:
        !            94:     mp_prec_t Nt;   /* Precision of the intermediary variable */
        !            95:     long int err;  /* Precision of error */
        !            96:
        !            97:     /* compute the precision of intermediary variable */
        !            98:     Nt=MAX(Nx,Ny);
        !            99:     /* the optimal number of bits : see algorithms.ps */
        !           100:     Nt=Nt+5+_mpfr_ceil_log2(Nt);
        !           101:
        !           102:     /* initialise of intermediary variable */
        !           103:     mpfr_init(t);
        !           104:
        !           105:     /* First computation of cosh */
        !           106:     do {
        !           107:
        !           108:       /* reactualisation of the precision */
        !           109:       mpfr_set_prec(t, Nt);
        !           110:
        !           111:       /* compute log1p */
        !           112:       mpfr_add_ui (t, x, 1, GMP_RNDN);   /* 1+x */
        !           113:       mpfr_log (t, t, GMP_RNDN);        /* log(1+x)*/
        !           114:
        !           115:       /* estimation of the error */
        !           116:       /*err=Nt-(_mpfr_ceil_log2(1+pow(2,1-MPFR_EXP(t))));*/
        !           117:       err=Nt-(MAX(1-MPFR_EXP(t),0)+1);
        !           118:
        !           119:       /* actualisation of the precision */
        !           120:       Nt += 10;
        !           121:
        !           122:     } while ((err<0) || !mpfr_can_round(t, err, GMP_RNDN, rnd_mode, Ny));
        !           123:
        !           124:     inexact = mpfr_set (y, t, rnd_mode);
        !           125:
        !           126:     mpfr_clear(t);
        !           127:   }
        !           128:   return inexact;
        !           129: }

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