lcompilers / lpython

Python compiler
https://lpython.org/
Other
1.5k stars 157 forks source link

Strange results with negative indices #2503

Closed rebcabin closed 5 months ago

rebcabin commented 7 months ago

See docstrings:

from lpython import (i16, i32, Const)
from numpy import empty, int16
dim: Const[i32] = 10

def foo():
    """Negative indices produce random results each run."""
    A: i16[dim] = empty((dim,), dtype=int16)
    ww: i32
    for ww in range(dim):
        A[ww] = i16(ww + 1)
    print(A[0], A[1], A[2], "...", A[-3], A[-2], A[-1])

def bar(dim_: i32):
    """Negative indices always produce zero when 'dim' is a parameter."""
    A: i16[dim_] = empty((dim_,), dtype=int16)
    ww: i32
    for ww in range(dim_):
        A[ww] = i16(ww + 1)
    print(A[0], A[1], A[2], "...", A[-3], A[-2], A[-1])

foo()
bar(10)
(lp) ┌─(~/Documents/GitHub/lpython/integration_tests)──────────────────────────────────────────────────────────────────────────────(brian@MacBook-Pro:s001)─┐
└─(10:27:56 on vector-backend ✹ ✭)──> lpython ../ISSUES/Issue2503.py                                                                     ──(Wed,Feb07)─┘
1 2 3 ... 0 -25536 611
1 2 3 ... 0 0 0
KhaledSayed04 commented 5 months ago

What are the expected results in the both?

Shaikh-Ubaid commented 5 months ago

What are the expected results in the both?

The expected results are same as that of CPython.

% cat examples/expr2.py   
from lpython import (i16, i32, Const)
from numpy import empty, int16
dim: Const[i32] = 10

def foo():
    """Negative indices produce random results each run."""
    A: i16[dim] = empty((dim,), dtype=int16)
    ww: i32
    for ww in range(dim):
        A[ww] = i16(ww + 1)
    print(A[0], A[1], A[2], "...", A[-3], A[-2], A[-1])

def bar(dim_: i32):
    """Negative indices always produce zero when 'dim' is a parameter."""
    A: i16[dim_] = empty((dim_,), dtype=int16)
    ww: i32
    for ww in range(dim_):
        A[ww] = i16(ww + 1)
    print(A[0], A[1], A[2], "...", A[-3], A[-2], A[-1])

foo()
bar(10)
% python examples/expr2.py
1 2 3 ... 8 9 10
1 2 3 ... 8 9 10