Skip to content

Commit a49f18f

Browse files
Added cbrt() and exp2() functions in math module (#1594)
1 parent e0f8d1e commit a49f18f

File tree

4 files changed

+37
-1
lines changed

4 files changed

+37
-1
lines changed

integration_tests/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,7 @@ RUN(NAME test_builtin_divmod LABELS cpython llvm c)
331331
RUN(NAME test_builtin_sum LABELS cpython llvm c)
332332
RUN(NAME test_math1 LABELS cpython llvm c)
333333
RUN(NAME test_math_02 LABELS cpython llvm)
334+
RUN(NAME test_math_03 LABELS llvm) #1595: TODO: Test using CPython (3.11 recommended)
334335
RUN(NAME test_pass_compare LABELS cpython llvm c)
335336
RUN(NAME test_c_interop_01 LABELS cpython llvm c)
336337
RUN(NAME test_c_interop_02 LABELS cpython llvm c

integration_tests/test_math.py

-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ def test_exp():
6060
i = exp(2.34)
6161
assert abs(i - 10.381236562731843) < eps
6262

63-
6463
def test_pow():
6564
eps: f64
6665
eps = 1e-12

integration_tests/test_math_03.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from math import (cbrt, exp2)
2+
from ltypes import f64
3+
4+
eps: f64
5+
eps = 1e-12
6+
7+
def test_exp2():
8+
i: f64
9+
i = exp2(4.3)
10+
assert abs(i - 19.698310613518657) < eps
11+
12+
def test_cbrt():
13+
eps: f64 = 1e-12
14+
assert abs(cbrt(124.0) - 4.986630952238646) < eps
15+
assert abs(cbrt(39.0) - 3.3912114430141664) < eps
16+
17+
def check():
18+
test_cbrt()
19+
test_exp2()
20+
21+
check()

src/runtime/math.py

+15
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,12 @@ def exp(x: f64) -> f64:
465465
"""
466466
return e**x
467467

468+
def exp2(x: f64) -> f64:
469+
"""
470+
Return `2` raised to the power `x`.
471+
"""
472+
return f64((2.0)**x)
473+
468474

469475
def mod(a: i32, b: i32) -> i32:
470476
"""
@@ -540,8 +546,17 @@ def trunc(x: f32) -> i32:
540546
return ceil(x)
541547

542548
def sqrt(x: f64) -> f64:
549+
"""
550+
Returns square root of a number x
551+
"""
543552
return x**(1/2)
544553

554+
def cbrt(x: f64) -> f64:
555+
"""
556+
Returns cube root of a number x
557+
"""
558+
return x**(1/3)
559+
545560
@ccall
546561
def _lfortran_dsin(x: f64) -> f64:
547562
pass

0 commit comments

Comments
 (0)