Skip to content

numpy floats are eagerly converted to sympy Floats #2505

Open

Description

@JDBetteridge

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 than (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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug-C-minorbug in the generated code not affecting correctness

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions