8000 feat: add checked_add method with overflow detection by Topology2333 · Pull Request #2284 · moonbitlang/core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: add checked_add method with overflow detection #2284

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Topology2333
Copy link
Contributor

PR Overview

Summary

Add checked_add to Rational for safe addition with Int64 overflow detection. Returns None if intermediate operations overflow, otherwise returns a reduced rational.

Changes

  • Add T::checked_add(self, other) -> T?
  • Handle overflow in numerator and denominator computation
  • Add unit tests for normal and overflow cases

Motivation

op_add does not check for overflow. checked_add offers a safe alternative for critical use cases.

relates to #241

Copy link
peter-jerry-ye-code-review bot commented Jun 16, 2025
Potential missing case in overflow detection for numerator addition

Category
Correctness
Code Snippet
let num = lhs + rhs
if (lhs ^ rhs) >= 0L && (lhs ^ num) < 0L {
return None
}
Recommendation
Consider adding explicit checks for lhs/rhs being INT64_MIN to handle edge cases
Reasoning
The current overflow check uses sign bit XOR which works for most cases, but could potentially miss edge cases when one operand is INT64_MIN. Explicit checks would make the overflow detection more robust.

Variable naming could be more descriptive

Category
Maintainability
Code Snippet
let (a, b) = (self, other)
let lhs = a.numerator * b.denominator
Recommendation
Use more descriptive names like first and second or keep original self/other names
Reasoning
Single letter variables like 'a' and 'b' reduce code readability. More descriptive names would make the code's intent clearer, especially for complex rational number operations.

Test coverage could be more comprehensive

Category
Maintainability
Code Snippet
test "checked_add" {
// Only tests 3 cases
}
Recommendation
Add more test cases covering edge cases like:

  • Adding with zero
  • INT64_MIN denominators
  • Denominator overflow cases
    Reasoning
    Current tests cover basic functionality and one overflow case. More comprehensive testing of edge cases would help ensure the implementation is robust across all scenarios.

@coveralls
Copy link
Collaborator
coveralls commented Jun 16, 2025

Pull Request Test Coverage Report for Build 7326

Details

  • 3 of 6 (50.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.03%) to 93.075%

Changes Missing Coverage Covered Lines Changed/Added Lines %
rational/rational.mbt 3 6 50.0%
Totals Coverage Status
Change from base Build 7310: -0.03%
Covered Lines: 8575
Relevant Lines: 9213

💛 - Coveralls

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.

2 participants
0