Skip to content

feat: custom float precision in the C writer (closes #22) - #34

Merged
jameskermode merged 1 commit into
masterfrom
fix/c-writer-precision
Jun 9, 2026
Merged

jameskermode merged 1 commit into
masterfrom
fix/c-writer-precision

Conversation

@jameskermode

Copy link
Copy Markdown
Member

What

ASE's default 8-decimal extxyz output truncates positions, which breaks hash-equality of Atoms before/after a write (#22, and the linked abcd issue). The pure-Python writer already honored a format_dict, but the C writer rejected it:

write_dicts(out, frames, use_cextxyz=True, format_dict={'R': '%.16g'})
# -> ValueError: C writer does not support custom format strings

Now it works on both backends, and a high-precision format round-trips:

format max round-trip error on a 15-sig-fig position
default %16.8f ~1e-9 (truncated)
%.16g < 1e-12

How (ABI-safe)

extxyz_write_ll's signature is used by the Fortran bindings and the standalone lib, so I didn't change it. Instead:

  • New extxyz_write_ll_fmt(fp, nat, info, arrays, fmt_i, fmt_f, fmt_b, fmt_s) — any format NULL falls back to the compiled-in default (FLOAT_FMT etc.).
  • extxyz_write_ll becomes a one-line wrapper calling it with all-NULL.
  • The formats apply to the per-atom data columns, matching the pure-Python writer's per-type format_dict (keyed R/I/S/L); info-line values keep default formatting (the pure-Python writer is the same).
  • Threaded through cextxyz.write_frame_dicts → core.write_dicts; the C-path ValueError is removed. Exported in _extxyz.def for Windows.

Verification

  • tests/test_write_precision.py (rtol=0): custom precision round-trips < 1e-12 and strictly beats the default; default output is byte-identical to before; no ValueError.
  • Core suite (24) + ase-extxyz suite (38) green; leaks clean.

Closes #22.

🤖 Generated with Claude Code

ASE's default 8-decimal output truncates positions, breaking hash-equality of
Atoms across a write (issue #22). The pure-Python writer already accepted a
`format_dict`; the C path raised `ValueError`.

Add `extxyz_write_ll_fmt(fp, nat, info, arrays, fmt_i, fmt_f, fmt_b, fmt_s)`
(any NULL -> compiled-in default) and make `extxyz_write_ll` a thin wrapper, so
the existing ABI (used by the Fortran bindings and standalone lib) is unchanged.
The formats apply to the per-atom data columns, matching the pure-Python
writer's per-type `format_dict` (keyed R/I/S/L). Threaded through
`cextxyz.write_frame_dicts` and `core.write_dicts`; the C-writer ValueError is
gone, so `write_dicts(..., format_dict={'R': '%.16g'})` now works on both
backends. Exported in _extxyz.def for Windows.

Tests: high-precision format round-trips (<1e-12) and beats the default
(~1e-9); default output byte-identical; no ValueError. Core + ase-extxyz suites
and `leaks` clean.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@jameskermode
jameskermode merged commit 4a4ed6a into master Jun 9, 2026
26 checks passed
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.

Precision specification when writing to extxyz

1 participant