Checked Euclidean division for signed grid coordinates (#11) #27

Merged
faumaray merged 2 commits from issue/11-euclidean-division into main 2026-12-03 08:08:27 +00:00
2 changed files with 157 additions and 0 deletions

No files matched your search

+41
View File
@@ -298,6 +298,47 @@ template <std::integral T>
return a / b;
}
/// Euclidean quotient and remainder for a strictly positive divisor:
/// `a == quotient * divisor + remainder` and `0 <= remainder < divisor`.
template <std::integral T> struct EuclideanDivision {
T quotient{};
T remainder{};
[[nodiscard]] constexpr bool
operator==(const EuclideanDivision&) const noexcept = default;
};
/// Checked Euclidean division. Unlike C++ integer division (which truncates
/// toward zero), this floors toward negative infinity so the remainder is
/// always nonnegative, e.g. (-1, 32) -> (-1, 31).
///
/// `divisor == 0` reports `DivisionByZero`; a negative divisor reports
/// `InvalidInput`. The signed minimum is handled without negating it, so no
/// intermediate overflows.
template <std::integral T>
[[nodiscard]] constexpr std::expected<EuclideanDivision<T>, Error>
checked_euclidean_div(T a, T divisor) noexcept
{
if (divisor == T{0}) {
return std::unexpected{Error::DivisionByZero};
}
if constexpr (std::is_signed_v<T>) {
if (divisor < T{0}) {
return std::unexpected{Error::InvalidInput};
}
}
T quotient = static_cast<T>(a / divisor);
T remainder = static_cast<T>(a % divisor);
if constexpr (std::is_signed_v<T>) {
if (remainder < T{0}) {
// Truncated division rounded toward zero: step one page down.
quotient = static_cast<T>(quotient - T{1});
remainder = static_cast<T>(remainder + divisor);
}
}
return EuclideanDivision<T>{.quotient = quotient, .remainder = remainder};
}
template <std::integral T>
[[nodiscard]] constexpr std::expected<T, Error>
checked_neg(T a) noexcept
+116
View File
@@ -505,3 +505,119 @@ Void::Testing::Test portable_overflow_helpers_boundary_cases()
Void::Testing::expect(portable_mul_overflow<std::uint64_t>(
std::numeric_limits<std::uint64_t>::max(), 2ULL, &u64));
}
// ── checked_euclidean_div ────────────────────────────────────────────────────
Void::Testing::Test checked_euclidean_div_examples_width_32()
{
using Void::Numeric::checked_euclidean_div;
const auto check = [](std::int32_t a, std::int32_t q, std::int32_t r) {
const auto result = checked_euclidean_div(a, std::int32_t{32});
Void::Testing::require(result.has_value());
Void::Testing::expect_eq(result->quotient, q);
Void::Testing::expect_eq(result->remainder, r);
};
check(-33, -2, 31);
check(-32, -1, 0);
check(-1, -1, 31);
check(0, 0, 0);
check(31, 0, 31);
check(32, 1, 0);
check(33, 1, 1);
// The existing truncating contract is intentionally unchanged.
Void::Testing::expect_eq(*Void::Numeric::checked_div(-1, 32), 0);
}
Void::Testing::Test checked_euclidean_div_is_constexpr()
{
using Void::Numeric::checked_euclidean_div;
static_assert(checked_euclidean_div(-1, 32).value().quotient == -1);
static_assert(checked_euclidean_div(-1, 32).value().remainder == 31);
static_assert(checked_euclidean_div(-33, 32).value().quotient == -2);
static_assert(checked_euclidean_div(31, 32).value().quotient == 0);
static_assert(checked_euclidean_div(32, 32).value().remainder == 0);
Void::Testing::expect(true);
}
Void::Testing::Test checked_euclidean_div_exhaustive_int8()
{
bool ok = true;
for (int a = -128; a <= 127 && ok; ++a) {
for (int d = 1; d <= 127 && ok; ++d) {
const auto result = Void::Numeric::checked_euclidean_div(
static_cast<std::int8_t>(a), static_cast<std::int8_t>(d));
ok = result.has_value();
if (ok) {
const int q = result->quotient;
const int r = result->remainder;
ok = r >= 0 && r < d && q * d + r == a;
}
}
}
Void::Testing::expect(ok);
}
Void::Testing::Test checked_euclidean_div_extremes()
{
using Void::Numeric::checked_euclidean_div;
const std::int32_t divisors[] = {1, 3, 32};
bool ok = true;
for (const auto d : divisors) {
const std::int32_t values[] = {
std::numeric_limits<std::int32_t>::min(),
std::numeric_limits<std::int32_t>::max()};
for (const auto a : values) {
const auto result = checked_euclidean_div(a, d);
ok = ok && result.has_value();
if (ok) {
const std::int64_t q = result->quotient;
const std::int64_t r = result->remainder;
ok = r >= 0 && r < d && q * d + r == a;
}
}
}
Void::Testing::expect(ok);
const auto min32 = checked_euclidean_div(
std::numeric_limits<std::int64_t>::min(), std::int64_t{32});
Void::Testing::require(min32.has_value());
Void::Testing::expect_eq(min32->quotient,
std::int64_t{-288230376151711744});
Void::Testing::expect_eq(min32->remainder, std::int64_t{0});
const auto min3 = checked_euclidean_div(
std::numeric_limits<std::int64_t>::min(), std::int64_t{3});
Void::Testing::require(min3.has_value());
Void::Testing::expect_eq(min3->quotient,
std::int64_t{-3074457345618258603});
Void::Testing::expect_eq(min3->remainder, std::int64_t{1});
const auto max32 = checked_euclidean_div(
std::numeric_limits<std::int64_t>::max(), std::int64_t{32});
Void::Testing::require(max32.has_value());
Void::Testing::expect_eq(max32->quotient,
std::int64_t{288230376151711743});
Void::Testing::expect_eq(max32->remainder, std::int64_t{31});
const auto max3 = checked_euclidean_div(
std::numeric_limits<std::int64_t>::max(), std::int64_t{3});
Void::Testing::require(max3.has_value());
Void::Testing::expect_eq(max3->quotient,
std::int64_t{3074457345618258602});
Void::Testing::expect_eq(max3->remainder, std::int64_t{1});
}
Void::Testing::Test checked_euclidean_div_invalid_width()
{
using Void::Numeric::checked_euclidean_div;
using Void::Numeric::Error;
const auto zero = checked_euclidean_div(5, 0);
Void::Testing::require(!zero.has_value());
Void::Testing::expect(zero.error() == Error::DivisionByZero);
const auto negative = checked_euclidean_div(5, -1);
Void::Testing::require(!negative.has_value());
Void::Testing::expect(negative.error() == Error::InvalidInput);
}