ack/lang/cem/libcc.ansi/core/math/hypot.c

44 lines
667 B
C
Raw Normal View History

1991-02-26 18:08:25 +00:00
/*
* (c) copyright 1988 by the Vrije Universiteit, Amsterdam, The Netherlands.
* See the copyright notice in the ACK home directory, in the file "Copyright".
*
* Author: Ceriel J.H. Jacobs
*/
#include <math.h>
1994-06-24 14:02:31 +00:00
/* $Id$ */
1991-02-26 18:08:25 +00:00
double
2018-06-21 20:33:47 +00:00
hypot(double x, double y)
1991-02-26 18:08:25 +00:00
{
/* Computes sqrt(x*x+y*y), avoiding overflow */
2018-06-21 20:33:47 +00:00
if (x < 0)
x = -x;
if (y < 0)
y = -y;
if (x > y)
{
1991-02-26 18:08:25 +00:00
double t = y;
y = x;
x = t;
}
/* sqrt(x*x+y*y) = sqrt(y*y*(x*x/(y*y)+1.0)) = y*sqrt(x*x/(y*y)+1.0) */
2018-06-21 20:33:47 +00:00
if (y == 0.0)
return 0.0;
1991-02-26 18:08:25 +00:00
x /= y;
2018-06-21 20:33:47 +00:00
return y * sqrt(x * x + 1.0);
1991-02-26 18:08:25 +00:00
}
2018-06-21 20:33:47 +00:00
struct complex
{
double r, i;
1991-02-26 18:08:25 +00:00
};
double
cabs(struct complex p_compl)
{
return hypot(p_compl.r, p_compl.i);
}