2024-02-29 14:06:33 +00:00
|
|
|
# A little math utility for common operations. Don't expect any high-level
|
|
|
|
# mathematical operations nor godly optimizations expected from a typical math
|
|
|
|
# library, it's just basic high school type of shit in all aspects.
|
2024-03-02 10:10:21 +00:00
|
|
|
{ pkgs, lib, self }:
|
2024-02-29 14:06:33 +00:00
|
|
|
|
|
|
|
rec {
|
|
|
|
/* Returns the absolute value of the given number.
|
|
|
|
|
|
|
|
Type: abs :: Int -> Int
|
|
|
|
|
|
|
|
Example:
|
|
|
|
abs -4
|
|
|
|
=> 4
|
|
|
|
|
|
|
|
abs (1 / 5)
|
|
|
|
=> 0.2
|
|
|
|
*/
|
|
|
|
abs = number:
|
|
|
|
if number < 0 then -(number) else number;
|
|
|
|
|
|
|
|
/* Exponentiates the given base with the exponent.
|
|
|
|
|
|
|
|
Type: pow :: Int -> Int -> Int
|
|
|
|
|
|
|
|
Example:
|
|
|
|
pow 2 3
|
|
|
|
=> 8
|
|
|
|
|
|
|
|
pow 6 4
|
|
|
|
=> 1296
|
|
|
|
*/
|
|
|
|
pow = base: exponent:
|
|
|
|
# I'll just make this as a tail recursive function instead.
|
|
|
|
let
|
|
|
|
absValue = abs exponent;
|
|
|
|
iter = product: counter: max-count:
|
|
|
|
if counter > max-count
|
|
|
|
then product
|
|
|
|
else iter (product * base) (counter + 1) max-count;
|
|
|
|
value = iter 1 1 absValue;
|
|
|
|
in
|
|
|
|
if exponent < 0 then (1 / value) else value;
|
|
|
|
|
2024-03-02 04:54:59 +00:00
|
|
|
/* Returns a boolean whether the given number is within the given (inclusive) range.
|
|
|
|
|
|
|
|
Type: isWithinRange :: Number -> Number -> Number -> Bool
|
|
|
|
|
|
|
|
Example:
|
|
|
|
isWithinRange 30 50 6
|
|
|
|
=> false
|
|
|
|
|
|
|
|
isWithinRange 0 100 75
|
|
|
|
=> true
|
|
|
|
*/
|
|
|
|
isWithinRange = min: max: number:
|
2024-03-02 10:10:21 +00:00
|
|
|
(lib.max number min) <= (lib.min number max);
|
2024-03-02 04:54:59 +00:00
|
|
|
|
2024-02-29 14:06:33 +00:00
|
|
|
/* Given a number, make it grow by given amount of percentage.
|
|
|
|
A value of 100 should make the number doubled.
|
|
|
|
|
|
|
|
Type: grow :: Number -> Number -> Number
|
|
|
|
|
|
|
|
Example:
|
|
|
|
grow 4 50.0
|
|
|
|
=> 2
|
|
|
|
|
|
|
|
grow 55.5 100
|
|
|
|
=> 111
|
|
|
|
*/
|
2024-03-02 04:54:59 +00:00
|
|
|
grow = value: number:
|
2024-02-29 14:06:33 +00:00
|
|
|
number + (percentage number value);
|
|
|
|
|
2024-03-02 04:54:59 +00:00
|
|
|
/* Similar to `grow` but only limits to be within the given (inclusive)
|
|
|
|
range.
|
2024-02-29 14:06:33 +00:00
|
|
|
|
2024-03-02 04:54:59 +00:00
|
|
|
Type: grow' :: Number -> Number -> Number -> Number
|
2024-02-29 14:06:33 +00:00
|
|
|
|
|
|
|
Example:
|
2024-03-02 04:54:59 +00:00
|
|
|
grow' 0 255 12 100
|
|
|
|
=> 24
|
2024-02-29 14:06:33 +00:00
|
|
|
|
2024-03-02 04:54:59 +00:00
|
|
|
grow' 1 10 5 (-200)
|
|
|
|
=> 1
|
2024-02-29 14:06:33 +00:00
|
|
|
*/
|
2024-03-02 04:54:59 +00:00
|
|
|
grow' = min: max: value: number:
|
|
|
|
let
|
|
|
|
res = grow number value;
|
|
|
|
in
|
2024-03-02 10:10:21 +00:00
|
|
|
lib.min max (lib.max res min);
|
2024-02-29 14:06:33 +00:00
|
|
|
|
|
|
|
/* Given a number, return its value by the given percentage.
|
|
|
|
|
|
|
|
Type: percentage :: Number -> Number -> Number
|
|
|
|
|
|
|
|
Example:
|
2024-03-02 09:18:15 +00:00
|
|
|
percentage 100.0 4
|
2024-02-29 14:06:33 +00:00
|
|
|
=> 4
|
|
|
|
|
2024-03-02 09:18:15 +00:00
|
|
|
percentage 200.0 5
|
2024-02-29 14:06:33 +00:00
|
|
|
=> 10
|
2024-03-02 09:18:15 +00:00
|
|
|
|
|
|
|
percentage 55.4 58
|
|
|
|
=> 32.132
|
|
|
|
|
|
|
|
percentage 0 24654
|
|
|
|
=> 0
|
2024-02-29 14:06:33 +00:00
|
|
|
*/
|
2024-03-02 09:18:15 +00:00
|
|
|
percentage = value: number:
|
|
|
|
if value == 0
|
|
|
|
then 0
|
|
|
|
else number / (100.0 / value);
|
2024-02-29 14:06:33 +00:00
|
|
|
|
|
|
|
/* Given a number, round up (or down) its number to the nearest integer.
|
|
|
|
|
|
|
|
Type: round :: Number -> Number
|
|
|
|
|
|
|
|
Example:
|
|
|
|
round 3.5
|
|
|
|
=> 4
|
|
|
|
|
|
|
|
round 2.3
|
|
|
|
=> 2
|
|
|
|
|
|
|
|
round 2.7
|
|
|
|
=> 3
|
|
|
|
*/
|
|
|
|
round = number:
|
|
|
|
let
|
|
|
|
number' = builtins.floor number;
|
|
|
|
difference = number - number';
|
|
|
|
in
|
|
|
|
if difference >= 0.5 then (number' + 1) else number';
|
|
|
|
}
|