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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 45 additions & 1 deletion rational/rational.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,38 @@ fn new_unchecked(numerator : Int64, denominator : Int64) -> T {

///|
/// NOTE: we don't check overflow here, to align with the `op_add` of `Int64`.
/// TODO: add a `checked_add` method.
pub impl Add for T with op_add(self : T, other : T) -> T {
new_unchecked(
self.numerator * other.denominator + other.numerator * self.denominator,
self.denominator * other.denominator,
)
}

///|
/// Checked addition for rational numbers.
/// Returns `None` if any intermediate calculation overflows,
/// otherwise returns the correctly reduced rational.
pub fn T::checked_add(self : T, other : T) -> T? {
let (a, b) = (self, other)
let lhs = a.numerator * b.denominator
if a.numerator != 0L && lhs / a.numerator != b.denominator {
return None
}
let rhs = b.numerator * a.denominator
if b.numerator != 0L && rhs / b.numerator != a.denominator {
return None
}
let num = lhs + rhs
if (lhs ^ rhs) >= 0L && (lhs ^ num) < 0L {
return None
}
let den = a.denominator * b.denominator
if a.denominator != 0L && den / a.denominator != b.denominator {
return None
}
new(num, den)
}

///|
pub impl Sub for T with op_sub(self : T, other : T) -> T {
new_unchecked(
Expand Down Expand Up @@ -366,6 +390,26 @@ test "op_add" {
assert_eq(c.denominator, 1L)
}

///|
test "checked_add" {
// 1/2 + 1/3 = 5/6
let a = new_unchecked(1L, 2L)
let b = new_unchecked(1L, 3L)
let c = a.checked_add(b)
assert_eq(c, Some(new_unchecked(5L, 6L)))

// -1/2 + 1/2 = 0
let a = new_unchecked(-1L, 2L)
let b = new_unchecked(1L, 2L)
let c = a.checked_add(b)
assert_eq(c, Some(new_unchecked(0L, 1L)))

// Large overflow case: MAX + MAX
let big = new_unchecked(@int64.max_value, 1L)
let c = big.checked_add(big)
assert_eq(c, None)
}

///|
test "op_sub" {
// 1/2 - 1/3 = 1/6
Expand Down
1 change: 1 addition & 0 deletions rational/rational.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ impl Show for RationalError
type T
fn T::abs(Self) -> Self
fn T::ceil(Self) -> Int64
fn T::checked_add(Self, Self) -> Self?
fn T::floor(Self) -> Int64
fn T::fract(Self) -> Self
fn T::is_integer(Self) -> Bool
Expand Down
Loading
0