Skip to content

fix: preserve non-canonical NaN payloads and distinguish -0.0 in v3 metadata - #4702

Open
barlowa124 wants to merge 6 commits into
zarr-developers:mainfrom
barlowa124:float-fill-4453
Open

barlowa124 wants to merge 6 commits into
zarr-developers:mainfrom
barlowa124:float-fill-4453

Conversation

@barlowa124

Copy link
Copy Markdown
Contributor

Supersedes #4486, which GitHub auto-closed when the branch history was rewritten to remove tooling attribution.

Summary

Third item of #4453: float fill values holding non-canonical NaN
payloads did not round-trip, and ArrayV3Metadata.__eq__ conflated
documents whose fill_values serialize differently.

Parse to serialize dropped NaN payloads. "0x7fc00001" parsed fine,
but float_to_json_v3 flattened every NaN to "NaN" on write. It now
emits the hex bit pattern for any NaN differing from the canonical
positive one, which is the case the spec's hex encoding names. The old
comment claiming NumPy cannot hold distinct NaNs was wrong. The
payload bits were being discarded on write, not absent. The canonical
check compares all NaN bits including sign, matching zarrs and
tensorstore. In the same place, float_from_json_v3 decoded hex into
Python's 64-bit float and narrowed float16/float32 payloads. Hex
now decodes through np.frombuffer into a same-width scalar, so all
three widths round-trip.

Equality conflated distinct documents. __eq__ compared to_dict()
dictionaries, where negative zero equals positive zero and every NaN
equaled "NaN". __eq__ now compares json.dumps(to_dict(), sort_keys=True). That is the string __hash__ already hashes, so
equal implies equal-hash by construction.

For reviewers

float_from_json_v3 returns np.floating for hex input rather than
float. Both call sites consume it through a cast or complex(), so
callers see no change. "NaN" still denotes the canonical positive
NaN per width. v2 serialization is untouched. Complex fills route
through the same helpers per component, so a complex64 real part with
a non-canonical payload now round-trips as hex as well. That path
was exercised but has no dedicated test. Payload bits survive only when the hex
width matches the dtype width, which the spec requires anyway.

Author attestation

  • I am a human. These are my changes. I have reviewed and understood every change and can explain why each is correct.

TODO

Done:

  • Add unit tests and/or doctests in docstrings
  • Add docstrings and API docs for any new/modified user-facing classes and functions
  • Changes documented as a new file in changes/

Outstanding:

  • New/modified features documented in docs/user-guide/*.md (no user-facing behavior beyond spec compliance)
  • GitHub Actions have all passed
  • Test coverage is 100% (Codecov passes)

barlowa124 and others added 6 commits December 4, 2026 17:20
…etadata

float_from_json_v3 decoded hex strings through Python float64, which
cannot carry a float16 payload and risks narrowing float32 payloads;
decode into a same-width NumPy scalar via np.frombuffer instead.
float_to_json_v3 flattened every NaN to "NaN" on write; emit the hex
bit pattern for NaNs that are not the canonical positive NaN so the
document round-trips the payload it parsed.

ArrayV3Metadata.__eq__ compared to_dict() dictionaries, under which
-0.0 == 0.0 and (before this change) any NaN payload == "NaN" even
though the documents serialize differently. Compare the canonical
serialized JSON text instead, which is also the exact form __hash__
already hashes, making the two consistent by construction.

Partial fix for zarr-developers#4453
The strict bits==canonical check serialized float32 0xffc00000 as hex,
but the canonical NaN designation covers the payload, not the sign; mask
the sign bit so 0xffc00000 writes as "NaN" like the canonical form.

This branch has not been deployed

No deployments
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