ack/lang/cem/libcc.ansi/math/hypot.c
George Koehler b00a2c906d Build fdopen(), hypot(), putenv() in libc.
These functions are in POSIX; hypot() is in C99.  Also build cabs()
because it rides with hypot(), but don't declare cabs() in any header
file, because our compiler can't parse C99 "double complex" type.

Touch build.lua so it sees that .c files moved.
2017-10-28 13:33:57 -04:00

39 lines
652 B
C

/*
* (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>
/* $Id$ */
double
hypot(double x,double y)
{
/* Computes sqrt(x*x+y*y), avoiding overflow */
if (x < 0) x = -x;
if (y < 0) y = -y;
if (x > y) {
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) */
if (y == 0.0) return 0.0;
x /= y;
return y*sqrt(x*x+1.0);
}
struct complex {
double r,i;
};
double
cabs(struct complex p_compl)
{
return hypot(p_compl.r, p_compl.i);
}