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

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

1.1       ohara       1: /* mpfr_ui_pow -- power of n function n^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:
                     28:  /* The computation of y=pow(n,z) is done by
                     29:
                     30:     y=exp(z*log(n))=n^z
                     31:  */
                     32:
                     33: int
                     34: mpfr_ui_pow (mpfr_ptr y, unsigned long int n, mpfr_srcptr x, mp_rnd_t rnd_mode)
                     35: {
                     36:     int inexact;
                     37:
                     38:     if (MPFR_IS_NAN(x))
                     39:       {
                     40:         MPFR_SET_NAN(y);
                     41:         MPFR_RET_NAN;
                     42:       }
                     43:
                     44:     MPFR_CLEAR_NAN(y);
                     45:
                     46:     if (MPFR_IS_INF(x))
                     47:       {
                     48:         if (MPFR_SIGN(x) < 0)
                     49:           {
                     50:             MPFR_CLEAR_INF(y);
                     51:             MPFR_SET_ZERO(y);
                     52:           }
                     53:         else
                     54:           {
                     55:             MPFR_SET_INF(y);
                     56:           }
                     57:         MPFR_SET_POS(y);
                     58:         MPFR_RET(0);
                     59:       }
                     60:
                     61:     /* n^0 = 1 */
                     62:     if (MPFR_IS_ZERO(x))
                     63:       {
                     64:         return mpfr_set_ui(y,1,rnd_mode);
                     65:       }
                     66:
                     67:     /* General case */
                     68:     {
                     69:     /* Declaration of the intermediary variable */
                     70:       mpfr_t t, te, ti;
                     71:
                     72:       /* Declaration of the size variable */
                     73:       mp_prec_t Nx = MPFR_PREC(x);   /* Precision of input variable */
                     74:       mp_prec_t Ny = MPFR_PREC(y);   /* Precision of input variable */
                     75:
                     76:       mp_prec_t Nt;   /* Precision of the intermediary variable */
                     77:       long int err;  /* Precision of error */
                     78:
                     79:       /* compute the precision of intermediary variable */
                     80:       Nt=MAX(Nx,Ny);
                     81:       /* the optimal number of bits : see algorithms.ps */
                     82:       Nt=Nt+5+_mpfr_ceil_log2(Nt);
                     83:
                     84:       /* initialise of intermediary    variable */
                     85:       mpfr_init(t);
                     86:       mpfr_init2(ti,sizeof(unsigned long int)*8); /* 8 = CHAR_BIT */
                     87:       mpfr_init(te);
                     88:
                     89:       do {
                     90:
                     91:        /* reactualisation of the precision */
                     92:        mpfr_set_prec(t,Nt);
                     93:        mpfr_set_prec(te,Nt);
                     94:
                     95:        /* compute   exp(x*ln(n))*/
                     96:         mpfr_set_ui(ti,n,GMP_RNDN);      /* ti <- n*/
                     97:         mpfr_log(t,ti,GMP_RNDU);         /* ln(n) */
                     98:         mpfr_mul(te,x,t,GMP_RNDU);       /* x*ln(n) */
                     99:         mpfr_exp(t,te,GMP_RNDN);         /* exp(x*ln(n))*/
                    100:
                    101:        /* estimation of the error -- see pow function in algorithms.ps*/
                    102:        err = Nt - (MPFR_EXP(te)+3);
                    103:
                    104:        /* actualisation of the precision */
                    105:        Nt += 10;
                    106:
                    107:       } while (err<0 || !mpfr_can_round(t,err,GMP_RNDN,rnd_mode,Ny));
                    108:
                    109:       inexact = mpfr_set(y,t,rnd_mode);
                    110:       mpfr_clear(t);
                    111:       mpfr_clear(ti);
                    112:       mpfr_clear(te);
                    113:     }
                    114:     return inexact;
                    115:
                    116: }

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