Struct rugrat::Rational [] [src]

pub struct Rational { /* fields omitted */ }

An arbitrary-precision rational number.

A rational number is made up of a numerator Integer and denominator Integer. After rational number functions, the number is always in canonical form, that is, the denominator is always greater than zero, and there are no common factors. Zero is stored as 0/1.

Examples

extern crate rugint;
extern crate rugrat;

use rugint::Integer;
use rugrat::Rational;

fn main() {
    let r = Rational::from((-12, 15));
    let (num, den) = r.into_numer_denom();
    assert!(num == -4);
    assert!(den == 5);
    let one = Rational::from((num, Integer::from(-4)));
    assert!(one == 1);
}

Methods

impl Rational
[src]

Constructs a new arbitrary-precision rational number with value 0.

Converts self to an f64, rounding towards zero.

Converts self to an f32, rounding towards zero.

Borrows the numerator.

Borrows the denominator.

Borrows the numerator and denominator.

Borrows the numerator and denominator mutably. The number is canonicalized when the borrow ends. The denominator must not be zero when the borrow ends.

Examples

use rugrat::Rational;

let mut r = Rational::from((3, 5));
{
    let mut num_den = r.as_mut_numer_denom();
    // change r from 3/5 to 4/8, which is equal to 1/2
    *num_den.0 += 1;
    *num_den.1 += 3;
    // borrow ends here
}
let num_den = r.as_numer_denom();
assert!(*num_den.0 == 1 && *num_den.1 == 2);

Panics

Panics if the denominator is zero when the borrow ends.

Converts self into numerator and denominator integers, consuming self.

Computes the absolute value of self

Computes the reciprocal of self.

Panics

Panics if the value is zero.

Returns Less if self is less than zero, Greater if self is greater than zero, or Equal if self is equal to zero.

Returns a string representation of self for the specified radix.

Examples

use rugrat::Rational;
let r1 = Rational::from(0);
assert!(r1.to_string_radix(10) == "0");
let r2 = Rational::from((15, 5));
assert!(r2.to_string_radix(10) == "3");
let r3 = Rational::from((10, -6));
assert!(r3.to_string_radix(10) == "-5/3");
assert!(r3.to_string_radix(5) == "-10/3");

Panics if radix is less than 2 or greater than 36.

Parses a Rational number.

See the corresponding assignment.

Panics

Panics if radix is less than 2 or greater than 36.

Parses a Rational number from a string.

Examples

use rugrat::Rational;
let mut r = Rational::new();
let ret = r.assign_str("1/0");
assert!(ret.is_err());
r.assign_str("-24/2").unwrap();
assert!(*r.numer() == -12);
assert!(*r.denom() == 1);

Parses a Rational number from a string with the specified radix.

Examples

use rugrat::Rational;
let mut r = Rational::new();
r.assign_str_radix("ff/a", 16).unwrap();
assert!(r == (255, 10));
r.assign_str_radix("+ff0/a0", 16).unwrap();
assert!(r == (255, 10));

Panics

Panics if radix is less than 2 or greater than 36.

Checks if a Rational number can be parsed.

If this method does not return an error, neither will any other function that parses a Rational number. If this method returns an error, the other functions will return the same error.

Panics

Panics if radix is less than 2 or greater than 36.

Trait Implementations

impl Drop for Rational
[src]

A method called when the value goes out of scope. Read more

impl Default for Rational
[src]

Returns the "default value" for a type. Read more

impl Clone for Rational
[src]

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

impl FromStr for Rational
[src]

The associated error which can be returned from parsing.

Parses a Rational number.

See the corresponding assignment.

impl<'a> From<&'a Rational> for Rational
[src]

Constructs a Rational number from another Rational number.

impl From<Integer> for Rational
[src]

Constructs a Rational number from an Integer.

This constructor allocates one new Integer and reuses the allocation for val.

impl<'a> From<&'a Integer> for Rational
[src]

Constructs a Rational number from an Integer.

impl From<(Integer, Integer)> for Rational
[src]

Constructs a Rational number from a numerator Integer and denominator Integer.

This constructor does not allocate, as it reuses the Integer components.

Panics

Panics if the denominator is zero.

impl<'a> From<(&'a Integer, &'a Integer)> for Rational
[src]

Constructs a Rational number from a numerator Integer and denominator Integer.

impl From<u32> for Rational
[src]

Constructs a Rational number from a u32.

impl From<i32> for Rational
[src]

Constructs a Rational number from an i32.

impl From<f64> for Rational
[src]

Constructs a Rational number from an f64, losing no precision.

Panics

Panics if t is a NaN or infinite.

impl From<f32> for Rational
[src]

Constructs a Rational number from an f32, losing no precision.

Panics

Panics if t is a NaN or infinite.

impl From<(u32, u32)> for Rational
[src]

Constructs a Rational number from a numerator u32 and denominator u32.

Panics

Panics if the denominator is zero.

impl From<(i32, u32)> for Rational
[src]

Constructs a Rational number from a numerator i32 and denominator u32.

Panics

Panics if the denominator is zero.

impl From<(u32, i32)> for Rational
[src]

Constructs a Rational number from a numerator u32 and denominator i32.

Panics

Panics if the denominator is zero.

impl From<(i32, i32)> for Rational
[src]

Constructs a Rational number from a numerator i32 and denominator i32.

Panics

Panics if the denominator is zero.

impl<'a> Assign<&'a Rational> for Rational
[src]

Assigns from another Rational number.

impl Assign<Rational> for Rational
[src]

Assigns from another Rational number.

impl<'a> Assign<&'a Integer> for Rational
[src]

Assigns from an Integer.

impl Assign<Integer> for Rational
[src]

Assigns from an Integer.

impl Assign<(Integer, Integer)> for Rational
[src]

Assigns from a numerator Integer and a denominator Integer.

# Panics

Panics if the denominator is zero.

impl<'a> Assign<(&'a Integer, &'a Integer)> for Rational
[src]

Assigns from a numerator Integer and a denominator Integer.

Panics

Panics if the denominator is zero.

impl Assign<u32> for Rational
[src]

Assigns from a u32.

impl Assign<i32> for Rational
[src]

Assigns from an i32.

impl Assign<f64> for Rational
[src]

Assigns from an f64, losing no precision.

Panics

Panics if f is a NaN or infinite.

impl Assign<f32> for Rational
[src]

Assigns from an f32, losing no precision.

Panics

Panics if f is a NaN or infinite.

impl Assign<(u32, u32)> for Rational
[src]

Assigns from a numerator u32 and a denominator u32.

# Panics

Panics if the denominator is zero.

impl Assign<(i32, u32)> for Rational
[src]

Assigns from a numerator i32 and a denominator u32.

# Panics

Panics if the denominator is zero.

impl Assign<(u32, i32)> for Rational
[src]

Assigns from a numerator u32 and a denominator i32.

# Panics

Panics if the denominator is zero.

impl Assign<(i32, i32)> for Rational
[src]

Assigns from a numerator i32 and a denominator i32.

# Panics

Panics if the denominator is zero.

impl<'a> Add<&'a Rational> for Rational
[src]

The resulting type after applying the + operator

The method for the + operator

impl Add<Rational> for Rational
[src]

The resulting type after applying the + operator

The method for the + operator

impl<'a> AddAssign<&'a Rational> for Rational
[src]

The method for the += operator

impl AddAssign<Rational> for Rational
[src]

The method for the += operator

impl<'a> Sub<&'a Rational> for Rational
[src]

The resulting type after applying the - operator

The method for the - operator

impl Sub<Rational> for Rational
[src]

The resulting type after applying the - operator

The method for the - operator

impl<'a> SubAssign<&'a Rational> for Rational
[src]

The method for the -= operator

impl SubAssign<Rational> for Rational
[src]

The method for the -= operator

impl<'a> SubFromAssign<&'a Rational> for Rational
[src]

Peforms the subtraction.

impl SubFromAssign<Rational> for Rational
[src]

Peforms the subtraction.

impl<'a> Mul<&'a Rational> for Rational
[src]

The resulting type after applying the * operator

The method for the * operator

impl Mul<Rational> for Rational
[src]

The resulting type after applying the * operator

The method for the * operator

impl<'a> MulAssign<&'a Rational> for Rational
[src]

The method for the *= operator

impl MulAssign<Rational> for Rational
[src]

The method for the *= operator

impl<'a> Div<&'a Rational> for Rational
[src]

The resulting type after applying the / operator

The method for the / operator

impl Div<Rational> for Rational
[src]

The resulting type after applying the / operator

The method for the / operator

impl<'a> DivAssign<&'a Rational> for Rational
[src]

The method for the /= operator

impl DivAssign<Rational> for Rational
[src]

The method for the /= operator

impl<'a> DivFromAssign<&'a Rational> for Rational
[src]

Peforms the division.

impl DivFromAssign<Rational> for Rational
[src]

Peforms the division.

impl Neg for Rational
[src]

The resulting type after applying the - operator

The method for the unary - operator

impl NegAssign for Rational
[src]

Peforms the negation.

impl Shl<u32> for Rational
[src]

The resulting type after applying the << operator

Multiplies self by 2 to the power of op.

impl ShlAssign<u32> for Rational
[src]

Multiplies self by 2 to the power of op.

impl Shr<u32> for Rational
[src]

The resulting type after applying the >> operator

Divides self by 2 to the power of op.

impl ShrAssign<u32> for Rational
[src]

Divides self by 2 to the power of op.

impl Shl<i32> for Rational
[src]

The resulting type after applying the << operator

Multiplies self by 2 to the power of op.

impl ShlAssign<i32> for Rational
[src]

Multiplies self by 2 to the power of op.

impl Shr<i32> for Rational
[src]

The resulting type after applying the >> operator

Divides self by 2 to the power of op.

impl ShrAssign<i32> for Rational
[src]

Divides self by 2 to the power of op.

impl Pow<u32> for Rational
[src]

The resulting type after the power operation.

Performs the power operation.

impl PowAssign<u32> for Rational
[src]

Peforms the power operation.

impl Pow<i32> for Rational
[src]

The resulting type after the power operation.

Performs the power operation.

impl PowAssign<i32> for Rational
[src]

Peforms the power operation.

impl Eq for Rational
[src]

impl Ord for Rational
[src]

This method returns an Ordering between self and other. Read more

impl PartialEq for Rational
[src]

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

impl PartialOrd for Rational
[src]

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

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

impl PartialEq<Integer> for Rational
[src]

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

impl PartialOrd<Integer> for Rational
[src]

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

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

impl PartialEq<u32> for Rational
[src]

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

impl PartialOrd<u32> for Rational
[src]

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

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

impl PartialEq<i32> for Rational
[src]

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

impl PartialOrd<i32> for Rational
[src]

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

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

impl PartialEq<(u32, u32)> for Rational
[src]

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

impl PartialOrd<(u32, u32)> for Rational
[src]

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

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

impl PartialEq<(i32, u32)> for Rational
[src]

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

impl PartialOrd<(i32, u32)> for Rational
[src]

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

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

impl PartialEq<(u32, i32)> for Rational
[src]

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

impl PartialOrd<(u32, i32)> for Rational
[src]

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

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

impl PartialEq<(i32, i32)> for Rational
[src]

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

impl PartialOrd<(i32, i32)> for Rational
[src]

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

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

impl Display for Rational
[src]

Formats the value using the given formatter. Read more

impl Debug for Rational
[src]

Formats the value using the given formatter.

impl Binary for Rational
[src]

Formats the value using the given formatter.

impl Octal for Rational
[src]

Formats the value using the given formatter.

impl LowerHex for Rational
[src]

Formats the value using the given formatter.

impl UpperHex for Rational
[src]

Formats the value using the given formatter.