mirror of
https://github.com/foo-dogsquared/nixos-config.git
synced 2025-02-07 06:19:00 +00:00
bahaghari/lib: update percentage
Also reordered the arguments to make it more "functional".
This commit is contained in:
parent
fc2f27eea8
commit
9d122c32c0
@ -93,14 +93,22 @@ rec {
|
|||||||
Type: percentage :: Number -> Number -> Number
|
Type: percentage :: Number -> Number -> Number
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
percentage 4 100.0
|
percentage 100.0 4
|
||||||
=> 4
|
=> 4
|
||||||
|
|
||||||
percentage 5 200.0
|
percentage 200.0 5
|
||||||
=> 10
|
=> 10
|
||||||
|
|
||||||
|
percentage 55.4 58
|
||||||
|
=> 32.132
|
||||||
|
|
||||||
|
percentage 0 24654
|
||||||
|
=> 0
|
||||||
*/
|
*/
|
||||||
percentage = number: value:
|
percentage = value: number:
|
||||||
number / (100.0 / value);
|
if value == 0
|
||||||
|
then 0
|
||||||
|
else number / (100.0 / value);
|
||||||
|
|
||||||
/* Given a number, round up (or down) its number to the nearest integer.
|
/* Given a number, round up (or down) its number to the nearest integer.
|
||||||
|
|
||||||
|
@ -12,7 +12,7 @@ pkgs.lib.runTests {
|
|||||||
};
|
};
|
||||||
|
|
||||||
testMathPowZero = {
|
testMathPowZero = {
|
||||||
expr = lib.math.pow 34 0;
|
expr = lib.math.pow 31 0;
|
||||||
expected = 1;
|
expected = 1;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -27,15 +27,35 @@ pkgs.lib.runTests {
|
|||||||
};
|
};
|
||||||
|
|
||||||
testMathPercentage = {
|
testMathPercentage = {
|
||||||
expr = lib.math.percentage 100 50;
|
expr = lib.math.percentage 50 100;
|
||||||
expected = 50;
|
expected = 50;
|
||||||
};
|
};
|
||||||
|
|
||||||
testMathPercentage2 = {
|
testMathPercentage2 = {
|
||||||
expr = lib.math.percentage 453 13;
|
expr = lib.math.percentage 13 453;
|
||||||
expected = 58.89;
|
expected = 58.89;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
testMathPercentageNegative = {
|
||||||
|
expr = lib.math.percentage (-20) 500;
|
||||||
|
expected = -100;
|
||||||
|
};
|
||||||
|
|
||||||
|
testMathPercentageNegative2 = {
|
||||||
|
expr = lib.math.percentage (-64) 843;
|
||||||
|
expected = -539.52;
|
||||||
|
};
|
||||||
|
|
||||||
|
testMathPercentageZero = {
|
||||||
|
expr = lib.math.percentage 0 45723;
|
||||||
|
expected = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
testMathPercentageZero2 = {
|
||||||
|
expr = lib.math.percentage 0 (-3423);
|
||||||
|
expected = 0;
|
||||||
|
};
|
||||||
|
|
||||||
testMathGrow = {
|
testMathGrow = {
|
||||||
expr = lib.math.grow 500 12;
|
expr = lib.math.grow 500 12;
|
||||||
expected = 72;
|
expected = 72;
|
||||||
|
Loading…
Reference in New Issue
Block a user