Struct Rational

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

A type representing a signed projectively-extended rational number

§Examples

use extended_rational::Rational;

let a = Rational::new(3, 17);
let b = Rational::new(-4, 7);

assert_eq!(a+b, Rational::new(-47, 119));

Use the ratio! macro for more convenient use.

let a = ratio!(3, 17);
let b = ratio!(-4, 7);

Or for easy conversions from primitive types.

let a = ratio!(-77.332);
let b = ratio!(2u8);

Implementations§

Source§

impl Rational

Source

pub fn new(numerator: i64, denominator: i64) -> Rational

Create a new signed rational with the given numerator and denominator.

Source

pub fn new_raw(unsigned: URational, negative: bool) -> Rational

Create a new signed rational with the given unsigned rational and sign.

Source

pub fn numerator(self) -> u64

Returns the underlying numerator of this rational.

Source

pub fn numerator_mut(&mut self) -> &mut u64

Returns the underlying numerator of this rational mutably.

Source

pub fn denominator(self) -> u64

Returns the underlying denominator of this rational.

Source

pub fn denominator_mut(&mut self) -> &mut u64

Returns the underlying denominator of this rational mutably.

Source

pub fn sign(self) -> bool

Returns the underlying sign of this rational.

Source

pub fn sign_mut(&mut self) -> &mut bool

Returns the underlying sign of this rational mutably.

Source

pub fn unsigned(self) -> URational

Returns the underlying unsigned rational of this rational.

Source

pub fn unsigned_mut(&mut self) -> &mut URational

Returns the underlying unsigned rational of this rational mutably.

Source

pub fn try_unsigned(self) -> URational

Returns the underlying unsigned rational of this rational, panicking if sign is negative.

Does not panic in optimized builds.

Source

pub fn min_value() -> Rational

Returns the smallest value a signed rational can store.

Source

pub fn min_pos_value() -> Rational

Returns the smallest positive value a signed rational can store.

Source

pub fn max_neg_value() -> Rational

Returns the largest negative value a signed rational can store.

Source

pub fn max_value() -> Rational

Returns the largest value a signed rational can store.

Source

pub fn nan() -> Rational

Returns a signed rational representing NaN.

Source

pub fn zero() -> Rational

Returns a signed rational representing 0.

Source

pub fn infinity() -> Rational

Returns a signed rational representing .

Source

pub fn one() -> Rational

Returns a signed rational representing 1.

Source

pub fn negative_one() -> Rational

Returns a signed rational representing -1.

Source

pub fn is_nan(self) -> bool

Returns true if this rational is NaN and false otherwise.

Source

pub fn is_zero(self) -> bool

Returns true if this rational is 0 and false otherwise.

Source

pub fn is_infinity(self) -> bool

Returns true if this rational is and false otherwise.

Source

pub fn is_signed(self) -> bool

Returns true if this rational is a signed number (not NaN, 0, or ) and false otherwise.

Source

pub fn is_negative(self) -> bool

Returns true if this rational is a negative number and false otherwise.

Source

pub fn reciprocal(self) -> Rational

Returns the reciprocal of this rational.

Source

pub fn negative_reciprocal(self) -> Rational

Returns the negative reciprocal of this rational.

Source

pub fn complexity(self) -> u64

Returns the complexity of this rational (max of absolute values of numerator and denominator).

Source

pub fn is_positive(self) -> bool

Returns true if this rational is a positive number and false otherwise.

Source

pub fn floor(self) -> Rational

Returns this rational with no fractional component by rounding towards zero.

Source

pub fn round(self) -> Rational

Returns this rational with no fractional component by rounding.

Source

pub fn ceil(self) -> Rational

Returns this rational with no fractional component by rounding away from zero.

Source

pub fn abs(self) -> Rational

Returns this rational without a negative sign.

Source

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

Computes self + other, returning None if rounding occurred.

Source

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

Computes self - other, returning None if rounding occurred.

Source

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

Computes self * other, returning None if rounding occurred.

Source

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

Computes self / other, returning None if rounding occurred.

Source

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

Computes self % other, returning None if rounding occurred.

Trait Implementations§

Source§

impl Add for Rational

Source§

type Output = Rational

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

impl AddAssign for Rational

Source§

fn add_assign(&mut self, other: Rational)

Performs the += operation. Read more
Source§

impl Clone for Rational

Source§

fn clone(&self) -> Rational

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 Rational

Source§

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

Formats the rational.

§Style

All numbers are written as fractions in parentheses (n/d)

Source§

impl Default for Rational

Source§

fn default() -> Rational

Returns the “default value” for a type. Read more
Source§

impl Display for Rational

Source§

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

Formats the rational.

§Style
  • NaN, , and whole numbers are written directly
  • Ratios with complexities less than 100 are written as fractions (n/d)
  • All other numbers are written as decimals
Source§

impl Div for Rational

Source§

type Output = Rational

The resulting type after applying the / operator.
Source§

fn div(self, other: Rational) -> Rational

Performs the / operation. Read more
Source§

impl DivAssign for Rational

Source§

fn div_assign(&mut self, other: Rational)

Performs the /= operation. Read more
Source§

impl From<[i16; 2]> for Rational

Source§

fn from(array: [i16; 2]) -> Rational

Creates a new signed rational with the given numerator and denominator array.

Source§

impl From<[i32; 2]> for Rational

Source§

fn from(array: [i32; 2]) -> Rational

Creates a new signed rational with the given numerator and denominator array.

Source§

impl From<[i64; 2]> for Rational

Source§

fn from(array: [i64; 2]) -> Rational

Creates a new signed rational with the given numerator and denominator array.

Source§

impl From<[i8; 2]> for Rational

Source§

fn from(array: [i8; 2]) -> Rational

Creates a new signed rational with the given numerator and denominator array.

Source§

impl From<[u16; 2]> for Rational

Source§

fn from(array: [u16; 2]) -> Rational

Create a new signed rational with the given numerator and denominator array.

Source§

impl From<[u32; 2]> for Rational

Source§

fn from(array: [u32; 2]) -> Rational

Create a new signed rational with the given numerator and denominator array.

Source§

impl From<[u64; 2]> for Rational

Source§

fn from(array: [u64; 2]) -> Rational

Create a new signed rational with the given numerator and denominator array.

Source§

impl From<[u8; 2]> for Rational

Source§

fn from(array: [u8; 2]) -> Rational

Create a new signed rational with the given numerator and denominator array.

Source§

impl From<(URational, bool)> for Rational

Source§

fn from(tuple: (URational, bool)) -> Rational

Create a new signed rational from an unsigned rational and a sign.

Source§

impl From<(i16, i16)> for Rational

Source§

fn from(tuple: (i16, i16)) -> Rational

Creates a new signed rational with the given numerator and denominator tuple.

Source§

impl From<(i32, i32)> for Rational

Source§

fn from(tuple: (i32, i32)) -> Rational

Creates a new signed rational with the given numerator and denominator tuple.

Source§

impl From<(i64, i64)> for Rational

Source§

fn from(tuple: (i64, i64)) -> Rational

Creates a new signed rational with the given numerator and denominator tuple.

Source§

impl From<(i8, i8)> for Rational

Source§

fn from(tuple: (i8, i8)) -> Rational

Creates a new signed rational with the given numerator and denominator tuple.

Source§

impl From<(u16, u16)> for Rational

Source§

fn from(tuple: (u16, u16)) -> Rational

Create a new signed rational with the given numerator and denominator tuple.

Source§

impl From<(u32, u32)> for Rational

Source§

fn from(tuple: (u32, u32)) -> Rational

Create a new signed rational with the given numerator and denominator tuple.

Source§

impl From<(u64, u64)> for Rational

Source§

fn from(tuple: (u64, u64)) -> Rational

Create a new signed rational with the given numerator and denominator tuple.

Source§

impl From<(u8, u8)> for Rational

Source§

fn from(tuple: (u8, u8)) -> Rational

Create a new signed rational with the given numerator and denominator tuple.

Source§

impl From<Rational> for f32

Source§

fn from(r: Rational) -> f32

Creates an approximation of the given signed rational.

Source§

impl From<Rational> for f64

Source§

fn from(r: Rational) -> f64

Creates an approximation of the given signed rational.

Source§

impl From<URational> for Rational

Source§

fn from(r: URational) -> Rational

Create a new signed rational from an unsigned rational.

Source§

impl From<f32> for Rational

Source§

fn from(f: f32) -> Rational

Attempts to approximate the given floating-point number with a signed rational.

§Rounding
  • If the exponent is too large, will be returned
  • If the exponent is too small, 0 will be returned
Source§

impl From<f64> for Rational

Source§

fn from(f: f64) -> Rational

Attempts to approximate the given floating-point number with a signed rational.

§Rounding
  • If the exponent is too large, will be returned
  • If the exponent is too small, 0 will be returned
Source§

impl From<i16> for Rational

Source§

fn from(n: i16) -> Rational

Creates a new signed rational with the given value.

Source§

impl From<i32> for Rational

Source§

fn from(n: i32) -> Rational

Creates a new signed rational with the given value.

Source§

impl From<i64> for Rational

Source§

fn from(n: i64) -> Rational

Creates a new signed rational with the given value.

Source§

impl From<i8> for Rational

Source§

fn from(n: i8) -> Rational

Creates a new signed rational with the given value.

Source§

impl From<u16> for Rational

Source§

fn from(n: u16) -> Rational

Create a new signed rational with the given value.

Source§

impl From<u32> for Rational

Source§

fn from(n: u32) -> Rational

Create a new signed rational with the given value.

Source§

impl From<u64> for Rational

Source§

fn from(n: u64) -> Rational

Create a new signed rational with the given value.

Source§

impl From<u8> for Rational

Source§

fn from(n: u8) -> Rational

Create a new signed rational with the given value.

Source§

impl Mul for Rational

Source§

type Output = Rational

The resulting type after applying the * operator.
Source§

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

Performs the * operation. Read more
Source§

impl MulAssign for Rational

Source§

fn mul_assign(&mut self, other: Rational)

Performs the *= operation. Read more
Source§

impl Neg for Rational

Source§

type Output = Rational

The resulting type after applying the - operator.
Source§

fn neg(self) -> Rational

Performs the unary - operation. Read more
Source§

impl PartialEq for Rational

Source§

fn eq(&self, other: &Rational) -> 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 Rational

Source§

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 · 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 for Rational

Source§

type Output = Rational

The resulting type after applying the % operator.
Source§

fn rem(self, other: Rational) -> Rational

Performs the % operation. Read more
Source§

impl RemAssign for Rational

Source§

fn rem_assign(&mut self, other: Rational)

Performs the %= operation. Read more
Source§

impl Sub for Rational

Source§

type Output = Rational

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

impl SubAssign for Rational

Source§

fn sub_assign(&mut self, other: Rational)

Performs the -= operation. Read more
Source§

impl Copy 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.