Skip to content

Commit 54844ab

Browse files
committed
Handle special case for round
1 parent f830649 commit 54844ab

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

integration_tests/test_builtin_round.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ def test_round():
1616
print(round(f))
1717
assert round(13.001) == 13
1818
assert round(-40.49999) == -40
19+
assert round(0.5) == 0
20+
assert round(-0.5) == 0
21+
assert round(1.5) == 2
22+
assert round(50.5) == 50
23+
assert round(56.78) == 57
1924

2025

2126
test_round()

src/lpython/semantics/python_comptime_eval.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -420,8 +420,10 @@ struct PythonIntrinsicProcedures {
420420
ASR::ttype_t* t = ASRUtils::expr_type(expr);
421421
if (ASRUtils::is_real(*t)) {
422422
double rv = ASR::down_cast<ASR::ConstantReal_t>(expr)->m_r;
423-
int64_t ival = round(rv);
424-
return ASR::down_cast<ASR::expr_t>(make_ConstantInteger_t(al, loc, ival, type));
423+
int64_t rounded = round(rv);
424+
if (fabs(rv-rounded) == 0.5)
425+
rounded = 2.0*round(rv/2.0);
426+
return ASR::down_cast<ASR::expr_t>(make_ConstantInteger_t(al, loc, rounded, type));
425427
} else {
426428
throw SemanticError("round() argument must be float for now, not '" +
427429
ASRUtils::type_to_str(t) + "'", loc);

0 commit comments

Comments
 (0)