From 55a15dc14b6093abbb11cd2a37aae69898692513 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Tue, 5 Oct 2021 21:57:05 +0200 Subject: [PATCH] includes/std: Added sign, copy_sign, factorial, comb and perm math functions --- includes/libstd/math.pat | 42 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/includes/libstd/math.pat b/includes/libstd/math.pat index 51cd8e4..95626b7 100644 --- a/includes/libstd/math.pat +++ b/includes/libstd/math.pat @@ -17,4 +17,46 @@ namespace std::math { else return x; }; + fn sign(auto x) { + if (x > 0) + return 1; + else if (x < 0) + return -1; + else + return 0; +}; + + fn copy_sign(auto x, auto y) { + if (y >= 0) + return std::math::abs(x); + else + return -std::math::abs(x); + }; + + fn factorial(u128 x) { + u128 result; + + result = x; + while (x > 1) { + x = x - 1; + result = result * x; + } + + return result; + }; + + fn comb(u128 n, u128 k) { + if (k > n) + return 0; + else + return std::math::factorial(n) / (std::math::factorial(k) * std::math::factorial(n - k)); + }; + + fn perm(u128 n, u128 k) { + if (k > n) + return 0; + else + return std::math::factorial(n) / std::math::factorial(n - k); + }; + } \ No newline at end of file