summaryrefslogtreecommitdiff
path: root/hypotenuse.c
blob: ab4bb2129b851296563e69aba51dc1554bc5625b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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);
}