Blame sys/hypot.c

Packit 67cb25
/* sys/hypot.c
Packit 67cb25
 * 
Packit 67cb25
 * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 Gerard Jungman, Brian Gough, Patrick Alken
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
#include <config.h>
Packit 67cb25
#include <math.h>
Packit 67cb25
#include <gsl/gsl_math.h>
Packit 67cb25
Packit 67cb25
double gsl_hypot (const double x, const double y)
Packit 67cb25
{
Packit 67cb25
  double xabs = fabs(x) ;
Packit 67cb25
  double yabs = fabs(y) ;
Packit 67cb25
  double min, max;
Packit 67cb25
Packit 67cb25
  /* Follow the optional behavior of the ISO C standard and return
Packit 67cb25
     +Inf when any of the argument is +-Inf, even if the other is NaN.
Packit 67cb25
     http://pubs.opengroup.org/onlinepubs/009695399/functions/hypot.html */
Packit 67cb25
  if (gsl_isinf(x) || gsl_isinf(y)) 
Packit 67cb25
    {
Packit 67cb25
      return GSL_POSINF;
Packit 67cb25
    }
Packit 67cb25
Packit 67cb25
  if (xabs < yabs) {
Packit 67cb25
    min = xabs ;
Packit 67cb25
    max = yabs ;
Packit 67cb25
  } else {
Packit 67cb25
    min = yabs ;
Packit 67cb25
    max = xabs ;
Packit 67cb25
  }
Packit 67cb25
Packit 67cb25
  if (min == 0) 
Packit 67cb25
    {
Packit 67cb25
      return max ;
Packit 67cb25
    }
Packit 67cb25
Packit 67cb25
  {
Packit 67cb25
    double u = min / max ;
Packit 67cb25
    return max * sqrt (1 + u * u) ;
Packit 67cb25
  }
Packit 67cb25
}
Packit 67cb25
Packit 67cb25
double
Packit 67cb25
gsl_hypot3(const double x, const double y, const double z)
Packit 67cb25
{
Packit 67cb25
  double xabs = fabs(x);
Packit 67cb25
  double yabs = fabs(y);
Packit 67cb25
  double zabs = fabs(z);
Packit 67cb25
  double w = GSL_MAX(xabs, GSL_MAX(yabs, zabs));
Packit 67cb25
Packit 67cb25
  if (w == 0.0)
Packit 67cb25
    {
Packit 67cb25
      return (0.0);
Packit 67cb25
    }
Packit 67cb25
  else
Packit 67cb25
    {
Packit 67cb25
      double r = w * sqrt((xabs / w) * (xabs / w) +
Packit 67cb25
                          (yabs / w) * (yabs / w) +
Packit 67cb25
                          (zabs / w) * (zabs / w));
Packit 67cb25
      return r;
Packit 67cb25
    }
Packit 67cb25
}