Skip to content
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

Add checked arithmetic operations for Int and Int64 #244

Closed
wants to merge 1 commit into from
Closed
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
115 changes: 113 additions & 2 deletions int/int.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,82 @@ pub fn signum(self : Int) -> Int {
}
}

// Returns the largest positive finite value of Int
/// Returns the largest positive finite value of Int
pub fn Int::max_value() -> Int {
max_val
}

// Returns the smallest positive nonzero value of Int
/// Returns the smallest positive nonzero value of Int
pub fn Int::min_value() -> Int {
min_val
}

/// Returns the mathematical absolute value of the Int,
/// None if the result overflows.
pub fn checked_abs(self : Int) -> Option[Int] {
if self == min_val {
None
} else {
Some(self.abs())
}
}

/// Returns the negation of the Int,
/// None if the result overflows.
pub fn checked_neg(self: Int) -> Option[Int] {
if self == min_val {
None
} else {
Some(-self)
}
}

/// Returns the sum of the arguments,
/// None if the result overflows.
pub fn checked_add(self: Int, other: Int) -> Option[Int] {
let r = self + other
// Overflow iff both arguments have the opposite sign of the result
if self.lxor(r).land(other.lxor(r)) < 0 {
None
} else {
Some(r)
}
}

/// Returns the difference of the arguments,
/// None if the result overflows.
pub fn checked_sub(self: Int, other: Int) -> Option[Int] {
let r = self - other
// Overflow iff the arguments have different signs and the sign
// of the result is different from the sign of x
if self.lxor(other).land(self.lxor(r)) < 0 {
None
} else {
Some(r)
}
}

/// Returns the product of the arguments,
/// None if the result overflows.
pub fn checked_mul(self: Int, other: Int) -> Option[Int] {
let r = self.to_int64() * other.to_int64()
if r > max_val.to_int64() || r < min_val.to_int64() {
None
} else {
Some(r.to_int())
}
}

/// Returns the quotient of the arguments,
/// None if the result overflows.
pub fn checked_div(self: Int, other: Int) -> Option[Int] {
if other == 0 || (self == min_val && other == -1) {
None
} else {
Some(self / other)
}
}

fn test_num[T : @num.Num + Debug + Default + Eq](
x : T,
y : T,
Expand Down Expand Up @@ -75,3 +141,48 @@ test "int.num" {
pub fn hash(self : Int) -> Int {
self
}

test "checked_abs" {
@assertion.assert_eq((1).checked_abs(), Some(1))?
@assertion.assert_eq((-1).checked_abs(), Some(1))?
@assertion.assert_eq(min_val.checked_abs(), None)?
@assertion.assert_eq(max_val.checked_abs(), Some(max_val))?
}

test "checked_neg" {
@assertion.assert_eq((1).checked_neg(), Some(-1))?
@assertion.assert_eq((-1).checked_neg(), Some(1))?
@assertion.assert_eq(min_val.checked_neg(), None)?
@assertion.assert_eq(max_val.checked_neg(), Some(-max_val))?
}

test "checked_add" {
@assertion.assert_eq((1).checked_add(1), Some(2))?
@assertion.assert_eq((-1).checked_add(1), Some(0))?
@assertion.assert_eq((-1).checked_add(-1), Some(-2))?
@assertion.assert_eq(max_val.checked_add(1), None)?
@assertion.assert_eq(min_val.checked_add(-1), None)?
}

test "checked_sub" {
@assertion.assert_eq((1).checked_sub(1), Some(0))?
@assertion.assert_eq((-1).checked_sub(1), Some(-2))?
@assertion.assert_eq((-1).checked_sub(-1), Some(0))?
@assertion.assert_eq(min_val.checked_sub(1), None)?
@assertion.assert_eq(max_val.checked_sub(-1), None)?
}

test "checked_mul" {
@assertion.assert_eq((2).checked_mul(3), Some(6))?
@assertion.assert_eq((-2).checked_mul(3), Some(-6))?
@assertion.assert_eq((-2).checked_mul(-3), Some(6))?
@assertion.assert_eq(min_val.checked_mul(2), None)?
@assertion.assert_eq(max_val.checked_mul(2), None)?
}

test "checked_div" {
@assertion.assert_eq((4).checked_div(2), Some(2))?
@assertion.assert_eq((-4).checked_div(2), Some(-2))?
@assertion.assert_eq((4).checked_div(0), None)?
@assertion.assert_eq(min_val.checked_div(-1), None)?
}
114 changes: 114 additions & 0 deletions int64/int64.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,75 @@ pub fn Int64::min_value() -> Int64 {
min_val
}

/// Returns the mathematical absolute value of the Int64,
/// None if the result overflows.
pub fn checked_abs(self : Int64) -> Option[Int64] {
if self == min_val {
None
} else {
Some(self.abs())
}
}

/// Returns the negation of the Int64,
/// None if the result overflows.
pub fn checked_neg(self: Int64) -> Option[Int64] {
if self == min_val {
None
} else {
Some(-self)
}
}

/// Returns the sum of the arguments,
/// None if the result overflows.
pub fn checked_add(self: Int64, other: Int64) -> Option[Int64] {
let r = self + other
// Overflow iff both arguments have the opposite sign of the result
if self.lxor(r).land(other.lxor(r)) < 0L {
None
} else {
Some(r)
}
}

/// Returns the difference of the arguments,
/// None if the result overflows.
pub fn checked_sub(self: Int64, other: Int64) -> Option[Int64] {
let r = self - other
// Overflow iff the arguments have different signs and the sign
// of the result is different from the sign of x
if self.lxor(other).land(self.lxor(r)) < 0L {
None
} else {
Some(r)
}
}

/// Returns the product of the arguments,
/// None if the result overflows.
pub fn checked_mul(self: Int64, other: Int64) -> Option[Int64] {
let r = self * other
let abs_self = self.abs()
let abs_other = other.abs()
if abs_self.lor(abs_other).lsr(31) != 0L {
// bits that greater than 2^31 might cause overflow
if ((other != 0L) && (r / other != self)) || (self == min_val && other == -1L) {
return None
}
}
Some(r)
}

/// Returns the quotient of the arguments,
/// None if the result overflows.
pub fn checked_div(self: Int64, other: Int64) -> Option[Int64] {
if other == 0L || (self == min_val && other == -1L) {
None
} else {
Some(self / other)
}
}
fn test_num[T : @num.Num + Debug + Default + Eq](
x : T,
y : T,
Expand Down Expand Up @@ -77,3 +146,48 @@ pub fn hash(self : Int64) -> Int {
let hi = self.lsr(32).to_int()
lo.lxor(hi)
}

test "checked_abs" {
@assertion.assert_eq((1L).checked_abs(), Some(1L))?
@assertion.assert_eq((-1L).checked_abs(), Some(1L))?
@assertion.assert_eq(min_val.checked_abs(), None)?
@assertion.assert_eq(max_val.checked_abs(), Some(max_val))?
}

test "checked_neg" {
@assertion.assert_eq((1L).checked_neg(), Some(-1L))?
@assertion.assert_eq((-1L).checked_neg(), Some(1L))?
@assertion.assert_eq(min_val.checked_neg(), None)?
@assertion.assert_eq(max_val.checked_neg(), Some(-max_val))?
}

test "checked_add" {
@assertion.assert_eq((1L).checked_add(1L), Some(2L))?
@assertion.assert_eq((-1L).checked_add(1L), Some(0L))?
@assertion.assert_eq((-1L).checked_add(-1L), Some(-2L))?
@assertion.assert_eq(max_val.checked_add(1L), None)?
@assertion.assert_eq(min_val.checked_add(-1L), None)?
}

test "checked_sub" {
@assertion.assert_eq((1L).checked_sub(1L), Some(0L))?
@assertion.assert_eq((-1L).checked_sub(1L), Some(-2L))?
@assertion.assert_eq((-1L).checked_sub(-1L), Some(0L))?
@assertion.assert_eq(min_val.checked_sub(1L), None)?
@assertion.assert_eq(max_val.checked_sub(-1L), None)?
}

test "checked_mul" {
@assertion.assert_eq((2L).checked_mul(3L), Some(6L))?
@assertion.assert_eq((-2L).checked_mul(3L), Some(-6L))?
@assertion.assert_eq((-2L).checked_mul(-3L), Some(6L))?
@assertion.assert_eq(min_val.checked_mul(2L), None)?
@assertion.assert_eq(max_val.checked_mul(2L), None)?
}

test "checked_div" {
@assertion.assert_eq((4L).checked_div(2L), Some(2L))?
@assertion.assert_eq((-4L).checked_div(2L), Some(-2L))?
@assertion.assert_eq((4L).checked_div(0L), None)?
@assertion.assert_eq(min_val.checked_div(-1L), None)?
}