Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

numpy floats are eagerly converted to sympy Floats #2505

Open
JDBetteridge opened this issue Dec 20, 2024 · 0 comments
Open

numpy floats are eagerly converted to sympy Floats #2505

JDBetteridge opened this issue Dec 20, 2024 · 0 comments
Labels
bug-C-minor bug in the generated code not affecting correctness

Comments

@JDBetteridge
Copy link
Contributor

See https://github.com/devitocodes/devito/blob/v4.8.11/tests/test_symbolics.py#L257-L260 for example of use.

Spitting out the generated code we can see that in the case where the constant value is an np.float64 sympy eagerly turns this into a Float losing the precision information. The generated code is explicitly using a 5.0e-1F single precision float rather that (presumably) the desired double precision float.

from devito import *
from devito.symbolics.inspection import sympy_dtype
from devito.symbolics import ccode
i1 = Dimension(name="i1")

for expr in [
    i1 - 1,
    i1 - 0.5,
    i1 - np.float32(0.5),
    i1 - np.float64(0.5),
    i1 - Constant('half', dtype=np.float64, default_value=0.5)
]:
    print(repr(expr))
    print(sympy_dtype(expr))
    print(ccode(Abs(expr)))
    print()

Produces:

i1 - 1
<class 'numpy.int32'>
abs(i1 - 1)

i1 - 0.5
<class 'numpy.int32'>
fabsf(i1 - 5.0e-1F)

i1 - 0.5
<class 'numpy.int32'>
fabsf(i1 - 5.0e-1F)

i1 - 0.5
<class 'numpy.int32'>
fabsf(i1 - 5.0e-1F)

i1 - half
<class 'numpy.float64'>
fabs(i1 - half)

Could probably be addressed alongside #2493 in a printer cleanup.

@JDBetteridge JDBetteridge added the bug-C-minor bug in the generated code not affecting correctness label Dec 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug-C-minor bug in the generated code not affecting correctness
Projects
None yet
Development

No branches or pull requests

1 participant