summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--it.class8
-rwxr-xr-xtests/it.t5
2 files changed, 13 insertions, 0 deletions
diff --git a/it.class b/it.class
index 9ade3a0..0ea9a2f 100644
--- a/it.class
+++ b/it.class
@@ -1061,4 +1061,12 @@ static function sort($array, $mode = "")
return $array;
}
+/**
+ * returns $a modulo $n as a positive number between 0 and $n - 1
+ */
+static function mod($a, $n)
+{
+ return (($a % $n) + $n) % $n;
+}
+
}
diff --git a/tests/it.t b/tests/it.t
index 4fd848b..3688d2a 100755
--- a/tests/it.t
+++ b/tests/it.t
@@ -410,3 +410,8 @@ foreach (array($dummy, false, true, null, 1, "a", "Ä", "/", array()) as $var)
is(it::sort(array("2!","19!","1!")), array("1!", "19!", "2!"));
is(it::sort(array("2!","19!","1!"), "r"), array("2!", "19!", "1!"));
is(it::sort(array("2!","19!","1!"), "rn"), array("19!", "2!", "1!"));
+
+is(it::mod(-9, 4), 3);
+is(it::mod(-8, 4), 0);
+is(it::mod(0, 4), 0);
+is(it::mod(7, 4), 3);