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 /* Similar to the nixpkgs' `trivial.mod` but retain the decimal values. This
is just an approximation from ECMAScript's implementation of the modulo 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: Example:
mod' 4.25 2 remainder 4.25 2
=> 0.25 => 0.25
mod' 1.5 2 remainder 1.5 2
=> 1.5 => 1.5
mod' 65 5 remainder 65 5
=> 0 => 0
mod' (-54) 4 remainder (-54) 4
=> -2 => -2
mod' (-54) (-4) remainder (-54) (-4)
=> -2 => -2
*/ */
mod' = base: number: remainder = base: number:
let let
base' = abs base; base' = abs base;
number' = abs number; number' = abs number;

View File

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