bahaghari/lib: rename math.mod' into remainder

It's a much easier thing to write and apparently it is different to
modulo.
This commit is contained in:
Gabriel Arazas 2024-06-15 16:23:30 +08:00
parent da4a0b8826
commit 4bd8aee223
No known key found for this signature in database
GPG Key ID: 62104B43D00AA360
2 changed files with 33 additions and 32 deletions

View File

@ -254,27 +254,27 @@ rec {
/* Similar to the nixpkgs' `trivial.mod` but retain the decimal values. This
is just an approximation from ECMAScript's implementation of the modulo
operator (%).
operator (%) which is more like a remainder operator.
Type: mod' :: Number -> Number -> Number
Type: remainder :: Number -> Number -> Number
Example:
mod' 4.25 2
remainder 4.25 2
=> 0.25
mod' 1.5 2
remainder 1.5 2
=> 1.5
mod' 65 5
remainder 65 5
=> 0
mod' (-54) 4
remainder (-54) 4
=> -2
mod' (-54) (-4)
remainder (-54) (-4)
=> -2
*/
mod' = base: number:
remainder = base: number:
let
base' = abs base;
number' = abs number;

View File

@ -227,63 +227,64 @@ lib.runTests {
expected = 1.4142135624;
};
testMathMod = {
expr = self.math.mod' 65.5 3;
testMathRemainder = {
expr = self.math.remainder 65.5 3;
expected = 2.5;
};
testMathMod2 = {
expr = self.math.mod' 1.5 3;
testMathRemainder2 = {
expr = self.math.remainder 1.5 3;
expected = 1.5;
};
testMathMod3 = {
expr = self.math.mod' 4.25 2;
testMathRemainder3 = {
expr = self.math.remainder 4.25 2;
expected = 0.25;
};
testMathMod4 = {
expr = self.math.mod' 6 6;
testMathRemainder4 = {
expr = self.math.remainder 6 6;
expected = 0;
};
testMathMod5 = {
expr = self.math.mod' 6.5 6;
testMathRemainder5 = {
expr = self.math.remainder 6.5 6;
expected = 0.5;
};
testMathMod6 = {
expr = self.math.mod' 7856.5 20;
testMathRemainder6 = {
expr = self.math.remainder 7856.5 20;
expected = 16.5;
};
testMathMod7 = {
expr = self.math.mod' 7568639.2 45633;
# Computers and their quirky floating-values implementations...
testMathRemainder7 = {
expr = self.math.remainder 7568639.2 45633;
expected = 39194.200000000186;
};
testMathMod8 = {
expr = self.math.mod' 567.5 3.5;
testMathRemainder8 = {
expr = self.math.remainder 567.5 3.5;
expected = 0.5;
};
testMathModBothPositive = {
expr = self.math.mod' 54.5 20.5;
testMathRemainderBothPositive = {
expr = self.math.remainder 54.5 20.5;
expected = 13.5;
};
testMathModNegativeBase = {
expr = self.math.mod' (-54.5) 20.5;
testMathRemainderNegativeBase = {
expr = self.math.remainder (-54.5) 20.5;
expected = -13.5;
};
testMathModNegativeNumber = {
expr = self.math.mod' 54.5 (-20.5);
testMathRemainderNegativeNumber = {
expr = self.math.remainder 54.5 (-20.5);
expected = 13.5;
};
testMathModBothNegatives = {
expr = self.math.mod' (-54.5) (-20.5);
testMathRemainderBothNegatives = {
expr = self.math.remainder (-54.5) (-20.5);
expected = -13.5;
};