Skip to content

Make pictures independent of floating-point rounding (-O3) - #27

Merged
certik merged 1 commit into
mainfrom
fix-numerical-robustness
Sep 14, 2026
Merged

certik merged 1 commit into
mainfrom
fix-numerical-robustness

Conversation

@certik

@certik certik commented Sep 13, 2026 •

Copy link
Copy Markdown
Member

Builds on #26 (merged), which added the fingerprint tests; rebased onto main as a single commit.

With -O3 (gfortran or flang, where fused multiply-add changes rounding), fpm test failed six pictures, and four more had a colorbar tick moved by one pixel. Three causes:

  • 0 vs 0.0 tick labels (quiver3d, trisurf, mosaic, subplots_2x2, subplots_shared). The zero tick comes out of the locator as exactly 0 or about 1e-17, and format_tick_fixed sent the tiny value to the power-of-ten branch. Values within 1e-8 of zero are now formatted as zero, which is what matplotlib's ScalarFormatter does.
  • Contour label placement (clabel). label_window chose the straightest window with a strict <, so on straight runs (deviation 0 up to 1e-14) rounding decided. A later window now has to win by more than 1e-6.
  • Pixel snapping (colorbar ticks in imshow, interp, cmap_special, colorbar_orient, …). pb_snap used floor(x + 0.5) on values meant to be exactly x.5. It rounds to 1/65536 px first.

Verification (macOS, locally):

  • gfortran -O0 vs gfortran -O3 vs flang -O3: no visible difference in any of the 124 PNGs (only single intensity levels).
  • 9 pictures of the plain build changed (the new deterministic rounding goes the other way in those cases). All 9 are closer to matplotlib than before in the changed region (e.g. colorbar_orient 9.70 → 0.18/255, clabel 5.13 → 4.76), so their fingerprints are updated.
  • fpm test with gfortran, gfortran --profile release, flang -O3 -march=native, and the flang script build all pass. The PNG comparison against matplotlib passes (1.50/255). LFortran 0.66.0 passes as well.

CI: all steps pass, including Test with LFortran (0.66.0). New step pixi run test-flang-O3 (fpm test --compiler flang --flag '-O3 -march=native'), so a picture that depends on rounding fails in CI.

Base automatically changed from fingerprint-tests to main September 14, 2026 05:42
…unding

Built with -O3 (gfortran or flang, fused multiply-add allowed), six test
pictures differed visibly from the plain build, and four more had a tick
one pixel off:

- A tick meant to be at 0 comes out of the locator as exactly 0 or as
  about 1e-17, and format_tick_fixed sent the second to the power-of-ten
  branch, which writes "0" where the rest of the axis writes "0.0".
  Values within 1e-8 of zero are now zero, as in matplotlib's
  ScalarFormatter.
- label_window took the straightest window by a strict comparison, so on
  a straight run of a contour, where every window deviates by 0 up to
  1e-14, rounding chose where the label went. A later window now has to
  be straighter by more than 1e-6.
- pb_snap rounded with floor(x + 0.5), so a coordinate meant to be x.5
  snapped to either pixel. It is rounded to 1/65536 px first.

The plain and -O3 builds now differ only by single intensity levels. The
nine pictures whose plain output changed are all closer to matplotlib than
before, so their fingerprints are updated. CI runs the tests with flang
-O3 -march=native to keep it that way.
@certik
certik force-pushed the fix-numerical-robustness branch from e49ad21 to e363533 Compare September 14, 2026 05:44
@certik
certik merged commit ad02e87 into main Sep 14, 2026
1 check passed
@certik
certik deleted the fix-numerical-robustness branch September 14, 2026 05:58
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