Skip to content

Bug: Unary Negation (-a) on ak.uint64 Does Not Match NumPy Semantics #5117

Closed

Description

@ajpotts

Bug: Unary Negation (-a) on ak.uint64 Does Not Match NumPy Semantics

Summary

Arkouda’s implementation of unary negation (-a) for unsigned 64-bit arrays (ak.uint64) does not match NumPy behavior.
NumPy defines unary negation for unsigned integers as a modular wrap-around operation in the same unsigned dtype:

np.uint64(x) → (-x) % 2**64

Arkouda instead returns a signed int64 result with negative values, breaking NumPy alignment tests and producing incorrect, dtype-inconsistent output.

This causes 100% mismatches in:

test_unary_ops_alignment[neg-neg-uint64]

Expected NumPy Behavior

In NumPy, unary negation on uint64:

  • Preserves dtype (uint64)
  • Computes result modulo 2⁶⁴:
>>> import numpy as np
>>> x = np.uint64(5)
>>> -x
18446744073709551611   # 2**64 - 5
>>> np.array([-x], dtype=np.uint64)
array([18446744073709551611], dtype=uint64)

Arkouda’s Current Behavior

Arkouda produces signed negative values instead of uint64 wraparound:

>>> import arkouda as ak
>>> a = ak.array([5], dtype=ak.uint64)
>>> (-a).to_ndarray()
array([-5])   # WRONG — treated as signed int64

Across random arrays, every element differs from NumPy:

[left]:  array([-1830621171, -1957544328, -1935796560, ...])
[right]: [18446744071878930445, 18446744071752007288, ...]

These pairs differ by exactly 2**64, proving Arkouda is performing signed negation, not uint64 modulo negation.

This leads to:

  • 100% mismatch rate in alignment tests
  • Incorrect dtype (signed instead of unsigned)
  • Incorrect numerical results

Why This Matters

1. NumPy Compatibility

Arkouda aims to match NumPy semantics for distributed arrays.
Unsigned negation is a well-defined operation in NumPy and widely relied upon.

2. Dtype Preservation

Users expect unary operations to preserve dtype unless NumPy explicitly promotes.
Arkouda violates this expectation.

3. Downstream Bitwise Logic

Unsigned wraparound negation is used in hashing, bitboard logic, cryptographic ops, bloom filters, etc. Negation returning signed incorrect values breaks these algorithms.


Root Cause

Arkouda appears to interpret uint64 arrays as signed when applying unary minus:

  • Likely calling a signed -x operator on the underlying Chapel array.
  • No modulo operation or dtype preservation is performed.
  • Result pdarray inherits the backend’s signed numerical interpretation.

NumPy, in contrast, uses fixed-width modular arithmetic for all unsigned integer operations.


Proposed Fix

Correct Implementation for Unary Minus on ak.uint64:

(-x) → (2**64 - x) % 2**64

Backend logic should:

  1. Detect dtype == uint64

  2. Compute modular negation:

    const mask:uint(64) = 0xFFFFFFFFFFFFFFFF;
    result = (~x + 1) & mask;   // equivalent to (-x) mod 2**64
  3. Return a uint64 pdarray, preserving dtype.


Alternative (If Full Support Is Undesired)

If Arkouda chooses not to support unary negation for unsigned integers (less ideal for NumPy parity), then:

  • Raise a TypeError("Unary minus not supported for uint64")
  • Mirror NumPy error semantics for unsupported operations (if any)

But given NumPy does define the operation, best practice is to implement it correctly.


Acceptance Criteria

  • Unary negation on ak.uint64 matches NumPy exactly:

    assert_arkouda_array_equivalent(-a_ak, -a_np)
  • Result dtype remains uint64

  • Modular negation works for all values, including large ones

  • Alignment test:

    test_unary_ops_alignment[neg-neg-uint64]
    

    passes without modification.


Minimal Reproduction

>>> import numpy as np, arkouda as ak

>>> a_np = np.random.randint(0, 2**64, 5, dtype=np.uint64)
>>> a_ak = ak.array(a_np)

>>> np_res = -a_np
>>> ak_res = (-a_ak).to_ndarray()

>>> print(np_res)
>>> print(ak_res)

Expected: arrays match exactly
Actual: Arkouda produces negative signed values


Conclusion

Unary negation on uint64 is currently misimplemented.
Arkouda must use unsigned modular negation rather than signed negation to achieve full NumPy compatibility and correct numeric behavior.

Activity

  1. added
    bug
    on Dec 5, 2025
  2. added this to the Numpy Alignment milestone on Dec 5, 2025
  3. added
    blocking
    on Dec 5, 2025
  4. self-assigned this
    on Dec 5, 2025
  5. added 6 commits that reference this issue on Dec 5, 2025
    f18a460
    f2dd364
    e8cfbeb
    5c55e5b
    a488c3d
    3bf3845
  6. added 6 commits that reference this issue on Dec 11, 2025
    be092a8
    740441f
    039d92a
    927fab9
    a4cb7e3
    a41e98e
  7. added a commit that references this issue on Dec 19, 2025
    2970c70
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

blockingbug

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions