Skip to content

Post sync integration tests - Math module #2847

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@ RUN(NAME test_import_05 LABELS cpython llvm llvm_jit c wasm wasm_x86 wasm_x
RUN(NAME test_import_06 LABELS cpython llvm llvm_jit)
RUN(NAME test_import_07 LABELS cpython llvm llvm_jit c)
RUN(NAME test_import_08 LABELS cpython llvm)
# RUN(NAME test_math LABELS cpython llvm llvm_jit NOFAST)
RUN(NAME test_math LABELS cpython llvm llvm_jit NOFAST)
# RUN(NAME test_membership_01 LABELS cpython llvm)
RUN(NAME test_numpy_01 LABELS cpython llvm llvm_jit c)
RUN(NAME test_numpy_02 LABELS cpython llvm llvm_jit c)
Expand Down
135 changes: 68 additions & 67 deletions integration_tests/test_math.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from math import (factorial, isqrt, perm, comb, degrees, radians, exp, pow,
ldexp, fabs, gcd, lcm, floor, ceil, remainder, expm1, fmod, log1p, trunc,
modf, fsum, prod, dist, frexp, isclose)
fsum, prod, dist, )
import math
from lpython import i8, i16, i32, i64, f32, f64

Expand Down Expand Up @@ -227,19 +227,19 @@ def test_dist():
y = [6.1, 7.2, 8.0, 9.0, 10.0]
assert abs(dist(x, y) - 11.081105044173166) < eps

def test_modf():
i: f64
i = 3.14

res: tuple[f64, f64]
res = modf(i)
assert abs(res[0] - 0.14) <= 1e-6
assert abs(res[1] - 3.0) <= 1e-6

i = -442.3
res = modf(i)
assert abs(res[0] + 0.3) <= 1e-6
assert abs(res[1] + 442.0) <= 1e-6
# def test_modf():
# i: f64
# i = 3.14
#
# res: tuple[f64, f64]
# res = modf(i)
# assert abs(res[0] - 0.14) <= 1e-6
# assert abs(res[1] - 3.0) <= 1e-6
#
# i = -442.3
# res = modf(i)
# assert abs(res[0] + 0.3) <= 1e-6
# assert abs(res[1] + 442.0) <= 1e-6


def test_issue_1242():
Expand All @@ -253,56 +253,56 @@ def test_issue_1242():
assert abs(math.pi - 3.14159265358979323846) < 1e-10


def test_frexp():
x:f64 = 6.23
mantissa:f64
exponent:i16
mantissa, exponent = frexp(x)
assert abs(mantissa - 0.77875) < eps and exponent == i16(3)

x = 0.8
mantissa, exponent = frexp(x)
assert abs(mantissa - 0.8) < eps and exponent == i16(0)

x = 19.74
mantissa, exponent = frexp(x)
assert abs(mantissa - 0.616875) < eps and exponent == i16(5)

x = -23.6
mantissa, exponent = frexp(x)
assert abs(mantissa + 0.7375) < eps and exponent == i16(5)

y:f32 = f32(1.23)
mantissa2:f32
exponent2:i8
mantissa2, exponent2 = frexp(y)
assert abs(mantissa2 - f32(0.615)) < f32(eps) and exponent2 == i8(1)

y = f32(-1.23)
mantissa2, exponent2 = frexp(y)
assert abs(mantissa2 - f32(-0.615)) < f32(eps) and exponent2 == i8(1)


def test_isclose():
x:f64 = 2.2130
y:f64 = 2.2129
assert isclose(x, y, rel_tol=0.01, abs_tol=0.001)
assert isclose(x,y,rel_tol=0.0000001,abs_tol=0.01)
assert isclose(x,y,rel_tol=0.1,abs_tol=0.000001)
assert not isclose(x,y,rel_tol=0.0000001,abs_tol=0.00001)

x = -1.265
y = 1.265
assert not isclose(x,y,rel_tol=0.001,abs_tol=0.0001)
assert not isclose(y,x,rel_tol=0.01,abs_tol=0.1)
assert not isclose(x,y,rel_tol=0.01,abs_tol=0.1)

x = -1.2650
y = -1.2651
assert isclose(x, y, rel_tol=0.01, abs_tol=0.001)
assert isclose(x,y,rel_tol=0.0000001,abs_tol=0.01)
assert isclose(x,y,rel_tol=0.1,abs_tol=0.000001)
assert not isclose(x,y,rel_tol=0.0000001,abs_tol=0.00001)
# def test_frexp():
# x:f64 = 6.23
# mantissa:f64
# exponent:i16
# mantissa, exponent = frexp(x)
# assert abs(mantissa - 0.77875) < eps and exponent == i16(3)
#
# x = 0.8
# mantissa, exponent = frexp(x)
# assert abs(mantissa - 0.8) < eps and exponent == i16(0)
#
# x = 19.74
# mantissa, exponent = frexp(x)
# assert abs(mantissa - 0.616875) < eps and exponent == i16(5)
#
# x = -23.6
# mantissa, exponent = frexp(x)
# assert abs(mantissa + 0.7375) < eps and exponent == i16(5)
#
# y:f32 = f32(1.23)
# mantissa2:f32
# exponent2:i8
# mantissa2, exponent2 = frexp(y)
# assert abs(mantissa2 - f32(0.615)) < f32(eps) and exponent2 == i8(1)
#
# y = f32(-1.23)
# mantissa2, exponent2 = frexp(y)
# assert abs(mantissa2 - f32(-0.615)) < f32(eps) and exponent2 == i8(1)
#
#
# def test_isclose():
# x:f64 = 2.2130
# y:f64 = 2.2129
# assert isclose(x, y, rel_tol=0.01, abs_tol=0.001)
# assert isclose(x,y,rel_tol=0.0000001,abs_tol=0.01)
# assert isclose(x,y,rel_tol=0.1,abs_tol=0.000001)
# assert not isclose(x,y,rel_tol=0.0000001,abs_tol=0.00001)
#
# x = -1.265
# y = 1.265
# assert not isclose(x,y,rel_tol=0.001,abs_tol=0.0001)
# assert not isclose(y,x,rel_tol=0.01,abs_tol=0.1)
# assert not isclose(x,y,rel_tol=0.01,abs_tol=0.1)
#
# x = -1.2650
# y = -1.2651
# assert isclose(x, y, rel_tol=0.01, abs_tol=0.001)
# assert isclose(x,y,rel_tol=0.0000001,abs_tol=0.01)
# assert isclose(x,y,rel_tol=0.1,abs_tol=0.000001)
# assert not isclose(x,y,rel_tol=0.0000001,abs_tol=0.00001)


def check():
Expand All @@ -328,10 +328,11 @@ def check():
test_fsum()
test_prod()
test_dist()
test_modf()
# test_modf()
test_issue_1242()
test_frexp()
test_isclose()
# test_frexp()
# test_isclose()


check()

33 changes: 17 additions & 16 deletions src/runtime/math.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from lpython import i8, i16, i32, f32, i64, f64, ccall, overload

from lpython import ccall, f32, f64, i8, i16, i32, i64, overload

pi: f64 = 3.141592653589793238462643383279502884197
e: f64 = 2.718281828459045235360287471352662497757
Expand Down Expand Up @@ -718,10 +717,11 @@ def frexp(x:f64) -> tuple[f64,i16]:
m is a float and e is an integer such that x == m * 2**e exactly.
'''
exponent: i16 = i16(0)
x_: f64 = x
while f64(fabs(x)) > f64(1.0):
exponent += i16(1)
x /= 2.0
return x, exponent
x_ /= 2.0
return x_, exponent


@overload
Expand All @@ -731,17 +731,18 @@ def frexp(x:f32) -> tuple[f32,i8]:
m is a float and e is an integer such that x == m * 2**e exactly.
'''
exponent: i8 = i8(0)
x_ :f32 = x
while f32(fabs(x)) > f32(1.0):
exponent += i8(1)
x /= f32(2.0)
return x, exponent


@overload
def isclose(a:f64, b:f64, rel_tol:f64 = 1e-09, abs_tol:f64 = 0.0) -> bool:
'''
Return True if the values a and b are close to each other and False otherwise.
'''
difference:f64 = fabs(a-b)
greater:f64 = max(fabs(a),fabs(b))
return difference <= max(rel_tol*greater, abs_tol)
x_ /= f32(2.0)
return x_, exponent


# @overload
# def isclose(a:f64, b:f64, rel_tol:f64 = 1e-09, abs_tol:f64 = 0.0) -> bool:
# '''
# Return True if the values a and b are close to each other and False otherwise.
# '''
# difference:f64 = fabs(a-b)
# greater:f64 = max(fabs(a),fabs(b))
# return difference <= max(rel_tol*greater, abs_tol)
Loading