Skip to content

Fix integer precision loss in mysave for large meshes#23

Open
AmitSubhash wants to merge 4 commits into
nirfast-admin:masterfrom
AmitSubhash:fix/mysave-integer-precision
Open

Fix integer precision loss in mysave for large meshes#23
AmitSubhash wants to merge 4 commits into
nirfast-admin:masterfrom
AmitSubhash:fix/mysave-integer-precision

Conversation

@AmitSubhash
Copy link
Copy Markdown

Summary

  • mysave.m uses %g format to write all data, but %g defaults to 6 significant digits
  • For meshes with >1M nodes, element indices are silently truncated (e.g. 1932581 becomes 1.93258e+06 = 1932580)
  • After save_mesh/load_mesh round-trip, elements reference non-existent node indices
  • This causes triangulation errors in PlotMeshSurface and potentially corrupted light models

Root cause

%g with 6 significant digits cannot represent integers above ~999,999 exactly. Element files (.elem) contain node indices that must be exact integers.

Fix

Detect integer-valued data and use %d format instead of %g. Floating-point data (e.g. node coordinates in .node files) continues to use %g.

Reproduction

  1. Generate a mesh with >1M nodes using NirfastMesh_Region
  2. save_mesh then load_mesh
  3. max(mesh.elements(:)) > size(mesh.nodes, 1) -- should be false, but is true due to precision loss
  4. PlotMeshSurface fails with "The input triangulation is invalid"

Test plan

  • Verify .elem files use integer format (no scientific notation) after fix
  • Verify .node files still use floating-point format
  • Verify load_mesh round-trip preserves exact element indices for meshes with >1M nodes
  • Verify existing small meshes (<1M nodes) are unaffected

AmitSubhash and others added 4 commits June 3, 2025 17:36
…t-product-errors

I'M A GOD FOR FIXING THIS THIS QUICK HAHAHAHAHA
The %g format specifier defaults to 6 significant digits, which silently
truncates node indices above ~1e6 when writing .elem files. For example,
node index 1932581 is written as 1.93258e+06 and read back as 1932580.

This causes "invalid triangulation" errors in PlotMeshSurface when the
mesh has more than ~1 million nodes, because element indices reference
node positions that no longer exist after the round-trip through
save_mesh/load_mesh.

The fix detects integer-valued data and uses %d instead of %g, while
preserving %g for floating-point data like node coordinates.
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

Successfully merging this pull request may close these issues.

1 participant