Skip to content

Commit

Permalink
Added cbrt() and exp2() functions in math module (lcompilers#…
Browse files Browse the repository at this point in the history
  • Loading branch information
harshsingh-24 committed Mar 22, 2023
1 parent e0f8d1e commit a49f18f
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
1 change: 1 addition & 0 deletions integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ RUN(NAME test_builtin_divmod LABELS cpython llvm c)
RUN(NAME test_builtin_sum LABELS cpython llvm c)
RUN(NAME test_math1 LABELS cpython llvm c)
RUN(NAME test_math_02 LABELS cpython llvm)
RUN(NAME test_math_03 LABELS llvm) #1595: TODO: Test using CPython (3.11 recommended)
RUN(NAME test_pass_compare LABELS cpython llvm c)
RUN(NAME test_c_interop_01 LABELS cpython llvm c)
RUN(NAME test_c_interop_02 LABELS cpython llvm c
Expand Down
1 change: 0 additions & 1 deletion integration_tests/test_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ def test_exp():
i = exp(2.34)
assert abs(i - 10.381236562731843) < eps


def test_pow():
eps: f64
eps = 1e-12
Expand Down
21 changes: 21 additions & 0 deletions integration_tests/test_math_03.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from math import (cbrt, exp2)
from ltypes import f64

eps: f64
eps = 1e-12

def test_exp2():
i: f64
i = exp2(4.3)
assert abs(i - 19.698310613518657) < eps

def test_cbrt():
eps: f64 = 1e-12
assert abs(cbrt(124.0) - 4.986630952238646) < eps
assert abs(cbrt(39.0) - 3.3912114430141664) < eps

def check():
test_cbrt()
test_exp2()

check()
15 changes: 15 additions & 0 deletions src/runtime/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,12 @@ def exp(x: f64) -> f64:
"""
return e**x

def exp2(x: f64) -> f64:
"""
Return `2` raised to the power `x`.
"""
return f64((2.0)**x)


def mod(a: i32, b: i32) -> i32:
"""
Expand Down Expand Up @@ -540,8 +546,17 @@ def trunc(x: f32) -> i32:
return ceil(x)

def sqrt(x: f64) -> f64:
"""
Returns square root of a number x
"""
return x**(1/2)

def cbrt(x: f64) -> f64:
"""
Returns cube root of a number x
"""
return x**(1/3)

@ccall
def _lfortran_dsin(x: f64) -> f64:
pass
Expand Down

0 comments on commit a49f18f

Please sign in to comment.