M lib/default.nix => lib/default.nix +1 -1
@@ 20,7 20,7 @@ let
inherit (lists) indexOf isSubsetOf;
math = callLibs ./math.nix;
- inherit (math) abs pow;
+ inherit (math) abs pow rem;
strings = callLibs ./strings.nix;
inherit (strings) capitalize parseHex;
M lib/math.nix => lib/math.nix +8 -0
@@ 25,4 25,12 @@ rec {
assert e >= 0;
assert e > 0 -> (abs result) >= (abs n);
result;
+
+ /* rem :: int -> int -> int
+ *
+ * Computes the remainder of the division of `n` by `d`, such that
+ * `rem n d == n - (n / d) * d`. The sign of the result is that of
+ * the dividend `n`.
+ */
+ rem = n: d: n - d * (n / d);
}
M lib/tests.nix => lib/tests.nix +25 -0
@@ 228,6 228,31 @@ runTestsOrDieTrying {
expected = -243;
};
+ testRem1 = {
+ expr = rem 120 9;
+ expected = 3;
+ };
+
+ testRem2 = {
+ expr = rem (-120) 9;
+ expected = -3;
+ };
+
+ testRem3 = {
+ expr = rem 120 (-9);
+ expected = 3;
+ };
+
+ testRem4 = {
+ expr = rem 688258375 8765;
+ expected = 688258375 - (688258375 / 8765) * 8765;
+ };
+
+ testRem5 = {
+ expr = rem 688258375 (-620);
+ expected = 688258375 - (688258375 / (-620)) * (-620);
+ };
+
###########
# strings #
###########