Skip to content

Commit dedeebb

Browse files
committed
Add distance
1 parent 6e92c82 commit dedeebb

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

src/distance.wat

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
(module
2+
3+
;; #include <math.h>
4+
;;
5+
;; double distance(double x1, double y1, double x2, double y2) {
6+
;; double a = x1 - x2;
7+
;; double b = y1 - y2;
8+
;; return sqrt(pow(a, 2) + pow(b, 2));
9+
;; }
10+
(func $distance (param $x1 f64) (param $y1 f64) (param $x2 f64) (param $y2 f64) (result f64)
11+
(f64.sqrt
12+
(f64.add
13+
(f64.mul
14+
(tee_local $x1
15+
(f64.sub
16+
(get_local $x1)
17+
(get_local $x2)))
18+
(get_local $x1))
19+
(f64.mul
20+
(tee_local $x1
21+
(f64.sub
22+
(get_local $y1)
23+
(get_local $y2)))
24+
(get_local $x1)))))
25+
26+
27+
;; in a different way
28+
(func $distance2 (param $x1 f64) (param $y1 f64) (param $x2 f64) (param $y2 f64) (result f64)
29+
(f64.sqrt
30+
(f64.add
31+
(call $square
32+
(f64.sub
33+
(get_local $x1)
34+
(get_local $x2)))
35+
(call $square
36+
(f64.sub
37+
(get_local $y1)
38+
(get_local $y2))))))
39+
40+
(func $square (param $a f64) (result f64)
41+
(f64.mul
42+
(get_local $a)
43+
(get_local $a)))
44+
)

0 commit comments

Comments
 (0)