Skip to content

Count trailing zeros in IsEvenInteger and IsOddInteger - #107

Merged
matt-edmondson merged 1 commit into
mainfrom
fix/parity-trailing-zeros
Sep 27, 2026
Merged

matt-edmondson merged 1 commit into
mainfrom
fix/parity-trailing-zeros

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #104

What was wrong

The sanitizing constructor moves trailing zeros into Exponent, so 10 is stored as significand 1 at exponent 1. IsEvenInteger and IsOddInteger looked only at Significand.IsEven. As a result, 10, 30, 100, 7 + 3 and every other multiple of 10 with an odd significand reported as odd and not even.

Change

This is the fix suggested in the issue:

IsEvenInteger: IsInteger(value) && (value.Exponent > 0 || value.Significand.IsEven)
IsOddInteger:  IsInteger(value) && value.Exponent == 0 && !value.Significand.IsEven

Both methods now have a remark explaining why the exponent matters.

As the triage note asked, I checked the other Significand-only predicates. IsPositive, IsNegative and IsZero depend only on sign or zero-ness, so trailing zeros don't affect them. IsUnit already checks Exponent == 0.

Tests

  • TestIsEvenIntegerForMultiplesOfTen: 10, 100, 30, -20 and 1,000,000 must be even and not odd.
  • TestParityOfASumWithTrailingZero: 7 + 3 from the issue.
  • TestIsOddIntegerForOddValues: 1, -3, 21 and 1001 stay odd and not even. This guards against over-correcting.

Verification

  • dotnet test: 395/395 pass.
  • With the source change reverted, the rows for 10, 30, 100 and 1,000,000 and the 7 + 3 test fail. -20 passes either way, because its significand (-2) is already even. It stays in as the case the issue asked for.
  • dotnet build PreciseNumber/PreciseNumber.csproj succeeds with no warnings.

🤖 Generated with Claude Code

https://claude.ai/code/session_018AEYBNCRr6chFLiZDv1MVj


Generated by Claude Code

Trailing zeros are stored in the exponent, so 10 is significand 1 at
exponent 1. The parity checks read only the significand, so every
multiple of 10 whose significand was odd (10, 30, 100, ...) reported as
odd. Any positive exponent makes the value a multiple of 10 and therefore
even; only a value at exponent 0 takes its parity from the significand.

Fixes #104

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018AEYBNCRr6chFLiZDv1MVj
@sonarqubecloud

Copy link
Copy Markdown

@matt-edmondson
matt-edmondson merged commit 0dd099b into main Sep 27, 2026
12 checks passed
@matt-edmondson
matt-edmondson deleted the fix/parity-trailing-zeros branch September 27, 2026 00:16
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.

IsEvenInteger(10) returns false and IsOddInteger(10) returns true for any integer with a trailing zero

2 participants