summaryrefslogtreecommitdiff
path: root/hypotenuse.c
diff options
context:
space:
mode:
authorrodri <rgl@antares-labs.eu>2020-02-22 09:56:09 +0000
committerrodri <rgl@antares-labs.eu>2020-02-22 09:56:09 +0000
commita39951d8f69209cfea2b7051832b851914e662ef (patch)
treee4cd1c32e5d6f531b523f6fda558cc3a5f603547 /hypotenuse.c
downloadbrokentoys-a39951d8f69209cfea2b7051832b851914e662ef.tar.gz
brokentoys-a39951d8f69209cfea2b7051832b851914e662ef.tar.bz2
brokentoys-a39951d8f69209cfea2b7051832b851914e662ef.zip
now version controlled.
Diffstat (limited to 'hypotenuse.c')
-rw-r--r--hypotenuse.c59
1 files changed, 59 insertions, 0 deletions
diff --git a/hypotenuse.c b/hypotenuse.c
new file mode 100644
index 0000000..ab4bb21
--- /dev/null
+++ b/hypotenuse.c
@@ -0,0 +1,59 @@
+#include <u.h>
+#include <libc.h>
+
+double
+hypot2(double p, double q)
+{
+ return sqrt(p*p + q*q);
+}
+
+double
+hypot3(double x, double y, double z)
+{
+ return sqrt(x*x + y*y + z*z);
+}
+
+double
+hypot3from2(double x, double y, double z)
+{
+ return hypot(hypot(x, z), y);
+}
+
+void
+usage(void)
+{
+ fprint(2, "usage: hypotenuse x y z\n");
+ exits("usage");
+}
+
+void
+main(int argc, char *argv[])
+{
+ double x, y, z, r;
+ vlong t0, t;
+
+ if(argc < 4)
+ usage();
+ x = strtod(argv[1], nil);
+ y = strtod(argv[2], nil);
+ z = strtod(argv[3], nil);
+ /*print("\t2D\n");
+ t0 = nsec();
+ r = hypot2(x, y);
+ t = nsec();
+ print("1st method: %g (%lld ns)\n", r, t-t0);
+ t0 = nsec();
+ r = hypot(x, y);
+ t = nsec();
+ print("2nd method: %g (%lld ns)\n", r, t-t0);
+ print("\t3D\n");
+ t0 = nsec();
+ r = hypot3(x, y, z);
+ t = nsec();
+ print("1st method: %g (%lld ns)\n", r, t-t0);*/
+ t0 = nsec();
+ r = hypot3from2(x, y, z);
+ t = nsec();
+ print("2nd method: %g (%lld ns)\n", r, t-t0);
+ exits(0);
+}