Added hypot.c
authorceriel <none@none>
Tue, 26 Feb 1991 18:08:25 +0000 (18:08 +0000)
committerceriel <none@none>
Tue, 26 Feb 1991 18:08:25 +0000 (18:08 +0000)
lang/cem/libcc.ansi/misc/.distr
lang/cem/libcc.ansi/misc/LIST
lang/cem/libcc.ansi/misc/hypot.c [new file with mode: 0644]

index 5aab4d8..f2c13a9 100644 (file)
@@ -21,3 +21,4 @@ sleep.c
 telldir.c
 termcap.c
 mktemp.c
+hypot.c
index 37407a3..bcf7082 100644 (file)
@@ -19,3 +19,4 @@ seekdir.c
 telldir.c
 isatty.c
 mktemp.c
+hypot.c
diff --git a/lang/cem/libcc.ansi/misc/hypot.c b/lang/cem/libcc.ansi/misc/hypot.c
new file mode 100644 (file)
index 0000000..b136ea0
--- /dev/null
@@ -0,0 +1,38 @@
+/*
+ * (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>
+
+/* $Header$ */
+
+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);
+}