mirror of
https://github.com/foo-dogsquared/nixos-config.git
synced 2025-01-31 04:58:01 +00:00
bahaghari/lib: simplify math.remainder
Indeed I'm too dumb for mathematics. :p Also, now the remainder implementation is double-checked with Python 3's math.remainder() function.
This commit is contained in:
parent
4bd8aee223
commit
61720afeb2
@ -253,8 +253,8 @@ rec {
|
|||||||
floor (difference + 0.5) * nearest;
|
floor (difference + 0.5) * nearest;
|
||||||
|
|
||||||
/* 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 remainder
|
||||||
operator (%) which is more like a remainder operator.
|
operator.
|
||||||
|
|
||||||
Type: remainder :: Number -> Number -> Number
|
Type: remainder :: Number -> Number -> Number
|
||||||
|
|
||||||
@ -274,20 +274,11 @@ rec {
|
|||||||
remainder (-54) (-4)
|
remainder (-54) (-4)
|
||||||
=> -2
|
=> -2
|
||||||
*/
|
*/
|
||||||
remainder = base: number:
|
remainder = dividend: divisor:
|
||||||
let
|
let
|
||||||
base' = abs base;
|
quotient = dividend / divisor;
|
||||||
number' = abs number;
|
|
||||||
difference = number' * ((floor (base' / number')) + 1);
|
|
||||||
|
|
||||||
result = abs (number' - (difference - base'));
|
|
||||||
in
|
in
|
||||||
if number' > base'
|
dividend - ((floor quotient) * divisor);
|
||||||
then base
|
|
||||||
else
|
|
||||||
if base < 0
|
|
||||||
then -(result)
|
|
||||||
else result;
|
|
||||||
|
|
||||||
/* Adds all of the given items on the list starting from a sum of zero.
|
/* Adds all of the given items on the list starting from a sum of zero.
|
||||||
|
|
||||||
|
@ -268,22 +268,22 @@ lib.runTests {
|
|||||||
expected = 0.5;
|
expected = 0.5;
|
||||||
};
|
};
|
||||||
|
|
||||||
testMathRemainderBothPositive = {
|
testMathRemainderPositiveOperands = {
|
||||||
expr = self.math.remainder 54.5 20.5;
|
expr = self.math.remainder 54.5 20.5;
|
||||||
expected = 13.5;
|
expected = 13.5;
|
||||||
};
|
};
|
||||||
|
|
||||||
testMathRemainderNegativeBase = {
|
testMathRemainderNegativeDividend = {
|
||||||
expr = self.math.remainder (-54.5) 20.5;
|
expr = self.math.remainder (-54.5) 20.5;
|
||||||
expected = -13.5;
|
expected = 7;
|
||||||
};
|
};
|
||||||
|
|
||||||
testMathRemainderNegativeNumber = {
|
testMathRemainderNegativeDivisor = {
|
||||||
expr = self.math.remainder 54.5 (-20.5);
|
expr = self.math.remainder 54.5 (-20.5);
|
||||||
expected = 13.5;
|
expected = -7;
|
||||||
};
|
};
|
||||||
|
|
||||||
testMathRemainderBothNegatives = {
|
testMathRemainderNegativeOperands = {
|
||||||
expr = self.math.remainder (-54.5) (-20.5);
|
expr = self.math.remainder (-54.5) (-20.5);
|
||||||
expected = -13.5;
|
expected = -13.5;
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user