Skip to content

Negative zero not preserved #3144

Closed

Description

@tomwhite

Zarr version

v3.0.8

Numcodecs version

v0.15.1

Python Version

3.11

Operating System

Mac

Installation

pip in a venv

Description

Negative zero (-0.0) is not distinguished from zero when reading back values from a Zarr array. This differs from the expected behaviour (and Zarr Python v2).

Steps to reproduce

# /// script
# requires-python = ">=3.11"
# dependencies = [
#   "zarr@git+https://github.com/zarr-developers/zarr-python.git@main",
# ]
# ///
#
# This script automatically imports the development branch of zarr to check for issues

import numpy as np
import zarr
print(zarr.array(np.array([0.0, -0.0]))[:])
#zarr.print_debug_info()

This prints

[0. 0.]

But with Zarr 2.18.7 it correctly prints

[ 0. -0.]

Additional output

No response

Activity

  1. added
    bugPotential issues with the zarr-python library
    on Jun 18, 2025
  2. d-v-b commented on Jun 18, 2025

    @d-v-b
    Contributor

    thanks for this report, I did a quick check and this bug was present prior to the recent dtype changes, so that should help narrow the search down a bit.

  3. tomwhite commented on Jun 18, 2025

    @tomwhite
    MemberAuthor

    thanks for this report, I did a quick check and this bug was present prior to the recent dtype changes, so that should help narrow the search down a bit.

    Yes, sorry I forgot to say it is in 3.0.8 as well as main.

  4. d-v-b commented on Jun 18, 2025

    @d-v-b
    Contributor
    # /// script
    # requires-python = ">=3.11"
    # dependencies = [
    #   "zarr==3.0.8",
    # ]
    # ///
    #
    # This script automatically imports the development branch of zarr to check for issues
    
    import numpy as np
    import zarr
    print(zarr.array(np.array([0.0, -0.0]), fill_value=1)[:])

    Adding a non-0 fill value preserves the signed 0 as expected. I also checked that the original test case doesn't write any chunks. So the error comes from zarr mistakenly identifying the chunk as empty during array creation, thenfailing to write it, which results in zarr using the fill value instead on indexing.

  5. tomwhite commented on Jun 18, 2025

    @tomwhite
    MemberAuthor

    Aha - which explains why it works on v2 - the default for write_empty_chunks is True (it's False in v3). On v2 the following fails too:

    >>> print(zarr.array(np.array([0.0, -0.0]), write_empty_chunks=False)[:])
    [0. 0.]
    
  6. d-v-b commented on Jun 18, 2025

    @d-v-b
    Contributor

    I suspect we are relying on this behavior:

    >>> np.array([0.0]) == np.array([-0.0])
    array([ True])
    

    We probably need to define a stricter type of equality.

  7. added 4 commits that reference this issue on Jul 8, 2025
    d4c1205
    2745b68
    7d6d74b
    c4904e3
  8. added a commit that references this issue on Aug 6, 2025
    1264a4d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugPotential issues with the zarr-python library

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions