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

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

1.1       maekawa     1: /* mpfr_pow_ui, mpfr_ui_pow_ui -- compute the power of a floating-point
                      2:                                   number or machine integer
                      3:
                      4: Copyright (C) 1999 PolKA project, Inria Lorraine and Loria
                      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
                      9: it under the terms of the GNU Library General Public License as published by
                     10: the Free Software Foundation; either version 2 of the License, or (at your
                     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
                     15: or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
                     16: License for more details.
                     17:
                     18: You should have received a copy of the GNU Library General Public License
                     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 "mpfr.h"
                     26:
                     27: /* sets x to y^n */
                     28: void
                     29: #if __STDC__
                     30: mpfr_pow_ui (mpfr_ptr x, mpfr_srcptr y, unsigned int n, unsigned char rnd)
                     31: #else
                     32: mpfr_pow_ui (x, y, n, rnd)
                     33:      mpfr_ptr x;
                     34:      mpfr_srcptr y;
                     35:      unsigned int n;
                     36:      unsigned char rnd;
                     37: #endif
                     38: {
                     39:   int i;
                     40:
                     41:   if (n==0) { mpfr_set_ui(x, 1, rnd); return; }
                     42:   mpfr_set(x, y, rnd);
                     43:   for (i=0;(1<<i)<=n;i++);
                     44:   /* now 2^(i-1) <= n < 2^i */
                     45:   for (i-=2; i>=0; i--) {
                     46:     mpfr_mul(x, x, x, rnd);
                     47:     if (n & (1<<i)) mpfr_mul(x, x, y, rnd);
                     48:   }
                     49:   return;
                     50: }
                     51:
                     52: /* sets x to y^n */
                     53: void mpfr_ui_pow_ui (mpfr_ptr x, unsigned int y, unsigned int n,
                     54:                     unsigned char rnd)
                     55: {
                     56:   int i;
                     57:
                     58:   if (n==0) { mpfr_set_ui(x, 1, rnd); return; }
                     59:   mpfr_set_ui(x, y, rnd);
                     60:   for (i=0;(1<<i)<=n;i++);
                     61:   /* now 2^(i-1) <= n < 2^i */
                     62:   for (i-=2; i>=0; i--) {
                     63:     mpfr_mul(x, x, x, rnd);
                     64:     if (n & (1<<i)) mpfr_mul_ui(x, x, y, rnd);
                     65:   }
                     66:   return;
                     67: }

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