mirror of
https://github.com/foo-dogsquared/nixos-config.git
synced 2025-01-31 04:58:01 +00:00
bahaghari/lib: add math.mod
Now, it's the real modulo operation.
This commit is contained in:
parent
61720afeb2
commit
ff0ddb0e27
@ -252,6 +252,23 @@ rec {
|
||||
in
|
||||
floor (difference + 0.5) * nearest;
|
||||
|
||||
/* Given a base and a modulus, returns the value of a modulo operation.
|
||||
|
||||
Type: mod :: Number -> Number -> Number
|
||||
|
||||
Example:
|
||||
mod 5 4
|
||||
=> 1
|
||||
|
||||
mod 1245 4.5
|
||||
=> 3
|
||||
|
||||
mod 19 (-12)
|
||||
=> -5
|
||||
*/
|
||||
mod = base: modulus:
|
||||
remainder ((remainder base modulus) + modulus) modulus;
|
||||
|
||||
/* Similar to the nixpkgs' `trivial.mod` but retain the decimal values. This
|
||||
is just an approximation from ECMAScript's implementation of the remainder
|
||||
operator.
|
||||
|
@ -227,6 +227,36 @@ lib.runTests {
|
||||
expected = 1.4142135624;
|
||||
};
|
||||
|
||||
testMathMod = {
|
||||
expr = self.math.mod 5 4;
|
||||
expected = 1;
|
||||
};
|
||||
|
||||
testMathMod2 = {
|
||||
expr = self.math.mod 1245 4.5;
|
||||
expected = 3;
|
||||
};
|
||||
|
||||
testMathModPositiveOperands = {
|
||||
expr = self.math.mod 19 12;
|
||||
expected = 7;
|
||||
};
|
||||
|
||||
testMathModNegativeDividend = {
|
||||
expr = self.math.mod (-19) 12;
|
||||
expected = 5;
|
||||
};
|
||||
|
||||
testMathModNegativeDivisor = {
|
||||
expr = self.math.mod 19 (-12);
|
||||
expected = -5;
|
||||
};
|
||||
|
||||
testMathModNegativeOperands = {
|
||||
expr = self.math.mod (-19) (-12);
|
||||
expected = -7;
|
||||
};
|
||||
|
||||
testMathRemainder = {
|
||||
expr = self.math.remainder 65.5 3;
|
||||
expected = 2.5;
|
||||
|
Loading…
Reference in New Issue
Block a user