Skip to main content

Rational

Struct Rational 

pub struct Rational { /* private fields */ }
Expand description

An exact rational number: a fraction kept in lowest terms with a strictly positive denominator. Built on BigInt, so it never rounds the way a JSON / f64 “number” does — 1/3 stays exactly 1/3, not 0.3333…, and a numerator past 2^53 survives instead of collapsing onto a double.

Representation is correct-first: BigInt numerator and denominator, reduced on every construction. The i64-fast-path (storing small num/den inline to skip the BigInt allocation) is the documented performance follow-up — exactly as Karatsuba is for BigInt — and must reproduce these values bit-for-bit.

Implementations§

§

impl Rational

pub fn new(num: BigInt, den: BigInt) -> Option<Rational>

The canonicalizing constructor: reduce num/den to lowest terms with a positive denominator. Returns None for a zero denominator (the one undefined fraction).

pub fn from_bigint(n: BigInt) -> Rational

The integer n as the fraction n/1.

pub fn from_i64(x: i64) -> Rational

pub fn from_ratio_i64(n: i64, d: i64) -> Option<Rational>

n/d from machine integers (convenience; None if d == 0).

pub fn zero() -> Rational

pub fn one() -> Rational

pub fn numerator(&self) -> &BigInt

pub fn denominator(&self) -> &BigInt

pub fn is_integer(&self) -> bool

True when the value is a whole number (den == 1).

pub fn is_zero(&self) -> bool

pub fn is_negative(&self) -> bool

pub fn is_positive(&self) -> bool

pub fn to_bigint(&self) -> Option<BigInt>

The integer value when this is whole, else None (the provable narrow back to BigInt).

pub fn to_i64(&self) -> Option<i64>

The i64 value when this is a whole number that fits, else None.

pub fn to_f64(&self) -> f64

Nearest f64 (for display / interop only — lossy for large terms, exact for small ones). The value stays exact; this is a view.

pub fn floor(&self) -> BigInt

The greatest integer ≤ self (round toward −∞). floor(7/2) == 3, floor(-7/2) == -4. The companion of explicit floor division.

pub fn ceil(&self) -> BigInt

The least integer ≥ self (round toward +∞). ceil(7/2) == 4, ceil(-7/2) == -3.

pub fn round(&self) -> BigInt

The nearest integer, ties rounded AWAY from zero (matching f64::round): round(x) = sign(x) · ⌊|x| + 1/2⌋ = sign(x) · ((2|num| + den) ÷ 2den).

pub fn negated(&self) -> Rational

pub fn abs(&self) -> Rational

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

1/selfNone when self == 0.

pub fn add(&self, other: &Rational) -> Rational

pub fn sub(&self, other: &Rational) -> Rational

pub fn mul(&self, other: &Rational) -> Rational

pub fn div(&self, other: &Rational) -> Option<Rational>

self / otherNone when other == 0.

pub fn pow(&self, exp: i32) -> Option<Rational>

self^exp, exact for every integer exponent. Negative exponents take the reciprocal first; None only for 0 raised to a negative power.

pub fn parse(s: &str) -> Option<Rational>

Parse "3/4", "-3/4", or a bare integer "5". Whitespace around the parts is tolerated; None on malformed input or a zero denominator.

Trait Implementations§

§

impl Clone for Rational

§

fn clone(&self) -> Rational

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
§

impl Debug for Rational

§

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

Formats the value using the given formatter. Read more
§

impl Display for Rational

§

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

Formats the value using the given formatter. Read more
§

impl From<BigInt> for Rational

§

fn from(x: BigInt) -> Rational

Converts to this type from the input type.
§

impl From<i64> for Rational

§

fn from(x: i64) -> Rational

Converts to this type from the input type.
§

impl Hash for Rational

§

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

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
§

impl Ord for Rational

§

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

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

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

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

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

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

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

Restrict a value to a certain interval. Read more
§

impl PartialEq for Rational

§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · 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.
§

impl PartialOrd for Rational

§

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

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · 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 (const: unstable) · 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 (const: unstable) · 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 (const: unstable) · 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
§

impl Eq for Rational

§

impl StructuralPartialEq for Rational

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.