jak-project/test/goalc/source_templates/with_game/test-approx-pi.gc
water111 460ec874bb
Add support for stack integers (#135)
* add support for stack integers

* update documentation

* revise value type stack variables
2020-11-22 12:22:19 -05:00

48 lines
985 B
Common Lisp

(start-test "approx-pi")
(defun test-approx-pi ((res integer))
(let ((rad (* res res))
(count 0))
(dotimes (x res)
(dotimes (y res)
(if (> rad (+ (* x x) (* y y)))
(+! count 1)
)
)
)
(* 4.0 (/ (the float count) (the float rad)))
)
)
(let ((approx-pi (test-approx-pi 1000)))
(expect-true (> approx-pi 3.14))
(expect-true (< approx-pi 3.15))
)
(defun test-approx-pi-float ((res float))
(let* ((rad (the float (* res res)))
(count (the float 0))
(x (the float 0))
(y (the float 0))
(scale (/ 1.0 rad)))
(while (< x res)
(set! y 0.0)
(while (< y res)
(if (> rad (+ (* x x) (* y y)))
(+! count scale)
)
(+! y 1.0)
)
(+! x 1.0)
)
(* 4.0 count)
)
)
(let ((approx-pi (test-approx-pi-float 500.0)))
(expect-true (> approx-pi 3.14))
(expect-true (< approx-pi 3.15))
)
(finish-test)