Skip to content

Commit 6b2ca3b

Browse files
committed
Handle special case for round
1 parent f830649 commit 6b2ca3b

File tree

7 files changed

+15
-7
lines changed

7 files changed

+15
-7
lines changed

integration_tests/run_tests.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"test_builtin_int.py",
2424
"test_builtin_len.py",
2525
"test_builtin_float.py",
26+
"test_builtin_str_02.py",
2627
"test_builtin_round.py",
2728
"test_math1.py",
2829
"test_math_02.py",

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);

tests/reference/asr-test_builtin_len-55b0dec.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "asr-test_builtin_len-55b0dec.stdout",
9-
"stdout_hash": "cb7bfc4061de7903f6e66043ba383d28086ef9bc59cb78ce9b7cea25",
9+
"stdout_hash": "792252950d9dc927a0deabb8eb0f395733f06f8e4ac57854e55cb4ba",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
(TranslationUnit (SymbolTable 1 {lpython_builtin: (IntrinsicModule lpython_builtin), main_program: (Program (SymbolTable 18 {}) main_program [] []), test_len: (Subroutine (SymbolTable 2 {len: (ExternalSymbol 2 len 4 len lpython_builtin [] len Private), s: (Variable 2 s Local () () Default (Character 1 -2 () []) Source Public Required .false.)}) test_len [] [(= (Var 2 s) (ConstantString "abcd" (Character 1 4 () [])) ()) (Assert (Compare (FunctionCall 2 len () [(Var 2 s)] [] (Integer 4 []) () ()) Eq (ConstantInteger 4 (Integer 4 [])) (Logical 4 []) () ()) ()) (= (Var 2 s) (ConstantString "" (Character 1 0 () [])) ()) (Assert (Compare (FunctionCall 2 len () [(Var 2 s)] [] (Integer 4 []) () ()) Eq (ConstantInteger 0 (Integer 4 [])) (Logical 4 []) () ()) ()) (Assert (Compare (FunctionCall 2 len () [(ConstantString "abcd" (Character 1 4 () []))] [] (Integer 4 []) (ConstantInteger 4 (Integer 4 [])) ()) Eq (ConstantInteger 4 (Integer 4 [])) (Logical 4 []) (ConstantLogical .true. (Logical 4 [])) ()) ()) (Assert (Compare (FunctionCall 2 len () [(ConstantString "" (Character 1 0 () []))] [] (Integer 4 []) (ConstantInteger 0 (Integer 4 [])) ()) Eq (ConstantInteger 0 (Integer 4 [])) (Logical 4 []) (ConstantLogical .true. (Logical 4 [])) ()) ())] Source Public Implementation () .false. .false.)}) [])
1+
(TranslationUnit (SymbolTable 1 {_lfortran_main_program: (Subroutine (SymbolTable 19 {}) _lfortran_main_program [] [(SubroutineCall 1 test_len () [] ())] Source Public Implementation () .false. .false.), lpython_builtin: (IntrinsicModule lpython_builtin), main_program: (Program (SymbolTable 18 {}) main_program [] [(SubroutineCall 1 _lfortran_main_program () [] ())]), test_len: (Subroutine (SymbolTable 2 {len: (ExternalSymbol 2 len 4 len lpython_builtin [] len Private), s: (Variable 2 s Local () () Default (Character 1 -2 () []) Source Public Required .false.)}) test_len [] [(= (Var 2 s) (ConstantString "abcd" (Character 1 4 () [])) ()) (Assert (Compare (FunctionCall 2 len () [(Var 2 s)] [] (Integer 4 []) () ()) Eq (ConstantInteger 4 (Integer 4 [])) (Logical 4 []) () ()) ()) (= (Var 2 s) (ConstantString "" (Character 1 0 () [])) ()) (Assert (Compare (FunctionCall 2 len () [(Var 2 s)] [] (Integer 4 []) () ()) Eq (ConstantInteger 0 (Integer 4 [])) (Logical 4 []) () ()) ()) (Assert (Compare (FunctionCall 2 len () [(ConstantString "abcd" (Character 1 4 () []))] [] (Integer 4 []) (ConstantInteger 4 (Integer 4 [])) ()) Eq (ConstantInteger 4 (Integer 4 [])) (Logical 4 []) (ConstantLogical .true. (Logical 4 [])) ()) ()) (Assert (Compare (FunctionCall 2 len () [(ConstantString "" (Character 1 0 () []))] [] (Integer 4 []) (ConstantInteger 0 (Integer 4 [])) ()) Eq (ConstantInteger 0 (Integer 4 [])) (Logical 4 []) (ConstantLogical .true. (Logical 4 [])) ()) ())] Source Public Implementation () .false. .false.)}) [])

tests/reference/asr-test_builtin_round-7417a21.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
"basename": "asr-test_builtin_round-7417a21",
33
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
44
"infile": "tests/../integration_tests/test_builtin_round.py",
5-
"infile_hash": "5dc1e5472efeefb682a75bea5791ea7aa73ffe01fc71e373e18c6645",
5+
"infile_hash": "48e36f1b357956530982b1f1a1d91bade8f408a2fcf6eed603e5f433",
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "asr-test_builtin_round-7417a21.stdout",
9-
"stdout_hash": "341ec4451bad82276a197e11b338fc8864b5442b64dd126f745e8057",
9+
"stdout_hash": "adc8eaae83e7e05a70c07d34ef025ad8ad31b6f38b380ee2fd703aaf",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
(TranslationUnit (SymbolTable 1 {_lfortran_main_program: (Subroutine (SymbolTable 19 {}) _lfortran_main_program [] [(SubroutineCall 1 test_round () [] ())] Source Public Implementation () .false. .false.), lpython_builtin: (IntrinsicModule lpython_builtin), main_program: (Program (SymbolTable 18 {}) main_program [] [(SubroutineCall 1 _lfortran_main_program () [] ())]), test_round: (Subroutine (SymbolTable 2 {f: (Variable 2 f Local () () Default (Real 8 []) Source Public Required .false.), round: (ExternalSymbol 2 round 4 round lpython_builtin [] round Private)}) test_round [] [(= (Var 2 f) (ConstantReal 5.678000 (Real 8 [])) ()) (Print () [(FunctionCall 2 round () [(Var 2 f)] [] (Integer 4 []) () ())]) (= (Var 2 f) (UnaryOp USub (ConstantReal 183745.230000 (Real 8 [])) (Real 8 []) (ConstantReal -183745.230000 (Real 8 []))) ()) (Print () [(FunctionCall 2 round () [(Var 2 f)] [] (Integer 4 []) () ())]) (= (Var 2 f) (ConstantReal 44.340000 (Real 8 [])) ()) (Print () [(FunctionCall 2 round () [(Var 2 f)] [] (Integer 4 []) () ())]) (Assert (Compare (FunctionCall 2 round () [(ConstantReal 13.001000 (Real 8 []))] [] (Integer 4 []) (ConstantInteger 13 (Integer 4 [])) ()) Eq (ConstantInteger 13 (Integer 4 [])) (Logical 4 []) (ConstantLogical .true. (Logical 4 [])) ()) ()) (Assert (Compare (FunctionCall 2 round () [(UnaryOp USub (ConstantReal 40.499990 (Real 8 [])) (Real 8 []) (ConstantReal -40.499990 (Real 8 [])))] [] (Integer 4 []) (ConstantInteger -40 (Integer 4 [])) ()) Eq (UnaryOp USub (ConstantInteger 40 (Integer 4 [])) (Integer 4 []) (ConstantInteger -40 (Integer 4 []))) (Logical 4 []) (ConstantLogical .true. (Logical 4 [])) ()) ())] Source Public Implementation () .false. .false.)}) [])
1+
(TranslationUnit (SymbolTable 1 {_lfortran_main_program: (Subroutine (SymbolTable 19 {}) _lfortran_main_program [] [(SubroutineCall 1 test_round () [] ())] Source Public Implementation () .false. .false.), lpython_builtin: (IntrinsicModule lpython_builtin), main_program: (Program (SymbolTable 18 {}) main_program [] [(SubroutineCall 1 _lfortran_main_program () [] ())]), test_round: (Subroutine (SymbolTable 2 {f: (Variable 2 f Local () () Default (Real 8 []) Source Public Required .false.), round: (ExternalSymbol 2 round 4 round lpython_builtin [] round Private)}) test_round [] [(= (Var 2 f) (ConstantReal 5.678000 (Real 8 [])) ()) (Print () [(FunctionCall 2 round () [(Var 2 f)] [] (Integer 4 []) () ())]) (= (Var 2 f) (UnaryOp USub (ConstantReal 183745.230000 (Real 8 [])) (Real 8 []) (ConstantReal -183745.230000 (Real 8 []))) ()) (Print () [(FunctionCall 2 round () [(Var 2 f)] [] (Integer 4 []) () ())]) (= (Var 2 f) (ConstantReal 44.340000 (Real 8 [])) ()) (Print () [(FunctionCall 2 round () [(Var 2 f)] [] (Integer 4 []) () ())]) (= (Var 2 f) (ConstantReal 0.500000 (Real 8 [])) ()) (Print () [(FunctionCall 2 round () [(Var 2 f)] [] (Integer 4 []) () ())]) (= (Var 2 f) (UnaryOp USub (ConstantReal 50.500000 (Real 8 [])) (Real 8 []) (ConstantReal -50.500000 (Real 8 []))) ()) (Print () [(FunctionCall 2 round () [(Var 2 f)] [] (Integer 4 []) () ())]) (= (Var 2 f) (ConstantReal 1.500000 (Real 8 [])) ()) (Print () [(FunctionCall 2 round () [(Var 2 f)] [] (Integer 4 []) () ())]) (Assert (Compare (FunctionCall 2 round () [(ConstantReal 13.001000 (Real 8 []))] [] (Integer 4 []) (ConstantInteger 13 (Integer 4 [])) ()) Eq (ConstantInteger 13 (Integer 4 [])) (Logical 4 []) (ConstantLogical .true. (Logical 4 [])) ()) ()) (Assert (Compare (FunctionCall 2 round () [(UnaryOp USub (ConstantReal 40.499990 (Real 8 [])) (Real 8 []) (ConstantReal -40.499990 (Real 8 [])))] [] (Integer 4 []) (ConstantInteger -40 (Integer 4 [])) ()) Eq (UnaryOp USub (ConstantInteger 40 (Integer 4 [])) (Integer 4 []) (ConstantInteger -40 (Integer 4 []))) (Logical 4 []) (ConstantLogical .true. (Logical 4 [])) ()) ()) (Assert (Compare (FunctionCall 2 round () [(ConstantReal 0.500000 (Real 8 []))] [] (Integer 4 []) (ConstantInteger 0 (Integer 4 [])) ()) Eq (ConstantInteger 0 (Integer 4 [])) (Logical 4 []) (ConstantLogical .true. (Logical 4 [])) ()) ()) (Assert (Compare (FunctionCall 2 round () [(UnaryOp USub (ConstantReal 0.500000 (Real 8 [])) (Real 8 []) (ConstantReal -0.500000 (Real 8 [])))] [] (Integer 4 []) (ConstantInteger 0 (Integer 4 [])) ()) Eq (ConstantInteger 0 (Integer 4 [])) (Logical 4 []) (ConstantLogical .true. (Logical 4 [])) ()) ()) (Assert (Compare (FunctionCall 2 round () [(ConstantReal 1.500000 (Real 8 []))] [] (Integer 4 []) (ConstantInteger 2 (Integer 4 [])) ()) Eq (ConstantInteger 2 (Integer 4 [])) (Logical 4 []) (ConstantLogical .true. (Logical 4 [])) ()) ()) (Assert (Compare (FunctionCall 2 round () [(ConstantReal 50.500000 (Real 8 []))] [] (Integer 4 []) (ConstantInteger 50 (Integer 4 [])) ()) Eq (ConstantInteger 50 (Integer 4 [])) (Logical 4 []) (ConstantLogical .true. (Logical 4 [])) ()) ()) (Assert (Compare (FunctionCall 2 round () [(ConstantReal 56.780000 (Real 8 []))] [] (Integer 4 []) (ConstantInteger 57 (Integer 4 [])) ()) Eq (ConstantInteger 57 (Integer 4 [])) (Logical 4 []) (ConstantLogical .true. (Logical 4 [])) ()) ())] Source Public Implementation () .false. .false.)}) [])

0 commit comments

Comments
 (0)