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

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

1.1       maekawa     1: /* mpfr_exp -- exponential of a floating-point number
                      2:
1.1.1.2 ! ohara       3: Copyright 1999, 2000, 2001, 2002 Free Software Foundation.
        !             4: Contributed by the Spaces project.
1.1       maekawa     5:
                      6: This file is part of the MPFR Library.
                      7:
                      8: The MPFR Library is free software; you can redistribute it and/or modify
1.1.1.2 ! ohara       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
1.1       maekawa    11: option) any later version.
                     12:
                     13: The MPFR Library is distributed in the hope that it will be useful, but
                     14: WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
1.1.1.2 ! ohara      15: or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
1.1       maekawa    16: License for more details.
                     17:
1.1.1.2 ! ohara      18: You should have received a copy of the GNU Lesser General Public License
1.1       maekawa    19: along with the MPFR Library; see the file COPYING.LIB.  If not, write to
                     20: the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
                     21: MA 02111-1307, USA. */
                     22:
                     23: #include <stdio.h>
                     24: #include "gmp.h"
                     25: #include "gmp-impl.h"
                     26: #include "mpfr.h"
1.1.1.2 ! ohara      27: #include "mpfr-impl.h"
1.1       maekawa    28:
                     29: /* #define DEBUG */
                     30:
                     31: /* use Brent's formula exp(x) = (1+r+r^2/2!+r^3/3!+...)^(2^K)*2^n
                     32:    where x = n*log(2)+(2^K)*r
                     33:    number of operations = O(K+prec(r)/K)
                     34: */
                     35: int
1.1.1.2 ! ohara      36: mpfr_exp (mpfr_ptr y, mpfr_srcptr x, mp_rnd_t rnd_mode)
1.1       maekawa    37: {
1.1.1.2 ! ohara      38:   int expx, precy;
        !            39:   double d;
1.1       maekawa    40:
1.1.1.2 ! ohara      41:   if (MPFR_IS_NAN(x))
        !            42:     {
        !            43:       MPFR_SET_NAN(y);
        !            44:       MPFR_RET_NAN;
        !            45:     }
        !            46:
        !            47:   MPFR_CLEAR_NAN(y);
        !            48:
        !            49:   if (MPFR_IS_INF(x))
        !            50:     {
        !            51:       if (MPFR_SIGN(x) > 0)
        !            52:        {
        !            53:          MPFR_SET_INF(y);
        !            54:        }
        !            55:       else
        !            56:        {
        !            57:          MPFR_CLEAR_INF(y);
        !            58:          MPFR_SET_ZERO(y);
        !            59:        }
        !            60:       MPFR_SET_POS(y);
        !            61:       MPFR_RET(0);
        !            62:     }
        !            63:
        !            64:   MPFR_CLEAR_INF(y);
        !            65:
        !            66:   if (MPFR_IS_ZERO(x))
        !            67:     return mpfr_set_ui (y, 1, GMP_RNDN);
        !            68:
        !            69:   expx = MPFR_EXP(x);
        !            70:   precy = MPFR_PREC(y);
        !            71:
        !            72:   /* result is +Inf when exp(x) >= 2^(__mpfr_emax), i.e.
        !            73:      x >= __mpfr_emax * log(2) */
        !            74:   d = mpfr_get_d1 (x);
        !            75:   if (d >= (double) __mpfr_emax * LOG2)
        !            76:     return mpfr_set_overflow(y, rnd_mode, 1);
        !            77:
        !            78:   /* result is 0 when exp(x) < 1/2*2^(__mpfr_emin), i.e.
        !            79:      x < (__mpfr_emin-1) * LOG2 */
        !            80:   if (d < ((double) __mpfr_emin - 1.0) * LOG2)
        !            81:     return mpfr_set_underflow(y, rnd_mode, 1);
1.1       maekawa    82:
                     83:   /* if x < 2^(-precy), then exp(x) i.e. gives 1 +/- 1 ulp(1) */
1.1.1.2 ! ohara      84:   if (expx < -precy)
        !            85:     {
        !            86:       int signx = MPFR_SIGN(x);
        !            87:
        !            88:       mpfr_set_ui (y, 1, rnd_mode);
        !            89:       if (signx > 0 && rnd_mode == GMP_RNDU)
        !            90:        {
        !            91:          mpfr_add_one_ulp (y, rnd_mode);
        !            92:          return 1;
        !            93:        }
        !            94:       else if (signx < 0 && (rnd_mode == GMP_RNDD || rnd_mode == GMP_RNDZ))
        !            95:        {
        !            96:          mpfr_sub_one_ulp (y, rnd_mode);
        !            97:          return -1;
        !            98:        }
        !            99:       return -signx;
        !           100:     }
        !           101:
        !           102:   if (precy > 13000)
        !           103:     return mpfr_exp3 (y, x, rnd_mode); /* O(M(n) log(n)^2) */
        !           104:   else
        !           105:     return mpfr_exp_2 (y, x, rnd_mode); /* O(n^(1/3) M(n)) */
1.1       maekawa   106: }

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