Skip to content

Commit

Permalink
Core Lib Documentation: Overflowing module (#6810)
Browse files Browse the repository at this point in the history
Co-authored-by: enitrat <[email protected]>
  • Loading branch information
TAdev0 and enitrat authored Dec 2, 2024
1 parent 4c7d21d commit b427927
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions corelib/src/num/traits/ops/overflowing.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
//! Arithmetic operations with overflow detection.
//!
//! This module provides traits for performing arithmetic operations that explicitly
//! track potential numeric overflow conditions.

/// Performs addition with a flag for overflow.
///
/// # Examples
///
/// ```
/// use core::num::traits::OverflowingAdd;
///
/// let (result, is_overflow) = 1_u8.overflowing_add(255_u8);
/// assert!(result == 0);
/// assert!(is_overflow);
/// ```
pub trait OverflowingAdd<T> {
/// Returns a tuple of the sum along with a boolean indicating whether an arithmetic overflow
/// would occur.
Expand All @@ -7,6 +22,16 @@ pub trait OverflowingAdd<T> {
}

/// Performs subtraction with a flag for overflow.
///
/// # Examples
///
/// ```
/// use core::num::traits::OverflowingSub;
///
/// let (result, is_underflow) = 1_u8.overflowing_sub(2_u8);
/// assert!(result == 255);
/// assert!(is_underflow);
/// ```
pub trait OverflowingSub<T> {
/// Returns a tuple of the difference along with a boolean indicating whether an arithmetic
/// overflow would occur.
Expand All @@ -15,6 +40,16 @@ pub trait OverflowingSub<T> {
}

/// Performs multiplication with a flag for overflow.
///
/// # Examples
///
/// ```
/// use core::num::traits::OverflowingMul;
///
/// let (result, is_overflow) = 1_u8.overflowing_mul(2_u8);
/// assert!(result == 2);
/// assert!(!is_overflow);
/// ```
pub trait OverflowingMul<T> {
/// Returns a tuple of the product along with a boolean indicating whether an arithmetic
/// overflow would occur.
Expand Down

0 comments on commit b427927

Please sign in to comment.