Repository navigation
Description
Bug Report: Integer Floor Division (//) Misalignment Between Arkouda and NumPy
Summary
Arkouda’s integer floor-division (//) between an ak.pdarray (dtype int64) and a scalar does not match NumPy’s results.
This produces widespread discrepancies in alignment tests and breaks algorithms that rely on mathematically correct floor-division semantics, particularly when negative values are involved.
NumPy uses true mathematical floor division:
x // y = floor(x / y)
Arkouda currently appears to use C-style truncation toward zero, leading to incorrect results for negative integers.
Example (no reference to unit tests)
>>> import numpy as np
>>> import arkouda as ak
# Create values that cross zero
>>> arr_np = np.array([-9, -7, -5, -1, 0, 1, 5, 7, 9], dtype=np.int64)
>>> arr_ak = ak.array(arr_np)
>>> d = 3 # positive divisorExpected behavior (NumPy / Python)
NumPy uses true floor-division:
>>> arr_np // d
array([-3, -3, -2, -1, 0, 0, 1, 2, 3])Explanation:
- -7 / 3 ≈ -2.333 → floor → -3
- -1 / 3 ≈ -0.333 → floor → -1
Arkouda’s current behavior (incorrect)
>>> (arr_ak // d).to_ndarray()
array([-3, -2, -1, 0, 0, 0, 1, 2, 3])Notice mismatches:
| Value | NumPy | Arkouda (today) | Error |
|---|---|---|---|
| -7 // 3 | -3 | -2 | wrong |
| -5 // 3 | -2 | -1 | wrong |
| -1 // 3 | -1 | 0 | wrong |
Arkouda is performing truncation toward zero rather than flooring.
Why this matters
Python, NumPy, and pandas all guarantee:
x // y is the mathematical floor of the division
Many algorithms rely on this:
- bucketization
- hashing
- range partitioning
- geometric tiling
- coordinate transforms
Incorrect floor division produces invalid bin IDs, negative bucket indices turning positive, and broken partition boundaries.
Example:
value = -7
bucket_width = 3
# Python
>>> -7 // 3
-3
# Arkouda (current)
>>> ak.array([-7]) // 3
array([-2]) # WRONGThis shifts entire buckets one index off.
Root cause
Arkouda is using C/C++-style division semantics:
result = trunc(x / y)
instead of:
result = floor(x / y)
For positive values they coincide; for negative values they diverge.
Proposed Fix
Implement Python/NumPy-compatible floor division:
x // y = floor(x / y)
A correct scalar implementation for integer division is:
def py_floor_div(x, y):
q = x // y # using Python semantics, not C
return qA C-style remainder-based correction:
# Given q = trunc(x / y), r = x % y
# Adjust q when signs differ and remainder != 0
if (r != 0) and ((x < 0) != (y < 0)):
q -= 1Arkouda should implement this logic at the pdarray operation level.
Acceptance Criteria
- Arkouda
ak.int64 // scalarmatches NumPy for all signs of numerator and denominator - No truncation-toward-zero for negative values
- Roundtrip example above produces identical results between Arkouda and NumPy
- Scalar–array and array–scalar both fixed
- Behavior consistent across:
ak.int64ak.uint64(where meaningful)- future integer types
Example for documentation after fix
>>> import arkouda as ak
>>> import numpy as np
>>> arr = ak.array([-9, -7, -5, -1, 0, 1, 5, 7, 9])
>>> (arr // 3).tolist()
[-3, -3, -2, -1, 0, 0, 1, 2, 3]This should exactly match:
>>> (np.array([-9, -7, -5, -1, 0, 1, 5, 7, 9]) // 3).tolist()
[-3, -3, -2, -1, 0, 0, 1, 2, 3]Additional context
Debug lines such as:
[pdarrayclass] DEBUG: created Chapel array ...
[pdarrayclass] DEBUG: deleting pdarray ...
are normal lifecycle logs and not related to the numerical mismatch.
The underlying issue is the integer division semantics in Arkouda.
Activity
Closes 5113, fixing behavior of floor division operator (//) (#5131)