Skip to main content

FastRational

Enum FastRational 

Source
pub enum FastRational {
    Small {
        num: i64,
        den: i64,
    },
    Big(Box<BigRational>),
}
Expand description

A rational number that uses i64/i64 for small values and falls back to BigRational.

Over 95% of Simplex operations stay in the small path, avoiding heap allocation.

Variants§

§

Small

Small representation: num / den where den > 0 and gcd(|num|, den) == 1.

Fields

§num: i64

Numerator (can be negative, zero, or positive).

§den: i64

Denominator (always positive, always coprime with |num|).

§

Big(Box<BigRational>)

Big representation: heap-allocated arbitrary-precision rational.

Implementations§

Source§

impl FastRational

Source

pub fn new_small(num: i64, den: i64) -> Self

Create a new Small rational, normalizing sign and reducing to lowest terms.

§Panics

Panics (debug-only) if den == 0. In release builds, division by zero is undefined and will produce garbage.

Source

pub fn from_big(br: BigRational) -> Self

Create a FastRational from a BigRational, demoting to Small if it fits.

Source

pub fn to_big_rational(&self) -> BigRational

Convert to BigRational.

Source

pub fn to_f64(&self) -> f64

Convert to f64 (lossy).

Source

pub fn recip(&self) -> Option<Self>

Return the reciprocal (1/self). Returns None if self is zero.

Source

pub fn floor(&self) -> BigInt

Compute the floor (greatest integer <= self).

Source

pub fn ceil(&self) -> BigInt

Compute the ceiling (smallest integer >= self).

Source

pub fn abs(&self) -> Self

Return the absolute value.

Source

pub fn numer(&self) -> BigInt

Return the numerator as a BigInt.

Source

pub fn denom(&self) -> BigInt

Return the denominator as a BigInt.

Source

pub fn is_integer(&self) -> bool

Check if this rational is an integer (denominator == 1).

Source

pub fn from_big_ref(br: &BigRational) -> Self

Create a FastRational from a BigRational reference.

Source

pub fn from_bigint(bi: &BigInt) -> Self

Create a FastRational representing an integer from a BigInt.

Source§

impl FastRational

Source

pub fn from_integer(n: BigInt) -> Self

Create a FastRational from an integer.

Source§

impl FastRational

Source

pub fn max(self, other: Self) -> Self

Returns the larger of self and other.

Source

pub fn min(self, other: Self) -> Self

Returns the smaller of self and other.

Trait Implementations§

Source§

impl Add<&FastRational> for &FastRational

Source§

type Output = FastRational

The resulting type after applying the + operator.
Source§

fn add(self, rhs: &FastRational) -> Self::Output

Performs the + operation. Read more
Source§

impl Add<&FastRational> for FastRational

Source§

type Output = FastRational

The resulting type after applying the + operator.
Source§

fn add(self, rhs: &FastRational) -> Self::Output

Performs the + operation. Read more
Source§

impl Add<FastRational> for &FastRational

Source§

type Output = FastRational

The resulting type after applying the + operator.
Source§

fn add(self, rhs: FastRational) -> Self::Output

Performs the + operation. Read more
Source§

impl Add for FastRational

Source§

type Output = FastRational

The resulting type after applying the + operator.
Source§

fn add(self, rhs: FastRational) -> Self::Output

Performs the + operation. Read more
Source§

impl AddAssign<&FastRational> for FastRational

Source§

fn add_assign(&mut self, rhs: &FastRational)

Performs the += operation. Read more
Source§

impl AddAssign for FastRational

Source§

fn add_assign(&mut self, rhs: FastRational)

Performs the += operation. Read more
Source§

impl Clone for FastRational

Source§

fn clone(&self) -> FastRational

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for FastRational

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for FastRational

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Div<&FastRational> for &FastRational

Source§

type Output = FastRational

The resulting type after applying the / operator.
Source§

fn div(self, rhs: &FastRational) -> Self::Output

Performs the / operation. Read more
Source§

impl Div<&FastRational> for FastRational

Source§

type Output = FastRational

The resulting type after applying the / operator.
Source§

fn div(self, rhs: &FastRational) -> Self::Output

Performs the / operation. Read more
Source§

impl Div<FastRational> for &FastRational

Source§

type Output = FastRational

The resulting type after applying the / operator.
Source§

fn div(self, rhs: FastRational) -> Self::Output

Performs the / operation. Read more
Source§

impl Div for FastRational

Source§

fn div(self, rhs: FastRational) -> Self::Output

Divide two FastRational values.

§Panics

Panics if the divisor is zero.

Source§

type Output = FastRational

The resulting type after applying the / operator.
Source§

impl DivAssign<&FastRational> for FastRational

Source§

fn div_assign(&mut self, rhs: &FastRational)

Performs the /= operation. Read more
Source§

impl DivAssign for FastRational

Source§

fn div_assign(&mut self, rhs: FastRational)

Performs the /= operation. Read more
Source§

impl From<&Ratio<BigInt>> for FastRational

Source§

fn from(br: &BigRational) -> Self

Converts to this type from the input type.
Source§

impl From<(i64, i64)> for FastRational

Source§

fn from((n, d): (i64, i64)) -> Self

Converts to this type from the input type.
Source§

impl From<BigInt> for FastRational

Source§

fn from(bi: BigInt) -> Self

Converts to this type from the input type.
Source§

impl From<Ratio<BigInt>> for FastRational

Source§

fn from(br: BigRational) -> Self

Converts to this type from the input type.
Source§

impl From<i64> for FastRational

Source§

fn from(n: i64) -> Self

Converts to this type from the input type.
Source§

impl Hash for FastRational

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Mul<&FastRational> for &FastRational

Source§

type Output = FastRational

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: &FastRational) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul<&FastRational> for FastRational

Source§

type Output = FastRational

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: &FastRational) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul<FastRational> for &FastRational

Source§

type Output = FastRational

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: FastRational) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul for FastRational

Source§

type Output = FastRational

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: FastRational) -> Self::Output

Performs the * operation. Read more
Source§

impl MulAssign<&FastRational> for FastRational

Source§

fn mul_assign(&mut self, rhs: &FastRational)

Performs the *= operation. Read more
Source§

impl MulAssign for FastRational

Source§

fn mul_assign(&mut self, rhs: FastRational)

Performs the *= operation. Read more
Source§

impl Neg for &FastRational

Source§

type Output = FastRational

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
Source§

impl Neg for FastRational

Source§

type Output = FastRational

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
Source§

impl Num for FastRational

Source§

type FromStrRadixErr = String

Source§

fn from_str_radix(str: &str, radix: u32) -> Result<Self, Self::FromStrRadixErr>

Convert from a string and radix (typically 2..=36). Read more
Source§

impl One for FastRational

Source§

fn one() -> Self

Returns the multiplicative identity element of Self, 1. Read more
Source§

fn is_one(&self) -> bool

Returns true if self is equal to the multiplicative identity. Read more
Source§

fn set_one(&mut self)

Sets self to the multiplicative identity element of Self, 1.
Source§

impl Ord for FastRational

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for FastRational

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for FastRational

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Rem<&FastRational> for &FastRational

Source§

type Output = FastRational

The resulting type after applying the % operator.
Source§

fn rem(self, rhs: &FastRational) -> Self::Output

Performs the % operation. Read more
Source§

impl Rem for FastRational

Source§

fn rem(self, rhs: FastRational) -> Self::Output

Remainder for rationals: a % b = a - b * floor(a/b).

For non-zero b, this returns a value in [0, |b|).

Source§

type Output = FastRational

The resulting type after applying the % operator.
Source§

impl RemAssign for FastRational

Source§

fn rem_assign(&mut self, rhs: FastRational)

Performs the %= operation. Read more
Source§

impl Signed for FastRational

Source§

fn abs(&self) -> Self

Computes the absolute value. Read more
Source§

fn abs_sub(&self, other: &Self) -> Self

The positive difference of two numbers. Read more
Source§

fn signum(&self) -> Self

Returns the sign of the number. Read more
Source§

fn is_positive(&self) -> bool

Returns true if the number is positive and false if the number is zero or negative.
Source§

fn is_negative(&self) -> bool

Returns true if the number is negative and false if the number is zero or positive.
Source§

impl Sub<&FastRational> for &FastRational

Source§

type Output = FastRational

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: &FastRational) -> Self::Output

Performs the - operation. Read more
Source§

impl Sub<&FastRational> for FastRational

Source§

type Output = FastRational

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: &FastRational) -> Self::Output

Performs the - operation. Read more
Source§

impl Sub<FastRational> for &FastRational

Source§

type Output = FastRational

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: FastRational) -> Self::Output

Performs the - operation. Read more
Source§

impl Sub for FastRational

Source§

type Output = FastRational

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: FastRational) -> Self::Output

Performs the - operation. Read more
Source§

impl SubAssign<&FastRational> for FastRational

Source§

fn sub_assign(&mut self, rhs: &FastRational)

Performs the -= operation. Read more
Source§

impl SubAssign for FastRational

Source§

fn sub_assign(&mut self, rhs: FastRational)

Performs the -= operation. Read more
Source§

impl Zero for FastRational

Source§

fn zero() -> Self

Returns the additive identity element of Self, 0. Read more
Source§

fn is_zero(&self) -> bool

Returns true if self is equal to the additive identity.
Source§

fn set_zero(&mut self)

Sets self to the additive identity element of Self, 0.
Source§

impl Eq for FastRational

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> NumAssign for T
where T: Num + NumAssignOps,

Source§

impl<T, Rhs> NumAssignOps<Rhs> for T
where T: AddAssign<Rhs> + SubAssign<Rhs> + MulAssign<Rhs> + DivAssign<Rhs> + RemAssign<Rhs>,

Source§

impl<T, Rhs, Output> NumOps<Rhs, Output> for T
where T: Sub<Rhs, Output = Output> + Mul<Rhs, Output = Output> + Div<Rhs, Output = Output> + Add<Rhs, Output = Output> + Rem<Rhs, Output = Output>,