URational

Struct URational 

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

A type representing an unsigned projectively-extended rational number

Subtracting a large number from a smaller one always returns 0 unless the larger number is .

§Examples

use extended_rational::URational;

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

assert_eq!(a+b, URational::new(89, 119));

Use the uratio! macro for more convenient use.

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

Or for easy conversions from primitive types.

let a = uratio!(343.863);
let b = uratio!(2u8);

Implementations§

Source§

impl URational

Source

pub fn new(numerator: u64, denominator: u64) -> URational

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

Source

pub fn numerator(self) -> u64

Returns the numerator of this rational.

Source

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

Returns the numerator of this rational mutably.

Source

pub fn denominator(self) -> u64

Returns the denominator of this rational.

Source

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

Returns the denominator of this rational mutably.

Source

pub fn min_value() -> URational

Returns the smallest value an unsigned rational can store.

Source

pub fn min_pos_value() -> URational

Returns the smallest non-zero value an unsigned rational can store.

Source

pub fn max_value() -> URational

Returns the largest value an unsigned rational can store.

Source

pub fn nan() -> URational

Returns an unsigned rational representing NaN.

Source

pub fn zero() -> URational

Returns an unsigned rational representing 0.

Source

pub fn infinity() -> URational

Returns an unsigned rational representing .

Source

pub fn one() -> URational

Returns an unsigned 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 reciprocal(self) -> URational

Returns the reciprocal of this rational.

Source

pub fn complexity(self) -> u64

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

Source

pub fn floor(self) -> URational

Returns this rational with no fractional component by rounding down.

Source

pub fn round(self) -> URational

Returns this rational with no fractional component by rounding.

Source

pub fn ceil(self) -> URational

Returns this rational with no fractional component by rounding up.

Source

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

Computes self + other, returning None if rounding occurred.

Source

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

Computes self - other, returning None if rounding occurred.

Source

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

Computes self * other, returning None if rounding occurred.

Source

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

Computes self / other, returning None if rounding occurred.

Source

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

Computes self % other, returning None if rounding occurred.

Trait Implementations§

Source§

impl Add for URational

Source§

type Output = URational

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

impl AddAssign for URational

Source§

fn add_assign(&mut self, other: URational)

Performs the += operation. Read more
Source§

impl Clone for URational

Source§

fn clone(&self) -> URational

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 URational

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 URational

Source§

fn default() -> URational

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

impl Display for URational

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 URational

Source§

type Output = URational

The resulting type after applying the / operator.
Source§

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

Performs the / operation. Read more
Source§

impl DivAssign for URational

Source§

fn div_assign(&mut self, other: URational)

Performs the /= operation. Read more
Source§

impl From<[u16; 2]> for URational

Source§

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

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

Source§

impl From<[u32; 2]> for URational

Source§

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

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

Source§

impl From<[u64; 2]> for URational

Source§

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

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

Source§

impl From<[u8; 2]> for URational

Source§

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

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

Source§

impl From<(u16, u16)> for URational

Source§

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

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

Source§

impl From<(u32, u32)> for URational

Source§

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

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

Source§

impl From<(u64, u64)> for URational

Source§

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

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

Source§

impl From<(u8, u8)> for URational

Source§

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

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

Source§

impl From<URational> for Rational

Source§

fn from(r: URational) -> Rational

Create a new signed rational from an unsigned rational.

Source§

impl From<URational> for f32

Source§

fn from(r: URational) -> f32

Creates an approximation of the given unsigned rational.

Source§

impl From<URational> for f64

Source§

fn from(r: URational) -> f64

Creates an approximation of the given unsigned rational.

Source§

impl From<u16> for URational

Source§

fn from(n: u16) -> URational

Create a new unsigned rational with the given value.

Source§

impl From<u32> for URational

Source§

fn from(n: u32) -> URational

Create a new unsigned rational with the given value.

Source§

impl From<u64> for URational

Source§

fn from(n: u64) -> URational

Create a new unsigned rational with the given value.

Source§

impl From<u8> for URational

Source§

fn from(n: u8) -> URational

Create a new unsigned rational with the given value.

Source§

impl Mul for URational

Source§

type Output = URational

The resulting type after applying the * operator.
Source§

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

Performs the * operation. Read more
Source§

impl MulAssign for URational

Source§

fn mul_assign(&mut self, other: URational)

Performs the *= operation. Read more
Source§

impl PartialEq for URational

Source§

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

Source§

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

Source§

type Output = URational

The resulting type after applying the % operator.
Source§

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

Performs the % operation. Read more
Source§

impl RemAssign for URational

Source§

fn rem_assign(&mut self, other: URational)

Performs the %= operation. Read more
Source§

impl Sub for URational

Source§

type Output = URational

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

impl SubAssign for URational

Source§

fn sub_assign(&mut self, other: URational)

Performs the -= operation. Read more
Source§

impl Copy for URational

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.