Skip to content

FloatingPointError: overflow encountered in scalar multiply when reading ASCII with numpy 2.2.6 #1523

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

Open
baptistelabat-syroco opened this issue May 27, 2025 · 0 comments

Comments

@baptistelabat-syroco
Copy link

Hi all. Here is a bug which appeared when switching from numpy 1.x to numpy 2.2.6. It can be created with the basic example from doc

    import meshio
    points = [
        [0.0, 0.0],
        [1.0, 0.0],
        [0.0, 1.0],
        [1.0, 1.0],
        [2.0, 0.0],
        [2.0, 1.0],
    ]
    cells = [
        ("triangle", [[0, 1, 2], [1, 3, 2]]),
        ("quad", [[1, 4, 5, 3]]),
    ]

    mesh = meshio.Mesh(
        points,
        cells,
        # Optionally provide extra data on points, cells, etc.
        point_data={"T": [0.3, -1.2, 0.5, 0.7, 0.0, -3.0]},
        # Each item in cell data must match the cells array
        cell_data={"a": [[0.1, 0.2], [0.4]]},
    )
    mesh.write(
        "test_mesh.stl",  # str, os.PathLike, or buffer/open file
        # file_format="vtk",  # optional if first argument is a path; inferred from extension
    )
    meshio.read("test_mesh.stl", file_format="stl")

Thre error raises here
https://github.com/nschloe/meshio/blame/b2ee99842e119901349fdeee06b5bf61e01f450a/src/meshio/stl/_stl.py#L40

def read(filename):
    with open_file(filename, "rb") as f:
        # Checking if the file is ASCII format is normally done by checking if the
        # first 5 characters of the header is "solid".
        # ```
        # header = f.read(80).decode()
        # ```
        # Unfortunately, there are mesh files out there which are binary and still put
        # "solid" there.
        # A suggested alternative is to pretend the file is binary, read the
        # num_triangles and see if it matches the file size
        # (https://stackoverflow.com/a/7394842/353337).
        filesize_bytes = os.path.getsize(filename)
        if filesize_bytes < 80:
            return _read_ascii(f)

        f.read(80)
        num_triangles = np.fromfile(f, count=1, dtype="<u4")[0]
        # for each triangle, one has 3 float32 (facet normal), 9 float32 (facet),
        # and 1 int16 (attribute count), 50 bytes in total
      if 84 + num_triangles * 50 == filesize_bytes:

E FloatingPointError: overflow encountered in scalar multiply

Reading ASCII file as a binary looks like a dangerous thing. A try catch block should be able to deal with issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant