changeset 443:aae8a83a4ebb

New module for some simple maths functions
author Chris Cannam
date Wed, 23 Oct 2013 13:58:42 +0100
parents 1404b9b58868
children cd6ddde000fd
files src/may/mathmisc.yeti src/may/mathmisc/test/test_mathmisc.yeti
diffstat 2 files changed, 45 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/may/mathmisc.yeti	Wed Oct 23 13:58:42 2013 +0100
@@ -0,0 +1,12 @@
+
+module may.mathmisc;
+
+gcd a b = (c = a % b; if c == 0 then b else gcd b c fi);
+
+factorial x = if x < 0 then 0 else fold do x y: x*y done 1 [1..x] fi;
+
+{
+    gcd,
+    factorial,
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/may/mathmisc/test/test_mathmisc.yeti	Wed Oct 23 13:58:42 2013 +0100
@@ -0,0 +1,33 @@
+
+module may.mathmisc.test.test_mathmisc;
+
+mm = load may.mathmisc;
+
+{ compare, compareUsing, assert } = load may.test.test;
+
+[
+
+"gcd": \(
+    compare (mm.gcd 1 1) 1 and
+    compare (mm.gcd 2 1) 1 and
+    compare (mm.gcd 2 3) 1 and
+    compare (mm.gcd 4 2) 2 and
+    compare (mm.gcd 18 24) 6 and
+    compare (mm.gcd 27 18) 9 and
+    compare (mm.gcd 18 36) 18 and
+    compare (mm.gcd 37 18) 1
+),
+
+"factorial": \(
+    compare (mm.factorial (-10)) 0 and
+    compare (mm.factorial 0) 1 and
+    compare (mm.factorial 1) 1 and
+    compare (mm.factorial 2) 2 and
+    compare (mm.factorial 3) 6 and
+    compare (mm.factorial 4) 24 and
+    compare (mm.factorial 4.5) 24 and
+    compare (mm.factorial 20) 2432902008176640000
+),
+
+] is hash<string, () -> boolean>;
+