Back to index

glibc  2.9
slowpow.c
Go to the documentation of this file.
00001 /*
00002  * IBM Accurate Mathematical Library
00003  * written by International Business Machines Corp.
00004  * Copyright (C) 2001 Free Software Foundation
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU Lesser General Public License as published by
00008  * the Free Software Foundation; either version 2.1 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00019  */
00020 /*************************************************************************/
00021 /* MODULE_NAME:slowpow.c                                                 */
00022 /*                                                                       */
00023 /* FUNCTION:slowpow                                                      */
00024 /*                                                                       */
00025 /*FILES NEEDED:mpa.h                                                     */
00026 /*             mpa.c mpexp.c mplog.c halfulp.c                           */
00027 /*                                                                       */
00028 /* Given two IEEE double machine numbers y,x , routine  computes the     */
00029 /* correctly  rounded (to nearest) value of x^y. Result calculated  by   */
00030 /* multiplication (in halfulp.c) or if result isn't accurate enough      */
00031 /* then routine converts x and y into multi-precision doubles     and    */
00032 /* calls to mpexp routine                                                */
00033 /*************************************************************************/
00034 
00035 #include "mpa.h"
00036 #include "math_private.h"
00037 
00038 void __mpexp(mp_no *x, mp_no *y, int p);
00039 void __mplog(mp_no *x, mp_no *y, int p);
00040 double ulog(double);
00041 double __halfulp(double x,double y);
00042 
00043 double __slowpow(double x, double y, double z) {
00044   double res,res1;
00045   mp_no mpx, mpy, mpz,mpw,mpp,mpr,mpr1;
00046   static const mp_no eps = {-3,{1.0,4.0}};
00047   int p;
00048 
00049   res = __halfulp(x,y);        /* halfulp() returns -10 or x^y             */
00050   if (res >= 0) return res;  /* if result was really computed by halfulp */
00051                   /*  else, if result was not really computed by halfulp */
00052   p = 10;         /*  p=precision   */
00053   __dbl_mp(x,&mpx,p);
00054   __dbl_mp(y,&mpy,p);
00055   __dbl_mp(z,&mpz,p);
00056   __mplog(&mpx, &mpz, p);     /* log(x) = z   */
00057   __mul(&mpy,&mpz,&mpw,p);    /*  y * z =w    */
00058   __mpexp(&mpw, &mpp, p);     /*  e^w =pp     */
00059   __add(&mpp,&eps,&mpr,p);    /*  pp+eps =r   */
00060   __mp_dbl(&mpr, &res, p);
00061   __sub(&mpp,&eps,&mpr1,p);   /*  pp -eps =r1 */
00062   __mp_dbl(&mpr1, &res1, p);  /*  converting into double precision */
00063   if (res == res1) return res;
00064 
00065   p = 32;     /* if we get here result wasn't calculated exactly, continue */
00066   __dbl_mp(x,&mpx,p);                          /* for more exact calculation */
00067   __dbl_mp(y,&mpy,p);
00068   __dbl_mp(z,&mpz,p);
00069   __mplog(&mpx, &mpz, p);   /* log(c)=z  */
00070   __mul(&mpy,&mpz,&mpw,p);  /* y*z =w    */
00071   __mpexp(&mpw, &mpp, p);   /* e^w=pp    */
00072   __mp_dbl(&mpp, &res, p);  /* converting into double precision */
00073   return res;
00074 }