From 7bc90a4e91d2dc67c689247a340992179ea8ea5c Mon Sep 17 00:00:00 2001 From: Smit-create Date: Thu, 10 Feb 2022 18:03:11 +0530 Subject: [PATCH 1/5] add more functions to math --- src/runtime/math.py | 51 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 3 deletions(-) diff --git a/src/runtime/math.py b/src/runtime/math.py index 61c6dec835..d3bb3a99ec 100644 --- a/src/runtime/math.py +++ b/src/runtime/math.py @@ -1,3 +1,5 @@ +from ltypes import i32 + def factorial(x: i32) -> i32: """ Computes the factorial of `x`. @@ -7,7 +9,50 @@ def factorial(x: i32) -> i32: return 0 result: i32 result = 1 - while x > 0: - result = result * x - x -= 1 + i: i32 + for i in range(1, x+1): + result *= i return result + + +def comb(n: i32, k: i32) -> i32: + """ + Computes the result of `nCk`, i.e, the number of ways to choose `k` + items from `n` items without repetition and without order. + """ + + if n < k or n < 0: + return 0 + return factorial(n)//(factorial(k)*factorial(n-k)) + + +def perm(n: i32, k: i32) -> i32: + """ + Computes the result of `nPk`, i.e, the number of ways to choose `k` items + from `n` items without repetition and with order. + """ + + if n < k or n < 0: + return 0 + return factorial(n)//factorial(n-k) + + +def isqrt(n: i32) -> i32: + """ + Computes the integer square root of the nonnegative integer `n`. + """ + if n < 0: + # TODO: raise error + return -1 + low: i32 + mid: i32 + high: i32 + low = 0 + high = n+1 + while low + 1 < high: + mid = (low + high)//2 + if mid*mid <= n: + low = mid + else: + high = mid + return low From 1904ef0cdad68b0e3e2ef357466c3e381cb0477b Mon Sep 17 00:00:00 2001 From: Smit-create Date: Thu, 10 Feb 2022 18:03:36 +0530 Subject: [PATCH 2/5] add integration tests --- integration_tests/test_math.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/integration_tests/test_math.py b/integration_tests/test_math.py index 1903380113..f65279ad2a 100644 --- a/integration_tests/test_math.py +++ b/integration_tests/test_math.py @@ -1,8 +1,30 @@ -from math import factorial +from math import factorial, isqrt, perm, comb +from ltypes import i32 def test_factorial_1(): i: i32 i = factorial(10) print(i) + +def test_comb(): + i: i32 + i = comb(10, 2) + print(i) + + +def test_perm(): + i: i32 + i = perm(5, 2) + + +def test_isqrt(): + i: i32 + i = isqrt(15) + print(i) + + test_factorial_1() +test_comb() +test_isqrt() +test_perm() From 359da07393f318fbb68ab55d445856e4bdc0e89f Mon Sep 17 00:00:00 2001 From: Smit-create Date: Thu, 10 Feb 2022 18:34:45 +0530 Subject: [PATCH 3/5] raise error if n < 0 --- src/runtime/math.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/runtime/math.py b/src/runtime/math.py index d3bb3a99ec..9191b6b558 100644 --- a/src/runtime/math.py +++ b/src/runtime/math.py @@ -42,8 +42,7 @@ def isqrt(n: i32) -> i32: Computes the integer square root of the nonnegative integer `n`. """ if n < 0: - # TODO: raise error - return -1 + raise ValueError('`n` should be nonnegative') low: i32 mid: i32 high: i32 From 14c37d03aef002214266c566a2b20acc3417980d Mon Sep 17 00:00:00 2001 From: Smit-create Date: Thu, 10 Feb 2022 18:50:22 +0530 Subject: [PATCH 4/5] add degrees, radians --- src/runtime/math.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/runtime/math.py b/src/runtime/math.py index 9191b6b558..477cf313f6 100644 --- a/src/runtime/math.py +++ b/src/runtime/math.py @@ -1,4 +1,7 @@ -from ltypes import i32 +from ltypes import i32, f64 + +def pi() -> f64: + return 3.141592653589793238462643383279502884197 def factorial(x: i32) -> i32: """ @@ -55,3 +58,17 @@ def isqrt(n: i32) -> i32: else: high = mid return low + + +def degrees(x: f64) -> f64: + """ + Convert angle `x` from radians to degrees. + """ + return x * 180.0 / pi() + + +def radians(x: f64) -> f64: + """ + Convert angle `x` from degrees to radians. + """ + return x * pi() / 180.0 From ebbe72dc6feb711ed47cd8924119aa2318994a83 Mon Sep 17 00:00:00 2001 From: Smit-create Date: Thu, 10 Feb 2022 18:50:35 +0530 Subject: [PATCH 5/5] add tests for degrees, radians --- integration_tests/test_math.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/integration_tests/test_math.py b/integration_tests/test_math.py index f65279ad2a..7f3b7a6fa5 100644 --- a/integration_tests/test_math.py +++ b/integration_tests/test_math.py @@ -1,5 +1,5 @@ -from math import factorial, isqrt, perm, comb -from ltypes import i32 +from math import factorial, isqrt, perm, comb, degrees, radians +from ltypes import i32, f64 def test_factorial_1(): i: i32 @@ -24,7 +24,21 @@ def test_isqrt(): print(i) +def test_degrees(): + i: f64 + i = degrees(32.2) + print(i) + + +def test_radians(): + i: f64 + i = degrees(100.1) + print(i) + + test_factorial_1() test_comb() test_isqrt() test_perm() +test_degrees() +test_radians()