/* mpfr_cbrt -- cube root function. Copyright 2002 Free Software Foundation. Contributed by the Spaces project, INRIA Lorraine. This file is part of the MPFR Library. The MPFR Library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. The MPFR Library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with the MPFR Library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include #include #include "gmp.h" #include "gmp-impl.h" #include "mpfr.h" #include "mpfr-impl.h" /* The computation of y=x^(1/3) is done by Case exp-log y=e^((1/3)*log(x)) Case Newton y / y_{k+1}=(1/3)[(x/(y_k^2))+2y_k] */ int #if __STDC__ mpfr_cbrt (mpfr_ptr y, mpfr_srcptr x , mp_rnd_t rnd_mode) #else mpfr_cbrt (y, x, rnd_mode) mpfr_ptr y; mpfr_srcptr x; mp_rnd_t rnd_mode; #endif { /****** Declaration ******/ /* Variable of Intermediary Calculation*/ mpfr_t t1,t2,t; int round; int boucle; long int exp_t; ldiv_t epsilon; int tau=2; int ktau=0; int i; mp_prec_t Nx; /* Precision of input variable */ mp_prec_t Ny; /* Precision of output variable */ mp_prec_t Nt; /* Precision of Intermediary Calculation variable */ mp_prec_t Ntemp; /* Precision of Intermediary Calculation variable */ mp_prec_t err; /* Precision of error */ /* Gestion des NaN */ if (MPFR_IS_NAN(x)) { MPFR_SET_NAN(y); return 1; } MPFR_CLEAR_NAN(y); /* Gestion des infinies*/ if (MPFR_IS_INF(x)){ MPFR_SET_INF(y); if(MPFR_SIGN(x) > 0) { if (MPFR_SIGN(y) < 0) MPFR_CHANGE_SIGN(y);} else{ if (MPFR_SIGN(y) < 0) MPFR_CHANGE_SIGN(y);} return 1; } MPFR_CLEAR_INF(y); /*Gestion du cas 0*/ if(!MPFR_NOTZERO(x)){ MPFR_SET_ZERO(y); /* cbrt(+/- 0) = +/- 0 */ if(MPFR_SIGN(x) > 0){ if (MPFR_SIGN(y) < 0) MPFR_CHANGE_SIGN(y); } else{ if (MPFR_SIGN(y) > 0) MPFR_CHANGE_SIGN(y); } return 0; } /* Initialisation of the Precision */ Nx=MPFR_PREC(x); Ny=MPFR_PREC(y); /* compute the size of intermediary variable */ if(Ny>=Nx) Nt=Ny; else Nt=Nx; /* Calcul du nombre d'iteration necessaire pour newton*/ /* t0=2, t{k+1}=2.t{k}-1 k / tk>n */ while(tau<=Nt){ tau=2*tau-1; ktau++; } /* Calcul de la taille des variable temporaire */ Ntemp=0; for(i=0;i