Skip to main content

test_overflow

Function test_overflow 

Source
pub fn test_overflow(result: &Float, o: Ordering) -> bool
Expand description

Given the (Float, Ordering) result of an operation, determines whether an overflow occurred.

We’re defining an overflow to occur whenever the actual result is outside the representable finite range, and is rounded to either infinity or to the maximum or minimum representable finite value. An overflow can present itself in four ways:

  • The result is $\infty$ and the Ordering is Greater
  • The result is $-\infty$ and the Ordering is Less
  • The result is the largest finite value (of any Float with its precision) and the Ordering is Less
  • The result is the smallest (most negative) finite value (of any Float with its precision) and the Ordering is Greater

§Worst-case complexity

$T(n) = O(n)$

$M(n) = O(1)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

§Examples

use malachite_base::num::basic::traits::{Infinity, NegativeInfinity, One};
use malachite_float::{test_overflow, Float};
use std::cmp::Ordering::*;

assert!(test_overflow(&Float::INFINITY, Greater));
assert!(test_overflow(&Float::NEGATIVE_INFINITY, Less));
assert!(test_overflow(&Float::max_finite_value_with_prec(10), Less));
assert!(test_overflow(
    &-Float::max_finite_value_with_prec(10),
    Greater
));

assert!(!test_overflow(&Float::INFINITY, Equal));
assert!(!test_overflow(&Float::ONE, Less));