From 817ec35b02b45a52bfebb29d4a9f08e437409af3 Mon Sep 17 00:00:00 2001 From: Gabriel Arazas Date: Thu, 30 May 2024 14:42:29 +0800 Subject: [PATCH] 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. --- subprojects/bahaghari/lib/default.nix | 3 ++- subprojects/bahaghari/lib/math.nix | 6 +++++- subprojects/bahaghari/tests/lib/math.nix | 20 ++++++++++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/subprojects/bahaghari/lib/default.nix b/subprojects/bahaghari/lib/default.nix index 627e1a40..52afc540 100644 --- a/subprojects/bahaghari/lib/default.nix +++ b/subprojects/bahaghari/lib/default.nix @@ -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; }) diff --git a/subprojects/bahaghari/lib/math.nix b/subprojects/bahaghari/lib/math.nix index a34683a3..63689b1f 100644 --- a/subprojects/bahaghari/lib/math.nix +++ b/subprojects/bahaghari/lib/math.nix @@ -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. diff --git a/subprojects/bahaghari/tests/lib/math.nix b/subprojects/bahaghari/tests/lib/math.nix index 08356434..5205a848 100644 --- a/subprojects/bahaghari/tests/lib/math.nix +++ b/subprojects/bahaghari/tests/lib/math.nix @@ -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;