bahaghari/lib: add floor and ceil for math subset

Even though they're already available from the builtins, we still want
the library subset to feel consistent.
This commit is contained in:
Gabriel Arazas 2024-05-30 14:42:29 +08:00
parent 14049db2e4
commit 817ec35b02
No known key found for this signature in database
GPG Key ID: 62104B43D00AA360
3 changed files with 27 additions and 2 deletions

View File

@ -48,5 +48,6 @@ pkgs.lib.makeExtensible
isNumber scale;
inherit (self.hex) isHexString;
inherit (self.math) abs pow percentage factorial round round' summate product;
inherit (self.math) abs pow percentage factorial floor ceil round round'
summate product;
})

View File

@ -4,6 +4,10 @@
{ pkgs, lib, self }:
rec {
# We have the rounding functions here anyways so we may as well include the
# rest of the decimal place changing functions here for consistency.
inherit (builtins) floor ceil;
pi = 3.141592653589793238462643383279502884197;
e = 2.7182818284590452353602874713527;
@ -173,7 +177,7 @@ rec {
nearest = pow 10.0 tens;
difference = number / nearest;
in
builtins.floor (difference + 0.5) * nearest;
floor (difference + 0.5) * nearest;
/* Adds all of the given items on the list starting from a sum of zero.

View File

@ -81,6 +81,26 @@ lib.runTests {
expected = 0;
};
testMathFloor = {
expr = self.math.floor 3.467;
expected = 3;
};
testMathFloor2 = {
expr = self.math.floor 3.796;
expected = 3;
};
testMathCeil = {
expr = self.math.ceil 3.469;
expected = 4;
};
testMathCeil2 = {
expr = self.math.ceil 3.796;
expected = 4;
};
testMathRoundDown = {
expr = self.math.round 2.3;
expected = 2;