Annotation of OpenXM_contrib/gmp/mpfr/pow_si.c, Revision 1.1
1.1 ! ohara 1: /* mpfr_pow_si -- power function x^y with y an unsigned int
! 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 y=pow(x,z) is done by
! 28:
! 29: y=pow_ui(x,z) if z>0
! 30: else
! 31: y=1/pow_ui(x,z) if z<0
! 32: */
! 33:
! 34: int
! 35: mpfr_pow_si (mpfr_ptr y, mpfr_srcptr x, long int n, mp_rnd_t rnd_mode)
! 36: {
! 37: if (n > 0)
! 38: return mpfr_pow_ui(y, x, n, rnd_mode);
! 39: else
! 40: {
! 41: int inexact = 0;
! 42:
! 43: if (MPFR_IS_NAN(x))
! 44: {
! 45: MPFR_SET_NAN(y);
! 46: MPFR_RET_NAN;
! 47: }
! 48:
! 49: MPFR_CLEAR_NAN(y);
! 50:
! 51: if (n == 0)
! 52: return mpfr_set_ui(y, 1, GMP_RNDN);
! 53:
! 54: if (MPFR_IS_INF(x))
! 55: {
! 56: MPFR_SET_ZERO(y);
! 57: if (MPFR_SIGN(x) > 0 || ((unsigned) n & 1) == 0)
! 58: MPFR_SET_POS(y);
! 59: else
! 60: MPFR_SET_NEG(y);
! 61: MPFR_RET(0);
! 62: }
! 63:
! 64: if (MPFR_IS_ZERO(x))
! 65: {
! 66: MPFR_SET_INF(y);
! 67: if (MPFR_SIGN(x) > 0 || ((unsigned) n & 1) == 0)
! 68: MPFR_SET_POS(y);
! 69: else
! 70: MPFR_SET_NEG(y);
! 71: MPFR_RET(0);
! 72: }
! 73:
! 74: MPFR_CLEAR_INF(y);
! 75:
! 76: n = -n;
! 77:
! 78: /* General case */
! 79: {
! 80: /* Declaration of the intermediary variable */
! 81: mpfr_t t, ti;
! 82:
! 83: /* Declaration of the size variable */
! 84: mp_prec_t Nx = MPFR_PREC(x); /* Precision of input variable */
! 85: mp_prec_t Ny = MPFR_PREC(y); /* Precision of input variable */
! 86:
! 87: mp_prec_t Nt; /* Precision of the intermediary variable */
! 88: long int err; /* Precision of error */
! 89:
! 90: /* compute the precision of intermediary variable */
! 91: Nt=MAX(Nx,Ny);
! 92: /* the optimal number of bits : see algorithms.ps */
! 93: Nt=Nt+3+_mpfr_ceil_log2(Nt);
! 94:
! 95: /* initialise of intermediary variable */
! 96: mpfr_init(t);
! 97: mpfr_init(ti);
! 98:
! 99: do {
! 100:
! 101: /* reactualisation of the precision */
! 102: mpfr_set_prec(t,Nt);
! 103: mpfr_set_prec(ti,Nt);
! 104:
! 105: /* compute 1/(x^n) n>0*/
! 106: mpfr_pow_ui(ti,x,(unsigned long int)(n),GMP_RNDN);
! 107: mpfr_ui_div(t,1,ti,GMP_RNDN);
! 108:
! 109: /* estimation of the error -- see pow function in algorithms.ps*/
! 110: err = Nt - 3;
! 111:
! 112: /* actualisation of the precision */
! 113: Nt += 10;
! 114:
! 115: } while (err<0 || !mpfr_can_round(t,err,GMP_RNDN,rnd_mode,Ny));
! 116:
! 117: inexact = mpfr_set(y,t,rnd_mode);
! 118: mpfr_clear(t);
! 119: mpfr_clear(ti);
! 120: }
! 121: return inexact;
! 122: }
! 123: }
! 124:
! 125:
! 126:
! 127:
! 128:
! 129:
! 130:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>