Pristine Ack-5.5
[Ack-5.5.git] / lang / cem / libcc.ansi / misc / hypot.c
1 /*
2  * (c) copyright 1988 by the Vrije Universiteit, Amsterdam, The Netherlands.
3  * See the copyright notice in the ACK home directory, in the file "Copyright".
4  *
5  * Author: Ceriel J.H. Jacobs
6  */
7
8 #include <math.h>
9
10 /* $Id: hypot.c,v 1.2 1994/06/24 11:45:27 ceriel Exp $ */
11
12 double
13 hypot(double x,double y)
14 {
15         /*      Computes sqrt(x*x+y*y), avoiding overflow */
16
17         if (x < 0) x = -x;
18         if (y < 0) y = -y;
19         if (x > y) {
20                 double t = y;
21                 y = x;
22                 x = t;
23         }
24         /* sqrt(x*x+y*y) = sqrt(y*y*(x*x/(y*y)+1.0)) = y*sqrt(x*x/(y*y)+1.0) */
25         if (y == 0.0) return 0.0;
26         x /= y;
27         return y*sqrt(x*x+1.0);
28 }
29
30 struct complex {
31         double r,i;
32 };
33
34 double
35 cabs(struct complex p_compl)
36 {
37         return hypot(p_compl.r, p_compl.i);
38 }