Skip to content

Integer dtype scalars truncate // and % for negative operands and panic on zero or MIN // -1 #1364

Open

Description

@ciaranra

The integer dtype scalars (pc.i8 … pc.i64, pc.u8 … pc.u64) implement // and % with Rust's truncating / and %. For negative operands they silently return results that differ from Python and NumPy. Division by zero and the signed MIN // -1 overflow panic, which surfaces as PanicException.

Reproduction

Expression pecos_rslib.dtypes NumPy 2
i64(-7) // 2 -3 -4
i64(-7) % 2 -1 1
7 // i64(-2) -3 -4
i32(7) % -2 1 -1
i64(1) // 0 PanicException: attempt to divide by zero 0 (RuntimeWarning)
u8(1) % 0 PanicException: attempt to calculate the remainder with a divisor of zero 0 (RuntimeWarning)
i64(-2**63) // -1 PanicException: attempt to divide with overflow -9223372036854775808
i8(-128) % -1 PanicException: attempt to calculate the remainder with overflow 0

Cause

python/pecos-rslib/src/dtypes.rs: every integer scalar's __floordiv__, __rfloordiv__, __mod__ and __rmod__ compute self.value / other_value or self.value % other_value (for example ScalarI64::__floordiv__, ~3902 on dev; ScalarU8::__mod__, ~1077). Rust / truncates toward zero and % takes the sign of the dividend, while Python and NumPy floor and take the sign of the divisor. The unchecked operators also panic on a zero divisor and on MIN / -1. The same file already uses wrapping_add/wrapping_sub for +/-, so it intends NumPy-style wrapping integer semantics.

Direction

Implement floor division and modulo with Python/NumPy semantics for every signed type (div_euclid is not the same: it differs from floor division for negative divisors), wrapping MIN // -1 like NumPy. Decide deliberately on division by zero: NumPy returns 0 with a RuntimeWarning, Python ints raise ZeroDivisionError. Either is fine, but never a panic. Add tests against NumPy for every integer type, both operand orders, and the zero and overflow cases.

Found while preparing #1342 (float dtype scalar arithmetic).

Activity

  1. added
    bugSomething isn't working
    rustPull requests that update rust code
    pythonPull requests that update python code
    severity:criticalSilently wrong results, data loss, or security exposure
    on Oct 18, 2026
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

    bugSomething isn't workingpythonPull requests that update python coderustPull requests that update rust codeseverity:criticalSilently wrong results, data loss, or security exposure

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions