diff --git a/subprojects/bahaghari/lib/math.nix b/subprojects/bahaghari/lib/math.nix index a283a611..30e9ed3c 100644 --- a/subprojects/bahaghari/lib/math.nix +++ b/subprojects/bahaghari/lib/math.nix @@ -253,8 +253,8 @@ rec { floor (difference + 0.5) * nearest; /* Similar to the nixpkgs' `trivial.mod` but retain the decimal values. This - is just an approximation from ECMAScript's implementation of the modulo - operator (%) which is more like a remainder operator. + is just an approximation from ECMAScript's implementation of the remainder + operator. Type: remainder :: Number -> Number -> Number @@ -274,20 +274,11 @@ rec { remainder (-54) (-4) => -2 */ - remainder = base: number: + remainder = dividend: divisor: let - base' = abs base; - number' = abs number; - difference = number' * ((floor (base' / number')) + 1); - - result = abs (number' - (difference - base')); + quotient = dividend / divisor; in - if number' > base' - then base - else - if base < 0 - then -(result) - else result; + dividend - ((floor quotient) * divisor); /* Adds all of the given items on the list starting from a sum of zero. diff --git a/subprojects/bahaghari/tests/lib/math.nix b/subprojects/bahaghari/tests/lib/math.nix index cd5a095f..eb413c81 100644 --- a/subprojects/bahaghari/tests/lib/math.nix +++ b/subprojects/bahaghari/tests/lib/math.nix @@ -268,22 +268,22 @@ lib.runTests { expected = 0.5; }; - testMathRemainderBothPositive = { + testMathRemainderPositiveOperands = { expr = self.math.remainder 54.5 20.5; expected = 13.5; }; - testMathRemainderNegativeBase = { + testMathRemainderNegativeDividend = { expr = self.math.remainder (-54.5) 20.5; - expected = -13.5; + expected = 7; }; - testMathRemainderNegativeNumber = { + testMathRemainderNegativeDivisor = { expr = self.math.remainder 54.5 (-20.5); - expected = 13.5; + expected = -7; }; - testMathRemainderBothNegatives = { + testMathRemainderNegativeOperands = { expr = self.math.remainder (-54.5) (-20.5); expected = -13.5; };