Skip to content

fix: correct int_signum! handling of minimum signed integer values#24

Merged
lemmih merged 5 commits intomainfrom
fix/int-signum-min-value
Jan 4, 2026
Merged

fix: correct int_signum! handling of minimum signed integer values#24
lemmih merged 5 commits intomainfrom
fix/int-signum-min-value

Conversation

@lemmih
Copy link
Copy Markdown
Member

@lemmih lemmih commented Jan 4, 2026

Summary

  • Fix incorrect absolute value computation for minimum signed integers (e.g., i8::MIN = -128) in the int_signum! macro
  • Add QuickCheck test for i8 orient2d that detected this bug
  • Increase all QuickCheck tests to run 10,000 iterations for more thorough coverage

Bug Details

The int_signum! macro used wrapping_neg() to compute absolute values when converting signed integers to BigInt. For minimum values like -128i8, wrapping_neg() returns -128 (since 128 cannot be represented in i8), and then as u64 sign-extends this to an incorrect large value.

Fix: Replace wrapping_neg() as u64 with unsigned_abs() as u64, which correctly handles all values including the minimum of each signed type.

lemmih added 4 commits January 4, 2026 02:34
The int_signum! macro incorrectly computed the absolute value of minimum
signed integers (e.g., i8::MIN = -128). Using wrapping_neg() on these
values returns the same negative value due to overflow, and casting to
u64 then sign-extends incorrectly.

Fix: Use unsigned_abs() which correctly handles all values including
the minimum of each signed type.

Also adds QuickCheck test for i8 orient2d and increases all QuickCheck
tests to run 10,000 iterations for more thorough coverage.
Add BigInt reference implementations and QuickCheck property tests for
i8::cmp_dist and i8::incircle predicates.

Also fix overflow bug in i8::incircle where subtractions were performed
outside the int_signum! macro, causing overflow for extreme i8 values.
Move all coordinate subtractions inside the apfp_signum!/int_signum!
macros across all geometry modules (f64, i16, i32, i64). This ensures
the subtractions are computed with arbitrary precision arithmetic,
avoiding potential overflow (for integers) or precision loss (for f64).

The i8 module was already fixed in the previous commit.

Also adds incircle benchmark for f64 comparing apfp, robust crate,
naive f64, and rational implementations.
@lemmih lemmih enabled auto-merge January 4, 2026 01:44
@lemmih lemmih added this pull request to the merge queue Jan 4, 2026
Merged via the queue into main with commit d121a49 Jan 4, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant