From 820ea56746421b3fe716df99104b8a938b029143 Mon Sep 17 00:00:00 2001 From: Gabriel Arazas Date: Sat, 3 Aug 2024 08:51:28 +0800 Subject: [PATCH] bahaghari/lib: move toFloat to `trivial` namespace --- subprojects/bahaghari/lib/default.nix | 2 +- subprojects/bahaghari/lib/math.nix | 14 --------- subprojects/bahaghari/lib/trivial.nix | 14 +++++++++ .../bahaghari/tests/lib/trivial/default.nix | 29 +++++++++++++++++++ 4 files changed, 44 insertions(+), 15 deletions(-) diff --git a/subprojects/bahaghari/lib/default.nix b/subprojects/bahaghari/lib/default.nix index 9ef99ce2..afa85dcf 100644 --- a/subprojects/bahaghari/lib/default.nix +++ b/subprojects/bahaghari/lib/default.nix @@ -46,7 +46,7 @@ pkgs.lib.makeExtensible inherit (self.trivial) importYAML toYAML toBaseDigitsWithGlyphs generateGlyphSet generateConversionTable generateBaseDigitType clamp - isNumber scale optionalNull; + isNumber scale optionalNull toFloat; inherit (self.hex) isHexString; inherit (self.math) abs pow percentage factorial floor ceil round round' diff --git a/subprojects/bahaghari/lib/math.nix b/subprojects/bahaghari/lib/math.nix index ed1a01cc..7f543d6b 100644 --- a/subprojects/bahaghari/lib/math.nix +++ b/subprojects/bahaghari/lib/math.nix @@ -60,20 +60,6 @@ rec { abs = number: if number < 0 then -(number) else number; - /* Given a Nix number, force it to be a floating value. - - Type: toFloat :: Number -> Float - - Example: - toFloat 5 - => 5.0 - - toFloat 59.0 - => 59.0 - */ - toFloat = x: - 1.0 * x; - /* Exponentiates the given base with the exponent. Type: pow :: Int -> Int -> Int diff --git a/subprojects/bahaghari/lib/trivial.nix b/subprojects/bahaghari/lib/trivial.nix index bbaa1818..1791d87b 100644 --- a/subprojects/bahaghari/lib/trivial.nix +++ b/subprojects/bahaghari/lib/trivial.nix @@ -178,6 +178,20 @@ rec { isNumber = v: lib.isInt v || lib.isFloat v; + /* Given a Nix number, force it to be a floating value. + + Type: toFloat :: Number -> Float + + Example: + toFloat 5 + => 5.0 + + toFloat 59.0 + => 59.0 + */ + toFloat = x: + 1.0 * x; + /* Given an initial range of integers, scale the given number with its own set of range. diff --git a/subprojects/bahaghari/tests/lib/trivial/default.nix b/subprojects/bahaghari/tests/lib/trivial/default.nix index 278a8ab5..8d9bec5c 100644 --- a/subprojects/bahaghari/tests/lib/trivial/default.nix +++ b/subprojects/bahaghari/tests/lib/trivial/default.nix @@ -1,6 +1,10 @@ { pkgs, lib, self }: let + # The typical rounding procedure for our results. 10 decimal places should be + # enough to test accuracy at least for a basic math subset like this. + round' = self.math.round' (-6); + customOctalGlyphs = { "0" = "A"; "1" = "B"; @@ -241,6 +245,21 @@ lib.runTests { expected = (-68); }; + testNumberScaleFloat = { + expr = self.trivial.scale { inMin = 0; inMax = 255; outMin = 0.0; outMax = 1.0; } 255; + expected = 1.0; + }; + + testNumberScaleFloat2 = { + expr = self.trivial.scale { inMin = 0; inMax = 255; outMin = 0.0; outMax = 1.0; } 127.5; + expected = 0.5; + }; + + testNumberScaleFloat3 = { + expr = round' (self.trivial.scale { inMin = 0; inMax = 255; outMin = 0.0; outMax = 1.0; } 53); + expected = round' 0.207843; + }; + testIsNumber1 = { expr = self.trivial.isNumber 3; expected = true; @@ -270,4 +289,14 @@ lib.runTests { expr = self.trivial.optionalNull false "HELLO"; expected = null; }; + + testToFloat = { + expr = self.trivial.toFloat 46; + expected = 46.0; + }; + + testToFloat2 = { + expr = self.trivial.toFloat 26.5; + expected = 26.5; + }; }