bahaghari/lib: add radian <-> degree conversion in math subset

This commit is contained in:
Gabriel Arazas 2024-06-27 21:08:09 +08:00
parent 5003d9723a
commit cf23a92548
No known key found for this signature in database
GPG Key ID: 62104B43D00AA360
3 changed files with 62 additions and 1 deletions

View File

@ -50,5 +50,5 @@ pkgs.lib.makeExtensible
inherit (self.hex) isHexString;
inherit (self.math) abs pow percentage factorial floor ceil round round'
summate product sqrt;
summate product sqrt remainder mod radiansToDegrees degreesToRadians;
})

View File

@ -313,4 +313,35 @@ rec {
=> 24
*/
product = builtins.foldl' builtins.mul 1;
/* Given a number in radians, convert it to degrees.
Type: radiansToDegrees :: Number -> Number
Example:
radiansToDegrees bahaghariLib.math.constants.pi
=> 180
radiansToDegrees 180
=> 10313.240312355
*/
radiansToDegrees = x:
x * 180.0 / constants.pi;
/* Given a number in degrees unit, convert it to radians.
Type: degreesToRadians :: Number -> Number
Example:
degreesToRadians 180
=> 3.141592653589793238462643383279502884197
degreesToRadians 360
=> 6.283185307
degreesToRadians 95
=> 1.658062789
*/
degreesToRadians = x:
x * constants.pi / 180.0;
}

View File

@ -332,4 +332,34 @@ lib.runTests {
expr = round' (self.math.exp 2);
expected = 7.3890560989;
};
testDegreesToRadians = {
expr = self.math.degreesToRadians 180;
expected = self.math.constants.pi;
};
testDegreesToRadians2 = {
expr = self.math.degreesToRadians 360;
expected = self.math.constants.pi * 2;
};
testDegreesToRadians3 = {
expr = self.math.round' (-5) (self.math.degreesToRadians 95);
expected = 1.65806;
};
testRadiansToDegrees = {
expr = self.math.radiansToDegrees self.math.constants.pi;
expected = 180;
};
testRadiansToDegrees2 = {
expr = self.math.round' (-3) (self.math.radiansToDegrees 180);
expected = 10313.24;
};
testRadiansToDegrees3 = {
expr = self.math.round' (-3) (self.math.radiansToDegrees 4.5);
expected = 257.831;
};
}