Provide checked Euclidean quotient and remainder for signed grid coordinates #11

Closed
opened 2026-12-02 19:24:39 +00:00 by faumaray · 1 comment
Owner

We split signed cell coordinates into a page coordinate and a nonnegative offset. For width 32, cell −1 belongs to page −1 at offset 31, cell 0 is page 0 at offset 0, and cells 31 and 32 fall on opposite sides of the next boundary. This also applies to tilemaps and paged buffers.

The pinned Void::Numeric::checked_div validates overflow and zero divisors and otherwise follows C++ integer division. For −1 divided by 32 it returns 0. The Numeric tests cover an exact positive quotient, zero and signed minimum divided by −1. A separate checked Euclidean quotient/remainder for a positive divisor would support this caller while keeping the existing division contract. Numeric.hpp · Numeric tests · Consumer coordinate contract

A replacement check would verify that a = q*d + r mathematically and 0 <= r < d for positive d, including signed minima and maxima without an overflowing intermediate or negating the minimum. Cover −33, −32, −1, 0, 31, 32, 33 at width 32, extreme values at widths 1, 3 and 32, and invalid widths. Compile the same cases as constant expressions where supported and run them on Windows x64. Region identity, storage layout and game-specific limits can stay with the caller.

We split signed cell coordinates into a page coordinate and a nonnegative offset. For width 32, cell −1 belongs to page −1 at offset 31, cell 0 is page 0 at offset 0, and cells 31 and 32 fall on opposite sides of the next boundary. This also applies to tilemaps and paged buffers. The pinned `Void::Numeric::checked_div` validates overflow and zero divisors and otherwise follows C++ integer division. For −1 divided by 32 it returns 0. The Numeric tests cover an exact positive quotient, zero and signed minimum divided by −1. A separate checked Euclidean quotient/remainder for a positive divisor would support this caller while keeping the existing division contract. [Numeric.hpp](https://git.faumaray.com/Void/Library/src/commit/7b0fea6095f603f9e1904f66dfdc7752f4be4792/include/Void/Math/Numeric.hpp) · [Numeric tests](https://git.faumaray.com/Void/Library/src/commit/7b0fea6095f603f9e1904f66dfdc7752f4be4792/tests/standalone/Math/Numeric.cpp) · [Consumer coordinate contract](https://git.faumaray.com/Lagg1ng/stoneheart_at_home/src/commit/2a5df1a18b055adc71bd9d3defe2bec40c4496e7/docs/plan/systems/S02.md) A replacement check would verify that `a = q*d + r` mathematically and `0 <= r < d` for positive d, including signed minima and maxima without an overflowing intermediate or negating the minimum. Cover −33, −32, −1, 0, 31, 32, 33 at width 32, extreme values at widths 1, 3 and 32, and invalid widths. Compile the same cases as constant expressions where supported and run them on Windows x64. Region identity, storage layout and game-specific limits can stay with the caller.
Author
Owner

Implemented on branch issue/11-euclidean-division (local commit 5a4af3e).

Void::Numeric::checked_euclidean_div added: positive-divisor Euclidean quotient/remainder with nonnegative remainder, no signed-min overflow, constexpr coverage and exhaustive int8 tests.

Merge request: #27

Implemented on branch `issue/11-euclidean-division` (local commit `5a4af3e`). `Void::Numeric::checked_euclidean_div` added: positive-divisor Euclidean quotient/remainder with nonnegative remainder, no signed-min overflow, constexpr coverage and exhaustive int8 tests. Merge request: https://git.faumaray.com/Void/Library/pulls/27
Sign in to join this conversation.
No labels
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: Void/Library#11