Skip to content

Break MaxMagnitude and MinMagnitude ties by sign, as int and double do - #113

Merged
matt-edmondson merged 2 commits into
mainfrom
claude/magnitude-tie-sign
Sep 27, 2026
Merged

matt-edmondson merged 2 commits into
mainfrom
claude/magnitude-tie-sign

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #111

Summary

MaxMagnitude and MinMagnitude returned the first argument whenever the magnitudes were equal, so MaxMagnitude(-2, 2) gave -2 and MinMagnitude(2, -2) gave 2. The result also changed with argument order. The INumberBase<T> contract, as int, double, and decimal implement it, returns the positive value from MaxMagnitude and the negative value from MinMagnitude on a tie. Both methods now compare magnitudes once and break a tie by sign. MaxMagnitudeNumber and MinMagnitudeNumber delegate to them, so they pick up the fix too.

Tests

  • The existing TestStaticMinMagnitude and TestStaticMinMagnitudeNumber tests expected the old answer for (1, -1). They now expect -1.
  • TestMagnitudeTiesFollowInt runs (-2, 2) and (2, -2) through all four methods and compares each result with int's own answer.
  • TestMagnitudeWithoutATiePicksByAbsoluteValue covers the non-tie cases for each sign and argument order.
  • With the library change reverted, 4 of the magnitude tests fail. With it applied, the full suite passes (391 tests), and the library builds for every target framework with no warnings.

🤖 Generated with Claude Code

https://claude.ai/code/session_01QK96a24CjVhUK9u1Ss2YNN


Generated by Claude Code

…o [patch]

MaxMagnitude and MinMagnitude returned the first argument whenever the two
magnitudes were equal, so MaxMagnitude(-2, 2) gave -2 and MinMagnitude(2, -2)
gave 2, and the answer depended on argument order. INumberBase, as int, double
and decimal implement it, returns the positive value from MaxMagnitude and the
negative value from MinMagnitude on a tie. MaxMagnitudeNumber and
MinMagnitudeNumber delegate to them and inherit the fix.

The existing MinMagnitude tests asserted the old behaviour with (1, -1); they
now expect -1. New tests check both argument orders of the tie against int's
own MaxMagnitude and MinMagnitude, and the non-tie cases for each sign.

Fixes #111

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QK96a24CjVhUK9u1Ss2YNN
… if [patch]

SonarCloud S3358 flagged the nested conditional expressions. Behaviour is
unchanged.

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

Copy link
Copy Markdown

@matt-edmondson
matt-edmondson merged commit 146aa18 into main Sep 27, 2026
12 checks passed
@matt-edmondson
matt-edmondson deleted the claude/magnitude-tie-sign branch September 27, 2026 00:19
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.

MaxMagnitude(-2, 2) returns -2 and MinMagnitude(2, -2) returns 2, the opposite of the INumberBase contract

2 participants