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

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

1.1       ohara       1: /*  mpfr_ui_pow_ui -- compute the power beetween two machine integer
                      2:
                      3: Copyright 1999, 2000, 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 "mpfr.h"
                     24: #include "mpfr-impl.h"
                     25:
                     26: int
                     27: mpfr_ui_pow_ui (mpfr_ptr x, unsigned long int y, unsigned long int n,
                     28:                 mp_rnd_t rnd)
                     29: {
                     30:   long int err;
                     31:   unsigned long m;
                     32:   mpfr_t res;
                     33:   mp_prec_t prec;
                     34:   int inexact;
                     35:
                     36:   MPFR_CLEAR_FLAGS(x);
                     37:
                     38:   if (n == 0) /* y^0 = 1 for any y */
                     39:     return mpfr_set_ui (x, 1, rnd);
                     40:
                     41:   if (y == 0) /* 0^n = 0 for any n > 0 */
                     42:     return mpfr_set_ui (x, 0, rnd);
                     43:
                     44:   mpfr_save_emin_emax ();
                     45:   mpfr_init (res);
                     46:
                     47:   prec = MPFR_PREC(x);
                     48:
                     49:   do
                     50:     {
                     51:       int i;
                     52:
                     53:       prec += 3;
                     54:       for (i = 0, m = n; m; i++, m >>= 1)
                     55:         prec++;
                     56:       mpfr_set_prec (res, prec);
                     57:       inexact = mpfr_set_ui (res, y, GMP_RNDU);
                     58:       err = 1;
                     59:       /* now 2^(i-1) <= n < 2^i */
                     60:       for (i-=2; i>=0; i--)
                     61:        {
                     62:          if (mpfr_mul (res, res, res, GMP_RNDU))
                     63:            inexact = 1;
                     64:          err++;
                     65:          if (n & (1 << i))
                     66:            if (mpfr_mul_ui (res, res, y, GMP_RNDU))
                     67:              inexact = 1;
                     68:        }
                     69:       err = prec - err;
                     70:       if (err < 0)
                     71:        err = 0;
                     72:     }
                     73:   while (inexact && (mpfr_can_round (res, err,
                     74:           MPFR_SIGN(res) > 0 ? GMP_RNDU : GMP_RNDD, rnd, MPFR_PREC(x)) == 0));
                     75:
                     76:   if (mpfr_set (x, res, rnd))
                     77:     inexact = 1;
                     78:
                     79:   mpfr_clear (res);
                     80:
                     81:   MPFR_RESTORE_RET(inexact, x, rnd);
                     82: }

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