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

Skip to content

a[i] * a[i] + sqrt(b) != a[i]**2 + sqrt(b) within 1e-6 #2072

Open
@rebcabin

Description

@rebcabin

I found a range of values where the differences are possibly significant. This is an adaptation of integration_tests/array_expr02.py, testing a larger range of values

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


# Issue 2066
n = TypeVar("n")


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


def verify(a: f32[:], b: f32[:], result: f32[:], size: i32):
    i: i32
    eps: f32
    eps = f32(1e-6)

    for i in range(size):
        check : f32 = 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: f32[8192] = empty(HDC_DIM, dtype=float32)
    b: f32[8192] = empty(HDC_DIM, dtype=float32)
    c: f32[8192] = empty(HDC_DIM, dtype=float32)

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

    # print(a)

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

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


if __name__ == "__main__":
    print("Module HDC")
    f()
/Users/brian/CLionProjects/lpython/src/bin/python /Users/brian/CLionProjects/lpython/lasr/LP-pycharm/hdc.py 
Module HDC
a[4113] = 4.11300000e+03, b[4113] = 4.11800000e+03
a[4113] * a[4113] + sqrt(b[4113]) = 1.69168320e+07
a[4113] ** 2      + sqrt(b[4113]) = 1.69168340e+07

a[4115] = 4.11500000e+03, b[4115] = 4.12000000e+03
a[4115] * a[4115] + sqrt(b[4115]) = 1.69332880e+07
a[4115] ** 2      + sqrt(b[4115]) = 1.69332900e+07

a[4117] = 4.11700000e+03, b[4117] = 4.12200000e+03
a[4117] * a[4117] + sqrt(b[4117]) = 1.69497520e+07
a[4117] ** 2      + sqrt(b[4117]) = 1.69497540e+07

... 

a[5787] = 5.78700000e+03, b[5787] = 5.79200000e+03
a[5787] * a[5787] + sqrt(b[5787]) = 3.34894440e+07
a[5787] ** 2      + sqrt(b[5787]) = 3.34894460e+07

a[5789] = 5.78900000e+03, b[5789] = 5.79400000e+03
a[5789] * a[5789] + sqrt(b[5789]) = 3.35125960e+07
a[5789] ** 2      + sqrt(b[5789]) = 3.35125980e+07

a[5791] = 5.79100000e+03, b[5791] = 5.79600000e+03
a[5791] * a[5791] + sqrt(b[5791]) = 3.35357560e+07
a[5791] ** 2      + sqrt(b[5791]) = 3.35357580e+07

a[5792] = 5.79200000e+03, b[5792] = 5.79700000e+03
a[5792] * a[5792] + sqrt(b[5792]) = 3.35473400e+07
a[5792] ** 2      + sqrt(b[5792]) = 3.35473400e+07

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions