Skip to content

Commit 4fd0811

Browse files
committed
Merge branch 'main' into list05
2 parents 958275a + 148cae5 commit 4fd0811

File tree

4 files changed

+29
-6
lines changed

4 files changed

+29
-6
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/libasr/runtime/lfortran_intrinsics.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,7 @@ LFORTRAN_API int32_t _lpython_bit_length4(int32_t num)
732732
LFORTRAN_API int32_t _lpython_bit_length8(int64_t num)
733733
{
734734
int32_t res = 0;
735-
num = abs(num);
735+
num = llabs(num);
736736
for(; num; num >>= 1, res++);
737737
return res;
738738
}

src/runtime/math.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@
77

88
# TODO: Change floor used inside functions implemented here to
99
# floordiv operator (//) once the multiple import issue is fixed
10+
11+
@overload
12+
def modf(x: f64) -> tuple[f64, f64]:
13+
"""
14+
Return fractional and integral parts of `x` as a pair.
15+
16+
Both results carry the sign of x and are floats.
17+
"""
18+
return (x - int(x), float(int(x)))
19+
1020
@overload
1121
def factorial(x: i32) -> i32:
1222
"""

0 commit comments

Comments
 (0)