Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Expected type mismatch between LPython f64 and numpy float32 but didn't get it #2082

Open
@rebcabin

Description

@rebcabin

Consider the following, which allocates an array of f64 (using the workaround from Issue #2072) from numpy, but uses numpy.float32 instead of numpy.float64. LPython does not detect a type mismatch, but I thought it might. This is a question rather than a bug.

from lpython import i8, i32, i64, f32, f64, TypeVar, Const
from numpy import empty, sqrt, float32


# Issue 2066
n = TypeVar("n")


def modify(b: f64[:], n: i32) -> f64[n]:
    return sqrt(b)


# Issue 2072; workaround is f64 instead of f32
def verify(a: f64[:], b: f64[:], result: f64[:], size: i32):
    i: i32
    eps: f64
    eps = f64(1e-6)

    for i in range(size):
        check : f64 = abs(a[i] * a[i] + sqrt(b[i]) - result[i])

        if not check <= eps or i == 5792:
            print("a[", end='')
            print(i, end='')
            print("] = ", end='')
            print(a[i], end='')
            print(", b[", end='')
            print(i,end='')
            print("] = ", end='')
            print(b[i])
            print("a[", end='')
            print(i, end='')
            print("] * a[", end='')
            print(i, end='')
            print("] + sqrt(b[", end='')
            print(i, end='')
            print("]) = ", end='')
            print(a[i] * a[i] + sqrt(b[i]))
            print("a[", end='')
            print(i, end='')
            print("] ** 2      + sqrt(b[", end='')
            print(i, end='')
            print("]) = ", end='')
            print(result[i])
            print('')

        # assert check <= eps


def f():
    i: i32
    j: i32

    HDC_DIM : Const[i32] = 8192
    a: f64[8192] = empty(HDC_DIM, dtype=float32)  ########## ATTENTION ##########
    b: f64[8192] = empty(HDC_DIM, dtype=float32)
    c: f64[8192] = empty(HDC_DIM, dtype=float32)

    for i in range(HDC_DIM):
        a[i] = f64(i)

    # print(a)

    for j in range(HDC_DIM):
        b[j] = f64(j + 5)

    c = a ** f64(2) + modify(b, HDC_DIM)
    verify(a, b, c, HDC_DIM)


if __name__ == "__main__":
    print("Module HDC")
    f()```

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingquestionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions