labs/lib/src/numbers/default.nix

73 lines
2.1 KiB
Nix
Raw Normal View History

2024-06-01 11:00:53 +00:00
lib: {
numbers = {
into = {
## Convert a number into a string.
##
## @type Int | Float -> String
2024-06-22 17:58:44 +00:00
string = value: if builtins.isInt value then builtins.toString value else builtins.toJSON value;
2024-06-01 11:00:53 +00:00
## Convert a number into a list of digits in the given base.
##
## @type Int -> Int -> List Int
2024-06-22 17:58:44 +00:00
base =
base: target:
let
process =
value:
let
r = value - ((value / base) * base);
q = (value - r) / base;
in
if value < base then [ value ] else [ r ] ++ process q;
result = process target;
2024-06-01 11:00:53 +00:00
in
assert lib.errors.trace (builtins.isInt base) "Base must be an integer.";
assert lib.errors.trace (builtins.isInt target) "Target must be an integer.";
assert lib.errors.trace (base >= 2) "Base must be at least 2.";
assert lib.errors.trace (target >= 0) "Target cannot be negative.";
2024-06-22 17:58:44 +00:00
lib.lists.reverse result;
2024-06-01 11:00:53 +00:00
## Convert a number into a hexadecimal string.
##
## @type Int -> String
2024-06-22 17:58:44 +00:00
hex =
value:
let
serialize =
part:
if part < 10 then
builtins.toString part
else if part == 10 then
"A"
else if part == 11 then
"B"
else if part == 12 then
"C"
else if part == 13 then
"D"
else if part == 14 then
"E"
else if part == 15 then
"F"
else
builtins.throw "Invalid hex digit.";
in
lib.strings.concatMap serialize (lib.numbers.into.base 16 value);
2024-06-01 11:00:53 +00:00
};
## Compare two numbers. When the first number is less than the second, -1
## is returned. When the first number is greater than the second, 1 is
## returned. When the numbers are equal, 0 is returned.
##
## @type Int -> Int -> Int
2024-06-22 17:58:44 +00:00
compare =
a: b:
if a < b then
-1
else if a > b then
1
else
0;
2024-06-01 11:00:53 +00:00
};
}