From cf23a9254849bbf143e8b2012c489936be8823f6 Mon Sep 17 00:00:00 2001 From: Gabriel Arazas Date: Thu, 27 Jun 2024 21:08:09 +0800 Subject: [PATCH] bahaghari/lib: add radian <-> degree conversion in math subset --- subprojects/bahaghari/lib/default.nix | 2 +- subprojects/bahaghari/lib/math.nix | 31 ++++++++++++++++++++++++ subprojects/bahaghari/tests/lib/math.nix | 30 +++++++++++++++++++++++ 3 files changed, 62 insertions(+), 1 deletion(-) diff --git a/subprojects/bahaghari/lib/default.nix b/subprojects/bahaghari/lib/default.nix index 5c3a3c07..9ef99ce2 100644 --- a/subprojects/bahaghari/lib/default.nix +++ b/subprojects/bahaghari/lib/default.nix @@ -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; }) diff --git a/subprojects/bahaghari/lib/math.nix b/subprojects/bahaghari/lib/math.nix index cc8494d3..8be21661 100644 --- a/subprojects/bahaghari/lib/math.nix +++ b/subprojects/bahaghari/lib/math.nix @@ -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; } diff --git a/subprojects/bahaghari/tests/lib/math.nix b/subprojects/bahaghari/tests/lib/math.nix index 6743a7aa..76571525 100644 --- a/subprojects/bahaghari/tests/lib/math.nix +++ b/subprojects/bahaghari/tests/lib/math.nix @@ -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; + }; }