Skip to content

fix: Fix Number comparison operator#7406

Open
ximinez wants to merge 13 commits into
developfrom
ximinez/number-fix-comparison
Open

fix: Fix Number comparison operator#7406
ximinez wants to merge 13 commits into
developfrom
ximinez/number-fix-comparison

Conversation

@ximinez
Copy link
Copy Markdown
Collaborator

@ximinez ximinez commented Jun 4, 2026

High Level Overview of Change

In operator<, if two Numbers have the same exponent and the same sign, then the final determination is done by comparing the unsigned mantissas directly. If they are both negative, and not equal it returns the wrong result.

Without the renames, comment changes, and test updates, this fix boils down to

        // If equal signs and exponents, compare mantissas.
        if (lneg)
            // If negative, the operator is reversed.
            return l.mantissa_ > r.mantissa_;

Context of Change

I went through all the Number comparisons, and as far as I can determine, all of the ones related to ledger data are guaranteed to have at least one parameter that is non-negative. That means there are no negative to negative comparisons, so we're safe to fix this directly.

Fortunately, the ledger just doesn't deal with a lot of negative numbers. Even with subtraction operations, the results don't go negative (or are checked against zero).

API Impact

  • Public API: New feature (new methods and/or new fields)
  • Public API: Breaking change (in general, breaking changes should only impact the next api_version)
  • libxrpl change (any change that may affect libxrpl or dependents of libxrpl)
  • Peer protocol change (must be backward compatible or bump the peer protocol version)

@ximinez ximinez changed the title Comment out most Number comparisons fix: Fix Number comparison operator Jun 4, 2026
@ximinez ximinez marked this pull request as draft June 4, 2026 16:11
Copy link
Copy Markdown
Contributor

@xrplf-ai-reviewer xrplf-ai-reviewer Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues.

Review by Claude Sonnet 4.6 · Prompt: V15

@ximinez ximinez added this to the 3.2.0 milestone Jun 4, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented Jun 4, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.3%. Comparing base (949887f) to head (7944cbf).

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##           develop   #7406   +/-   ##
=======================================
  Coverage     82.3%   82.3%           
=======================================
  Files         1011    1011           
  Lines        76913   76915    +2     
  Branches      8964    8964           
=======================================
+ Hits         63336   63339    +3     
+ Misses       13568   13567    -1     
  Partials         9       9           
Files with missing lines Coverage Δ
include/xrpl/basics/Number.h 98.4% <100.0%> (+<0.1%) ⬆️

... and 2 files with indirect coverage changes

Impacted file tree graph

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ximinez ximinez marked this pull request as ready for review June 4, 2026 17:32
Copy link
Copy Markdown
Contributor

@xrplf-ai-reviewer xrplf-ai-reviewer Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues.

Review by Claude Sonnet 4.6 · Prompt: V15

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Comment thread src/test/basics/Number_test.cpp
Copy link
Copy Markdown
Collaborator

@gregtatcam gregtatcam left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. I left one suggestion to consider regarding the unit-tests readability.

Copy link
Copy Markdown
Contributor

@xrplf-ai-reviewer xrplf-ai-reviewer Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues.

Review by Claude Sonnet 4.6 · Prompt: V15

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

Comment thread src/test/basics/Number_test.cpp
Copy link
Copy Markdown
Contributor

@xrplf-ai-reviewer xrplf-ai-reviewer Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues.

Review by Claude Sonnet 4.6 · Prompt: V15

@ximinez ximinez changed the base branch from develop to release/3.2.x June 4, 2026 23:52
@ximinez ximinez force-pushed the ximinez/number-fix-comparison branch from 58cdf96 to a489708 Compare June 4, 2026 23:56
@ximinez
Copy link
Copy Markdown
Collaborator Author

ximinez commented Jun 4, 2026

Rebased on top of the release/3.2.x branch to make merging into the release easier.

Copy link
Copy Markdown
Contributor

@xrplf-ai-reviewer xrplf-ai-reviewer Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues.

Review by Claude Sonnet 4.6 · Prompt: V15

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

Comment on lines 410 to 412
friend constexpr bool
operator<(Number const& x, Number const& y) noexcept
operator<(Number const& l, Number const& r) noexcept
{
Copy link
Copy Markdown
Contributor

@vvysokikh1 vvysokikh1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I think this PR looks good, I was just wondering if it worth switching to operator<=> here to let the compiler do the job. I made a quick pr (#7410) in case you're interested

@ximinez
Copy link
Copy Markdown
Collaborator Author

ximinez commented Jun 5, 2026

While I think this PR looks good, I was just wondering if it worth switching to operator<=> here to let the compiler do the job. I made a quick pr (#7410) in case you're interested

@vvysokikh1 Great suggestion and PR. Let's save it for 3.3 so it can get a more thorough looking at.

@ximinez ximinez added the Ready to merge *PR author* thinks it's ready to merge. Has passed code review. Perf sign-off may still be required. label Jun 5, 2026
Copy link
Copy Markdown
Contributor

@xrplf-ai-reviewer xrplf-ai-reviewer Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues.

Review by Claude Sonnet 4.6 · Prompt: V15

@ximinez ximinez changed the base branch from release/3.2.x to develop June 5, 2026 21:24
Copy link
Copy Markdown
Contributor

@xrplf-ai-reviewer xrplf-ai-reviewer Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues.

Review by Claude Sonnet 4.6 · Prompt: V15

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Ready to merge *PR author* thinks it's ready to merge. Has passed code review. Perf sign-off may still be required.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants