Blame specfunc/pow_int.c

Packit 67cb25
/* specfunc/pow_int.c
Packit 67cb25
 * 
Packit 67cb25
 * Copyright (C) 1996, 1997, 1998, 1999, 2000 Gerard Jungman
Packit 67cb25
 * 
Packit 67cb25
 * This program is free software; you can redistribute it and/or modify
Packit 67cb25
 * it under the terms of the GNU General Public License as published by
Packit 67cb25
 * the Free Software Foundation; either version 3 of the License, or (at
Packit 67cb25
 * your option) any later version.
Packit 67cb25
 * 
Packit 67cb25
 * This program is distributed in the hope that it will be useful, but
Packit 67cb25
 * WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 67cb25
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 67cb25
 * General Public License for more details.
Packit 67cb25
 * 
Packit 67cb25
 * You should have received a copy of the GNU General Public License
Packit 67cb25
 * along with this program; if not, write to the Free Software
Packit 67cb25
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Packit 67cb25
 */
Packit 67cb25
Packit 67cb25
/* Author:  G. Jungman */
Packit 67cb25
Packit 67cb25
#include <config.h>
Packit 67cb25
#include <gsl/gsl_math.h>
Packit 67cb25
#include <gsl/gsl_errno.h>
Packit 67cb25
#include <gsl/gsl_sf_pow_int.h>
Packit 67cb25
Packit 67cb25
Packit 67cb25
/*-*-*-*-*-*-*-*-*-*-*-* Functions w/ Error handling *-*-*-*-*-*-*-*-*-*-*-*/
Packit 67cb25
Packit 67cb25
int gsl_sf_pow_int_e(double x, int n, gsl_sf_result * result)
Packit 67cb25
{
Packit 67cb25
  double value = 1.0;
Packit 67cb25
  int count = 0;
Packit 67cb25
Packit 67cb25
  /* CHECK_POINTER(result) */
Packit 67cb25
Packit 67cb25
Packit 67cb25
  if(n < 0) {
Packit 67cb25
    n = -n;
Packit 67cb25
Packit 67cb25
    if(x == 0.0) {
Packit 67cb25
      double u = 1.0 / x;
Packit 67cb25
      result->val = (n % 2) ? u : (u * u) ;  /* correct sign of infinity */
Packit 67cb25
      result->err = GSL_POSINF;
Packit 67cb25
      GSL_ERROR ("overflow", GSL_EOVRFLW);
Packit 67cb25
    }
Packit 67cb25
Packit 67cb25
    x = 1.0/x;
Packit 67cb25
  }
Packit 67cb25
Packit 67cb25
  /* repeated squaring method 
Packit 67cb25
   * returns 0.0^0 = 1.0, so continuous in x
Packit 67cb25
   */
Packit 67cb25
  do {
Packit 67cb25
     if(GSL_IS_ODD(n)) value *= x;
Packit 67cb25
     n >>= 1;
Packit 67cb25
     x *= x;
Packit 67cb25
     ++count;
Packit 67cb25
  } while (n);
Packit 67cb25
Packit 67cb25
  result->val = value;
Packit 67cb25
  result->err = 2.0 * GSL_DBL_EPSILON * (count + 1.0) * fabs(value); 
Packit 67cb25
Packit 67cb25
  return GSL_SUCCESS;
Packit 67cb25
}
Packit 67cb25
Packit 67cb25
/*-*-*-*-*-*-*-*-*-* Functions w/ Natural Prototypes *-*-*-*-*-*-*-*-*-*-*/
Packit 67cb25
Packit 67cb25
#include "eval.h"
Packit 67cb25
Packit 67cb25
double gsl_sf_pow_int(const double x, const int n)
Packit 67cb25
{
Packit 67cb25
  EVAL_RESULT(gsl_sf_pow_int_e(x, n, &result));
Packit 67cb25
}