Skip to content

Commit 6084f51

Browse files
committed
Add test for modf()
1 parent afb2db8 commit 6084f51

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

integration_tests/test_list_04.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from random import randrange
21
from math import sqrt
32
from ltypes import i32
43

integration_tests/test_math.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from math import (factorial, isqrt, perm, comb, degrees, radians, exp, pow,
2-
ldexp, fabs, gcd, lcm, floor, ceil, remainder, expm1, fmod, log1p, trunc, fsum, prod)
3-
from ltypes import i8, i16, i32, i64, f32, f64
4-
from numpy import empty
2+
ldexp, fabs, gcd, lcm, floor, ceil, remainder, expm1, fmod, log1p, trunc,
3+
modf, fsum, prod)
4+
from ltypes import i32, i64, f32, f64
55

66
eps: f64
77
eps = 1e-12
@@ -191,7 +191,6 @@ def test_fsum():
191191
res = fsum(arr_f64)
192192
assert abs(res - 19.0) < eps
193193

194-
195194
def test_prod():
196195
res: f64
197196
eps: f64 = 1e-12
@@ -220,6 +219,20 @@ def test_prod():
220219
res = prod(arr_f64)
221220
assert abs(res - 80.64) < eps
222221

222+
def test_modf():
223+
i: f64
224+
i = 3.14
225+
226+
res: tuple[f64, f64]
227+
res = modf(i)
228+
assert abs(res[0] - 0.14) <= 1e-6
229+
assert abs(res[1] - 3.0) <= 1e-6
230+
231+
i = -442.3
232+
res = modf(i)
233+
assert abs(res[0] + 0.3) <= 1e-6
234+
assert abs(res[1] + 442.0) <= 1e-6
235+
223236

224237
def check():
225238
test_factorial_1()
@@ -243,6 +256,7 @@ def check():
243256
test_trunc()
244257
test_fsum()
245258
test_prod()
259+
test_modf()
246260

247261

248262
check()

src/runtime/math.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,16 @@
88
# TODO: Change floor used inside functions implemented here to
99
# floordiv operator (//) once the multiple import issue is fixed
1010

11-
def modf(x: f64) -> tuple[f64, i32]:
11+
@overload
12+
def modf(x: f64) -> tuple[f64, f64]:
1213
"""
1314
Return fractional and integral parts of `x` as a pair.
15+
16+
Both results carry the sign of x and are floats.
1417
"""
15-
return (x - int(x), int(x))
18+
return (x - int(x), float(int(x)))
1619

20+
@overload
1721
def factorial(x: i32) -> i32:
1822
"""
1923
Computes the factorial of `x`.

0 commit comments

Comments
 (0)