bahaghari/lib: move toFloat to trivial namespace

This commit is contained in:
Gabriel Arazas 2024-08-03 08:51:28 +08:00
parent dc80f95e2b
commit 820ea56746
No known key found for this signature in database
GPG Key ID: 62104B43D00AA360
4 changed files with 44 additions and 15 deletions

View File

@ -46,7 +46,7 @@ pkgs.lib.makeExtensible
inherit (self.trivial) importYAML toYAML toBaseDigitsWithGlyphs inherit (self.trivial) importYAML toYAML toBaseDigitsWithGlyphs
generateGlyphSet generateConversionTable generateBaseDigitType clamp generateGlyphSet generateConversionTable generateBaseDigitType clamp
isNumber scale optionalNull; isNumber scale optionalNull toFloat;
inherit (self.hex) isHexString; inherit (self.hex) isHexString;
inherit (self.math) abs pow percentage factorial floor ceil round round' inherit (self.math) abs pow percentage factorial floor ceil round round'

View File

@ -60,20 +60,6 @@ rec {
abs = number: abs = number:
if number < 0 then -(number) else 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. /* Exponentiates the given base with the exponent.
Type: pow :: Int -> Int -> Int Type: pow :: Int -> Int -> Int

View File

@ -178,6 +178,20 @@ rec {
isNumber = v: isNumber = v:
lib.isInt v || lib.isFloat 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 /* Given an initial range of integers, scale the given number with its own
set of range. set of range.

View File

@ -1,6 +1,10 @@
{ pkgs, lib, self }: { pkgs, lib, self }:
let 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 = { customOctalGlyphs = {
"0" = "A"; "0" = "A";
"1" = "B"; "1" = "B";
@ -241,6 +245,21 @@ lib.runTests {
expected = (-68); 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 = { testIsNumber1 = {
expr = self.trivial.isNumber 3; expr = self.trivial.isNumber 3;
expected = true; expected = true;
@ -270,4 +289,14 @@ lib.runTests {
expr = self.trivial.optionalNull false "HELLO"; expr = self.trivial.optionalNull false "HELLO";
expected = null; expected = null;
}; };
testToFloat = {
expr = self.trivial.toFloat 46;
expected = 46.0;
};
testToFloat2 = {
expr = self.trivial.toFloat 26.5;
expected = 26.5;
};
} }