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]
fn new() -> Rational
Constructs a new arbitrary-precision rational number with value 0.
fn to_f64(&self) -> f64
Converts self
to an f64
, rounding towards zero.
fn to_f32(&self) -> f32
Converts self
to an f32
, rounding towards zero.
fn numer(&self) -> &Integer
Borrows the numerator.
fn denom(&self) -> &Integer
Borrows the denominator.
fn as_numer_denom(&self) -> (&Integer, &Integer)
Borrows the numerator and denominator.
fn as_mut_numer_denom(&mut self) -> MutNumerDenom
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.
fn into_numer_denom(self) -> (Integer, Integer)
Converts self
into numerator and denominator integers,
consuming self
.
fn abs(&mut self) -> &mut Rational
Computes the absolute value of self
fn recip(&mut self) -> &mut Rational
fn sign(&self) -> Ordering
Returns Less
if self
is less than zero,
Greater
if self
is greater than zero,
or Equal
if self
is equal to zero.
fn to_string_radix(&self, radix: i32) -> String
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.
fn from_str_radix(src: &str, radix: i32) -> Result<Rational, ParseRationalError>
Parses a Rational
number.
See the corresponding assignment.
Panics
Panics if radix
is less than 2 or greater than 36.
fn assign_str(&mut self, src: &str) -> Result<(), ParseRationalError>
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);
fn assign_str_radix(
&mut self,
src: &str,
radix: i32
) -> Result<(), ParseRationalError>
&mut self,
src: &str,
radix: i32
) -> Result<(), ParseRationalError>
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.
fn valid_str_radix(src: &str, radix: i32) -> Result<(), ParseRationalError>
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]
impl Default for Rational
[src]
impl Clone for Rational
[src]
fn clone(&self) -> Rational
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Rational)
Performs copy-assignment from source
. Read more
impl FromStr for Rational
[src]
type Err = ParseRationalError
The associated error which can be returned from parsing.
fn from_str(src: &str) -> Result<Rational, ParseRationalError>
Parses a Rational
number.
See the corresponding assignment.
impl<'a> From<&'a Rational> for Rational
[src]
impl From<Integer> for Rational
[src]
fn from(val: Integer) -> Rational
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]
impl From<(Integer, Integer)> for Rational
[src]
fn from((num, den): (Integer, Integer)) -> Rational
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]
fn from(t: (&'a Integer, &'a Integer)) -> Rational
Constructs a Rational
number from
a numerator Integer
and denominator Integer
.
impl From<u32> for Rational
[src]
impl From<i32> for Rational
[src]
impl From<f64> for Rational
[src]
fn from(t: f64) -> Rational
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]
fn from(t: f32) -> Rational
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]
fn from(t: (u32, u32)) -> Rational
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]
fn from(t: (i32, u32)) -> Rational
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]
fn from(t: (u32, i32)) -> Rational
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]
fn from(t: (i32, i32)) -> Rational
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]
impl Assign<Rational> for Rational
[src]
impl<'a> Assign<&'a Integer> for Rational
[src]
fn assign(&mut self, val: &'a Integer)
Assigns from an Integer
.
impl Assign<Integer> for Rational
[src]
fn assign(&mut self, other: Integer)
Assigns from
an Integer
.
impl Assign<(Integer, Integer)> for Rational
[src]
fn assign(&mut self, (num, den): (Integer, Integer))
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]
fn assign(&mut self, (num, den): (&Integer, &Integer))
Assigns from a numerator Integer
and a denominator Integer
.
Panics
Panics if the denominator is zero.
impl Assign<u32> for Rational
[src]
impl Assign<i32> for Rational
[src]
impl Assign<f64> for Rational
[src]
impl Assign<f32> for Rational
[src]
impl Assign<(u32, u32)> for Rational
[src]
fn assign(&mut self, (num, den): (u32, u32))
Assigns from
a numerator u32
and a denominator u32
.
# Panics
Panics if the denominator is zero.
impl Assign<(i32, u32)> for Rational
[src]
fn assign(&mut self, (num, den): (i32, u32))
Assigns from
a numerator i32
and a denominator u32
.
# Panics
Panics if the denominator is zero.
impl Assign<(u32, i32)> for Rational
[src]
fn assign(&mut self, (num, den): (u32, i32))
Assigns from
a numerator u32
and a denominator i32
.
# Panics
Panics if the denominator is zero.
impl Assign<(i32, i32)> for Rational
[src]
fn assign(&mut self, (num, den): (i32, i32))
Assigns from
a numerator i32
and a denominator i32
.
# Panics
Panics if the denominator is zero.
impl<'a> Add<&'a Rational> for Rational
[src]
type Output = Rational
The resulting type after applying the +
operator
fn add(self, op: &'a Rational) -> Rational
The method for the +
operator
impl Add<Rational> for Rational
[src]
type Output = Rational
The resulting type after applying the +
operator
fn add(self, op: Rational) -> Rational
The method for the +
operator
impl<'a> AddAssign<&'a Rational> for Rational
[src]
fn add_assign(&mut self, op: &'a Rational)
The method for the +=
operator
impl AddAssign<Rational> for Rational
[src]
fn add_assign(&mut self, op: Rational)
The method for the +=
operator
impl<'a> Sub<&'a Rational> for Rational
[src]
type Output = Rational
The resulting type after applying the -
operator
fn sub(self, op: &'a Rational) -> Rational
The method for the -
operator
impl Sub<Rational> for Rational
[src]
type Output = Rational
The resulting type after applying the -
operator
fn sub(self, op: Rational) -> Rational
The method for the -
operator
impl<'a> SubAssign<&'a Rational> for Rational
[src]
fn sub_assign(&mut self, op: &'a Rational)
The method for the -=
operator
impl SubAssign<Rational> for Rational
[src]
fn sub_assign(&mut self, op: Rational)
The method for the -=
operator
impl<'a> SubFromAssign<&'a Rational> for Rational
[src]
fn sub_from_assign(&mut self, lhs: &'a Rational)
Peforms the subtraction.
impl SubFromAssign<Rational> for Rational
[src]
fn sub_from_assign(&mut self, lhs: Rational)
Peforms the subtraction.
impl<'a> Mul<&'a Rational> for Rational
[src]
type Output = Rational
The resulting type after applying the *
operator
fn mul(self, op: &'a Rational) -> Rational
The method for the *
operator
impl Mul<Rational> for Rational
[src]
type Output = Rational
The resulting type after applying the *
operator
fn mul(self, op: Rational) -> Rational
The method for the *
operator
impl<'a> MulAssign<&'a Rational> for Rational
[src]
fn mul_assign(&mut self, op: &'a Rational)
The method for the *=
operator
impl MulAssign<Rational> for Rational
[src]
fn mul_assign(&mut self, op: Rational)
The method for the *=
operator
impl<'a> Div<&'a Rational> for Rational
[src]
type Output = Rational
The resulting type after applying the /
operator
fn div(self, op: &'a Rational) -> Rational
The method for the /
operator
impl Div<Rational> for Rational
[src]
type Output = Rational
The resulting type after applying the /
operator
fn div(self, op: Rational) -> Rational
The method for the /
operator
impl<'a> DivAssign<&'a Rational> for Rational
[src]
fn div_assign(&mut self, op: &'a Rational)
The method for the /=
operator
impl DivAssign<Rational> for Rational
[src]
fn div_assign(&mut self, op: Rational)
The method for the /=
operator
impl<'a> DivFromAssign<&'a Rational> for Rational
[src]
fn div_from_assign(&mut self, lhs: &'a Rational)
Peforms the division.
impl DivFromAssign<Rational> for Rational
[src]
fn div_from_assign(&mut self, lhs: Rational)
Peforms the division.
impl Neg for Rational
[src]
type Output = Rational
The resulting type after applying the -
operator
fn neg(self) -> Rational
The method for the unary -
operator
impl NegAssign for Rational
[src]
fn neg_assign(&mut self)
Peforms the negation.
impl Shl<u32> for Rational
[src]
type Output = Rational
The resulting type after applying the <<
operator
fn shl(self, op: u32) -> Rational
Multiplies self
by 2 to the power of op
.
impl ShlAssign<u32> for Rational
[src]
fn shl_assign(&mut self, op: u32)
Multiplies self
by 2 to the power of op
.
impl Shr<u32> for Rational
[src]
type Output = Rational
The resulting type after applying the >>
operator
fn shr(self, op: u32) -> Rational
Divides self
by 2 to the power of op
.
impl ShrAssign<u32> for Rational
[src]
fn shr_assign(&mut self, op: u32)
Divides self
by 2 to the power of op
.
impl Shl<i32> for Rational
[src]
type Output = Rational
The resulting type after applying the <<
operator
fn shl(self, op: i32) -> Rational
Multiplies self
by 2 to the power of op
.
impl ShlAssign<i32> for Rational
[src]
fn shl_assign(&mut self, op: i32)
Multiplies self
by 2 to the power of op
.
impl Shr<i32> for Rational
[src]
type Output = Rational
The resulting type after applying the >>
operator
fn shr(self, op: i32) -> Rational
Divides self
by 2 to the power of op
.
impl ShrAssign<i32> for Rational
[src]
fn shr_assign(&mut self, op: i32)
Divides self
by 2 to the power of op
.
impl Pow<u32> for Rational
[src]
type Output = Rational
The resulting type after the power operation.
fn pow(self, op: u32) -> Rational
Performs the power operation.
impl PowAssign<u32> for Rational
[src]
fn pow_assign(&mut self, op: u32)
Peforms the power operation.
impl Pow<i32> for Rational
[src]
type Output = Rational
The resulting type after the power operation.
fn pow(self, op: i32) -> Rational
Performs the power operation.
impl PowAssign<i32> for Rational
[src]
fn pow_assign(&mut self, op: i32)
Peforms the power operation.
impl Eq for Rational
[src]
impl Ord for Rational
[src]
fn cmp(&self, other: &Rational) -> Ordering
This method returns an Ordering
between self
and other
. Read more
impl PartialEq for Rational
[src]
fn eq(&self, other: &Rational) -> bool
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, other: &Rhs) -> bool
1.0.0
This method tests for !=
.
impl PartialOrd for Rational
[src]
fn partial_cmp(&self, other: &Rational) -> Option<Ordering>
This method returns an ordering between self
and other
values if one exists. Read more
fn lt(&self, other: &Rhs) -> bool
1.0.0
This method tests less than (for self
and other
) and is used by the <
operator. Read more
fn le(&self, other: &Rhs) -> bool
1.0.0
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
fn gt(&self, other: &Rhs) -> bool
1.0.0
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
fn ge(&self, other: &Rhs) -> bool
1.0.0
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]
fn eq(&self, other: &Integer) -> bool
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, other: &Rhs) -> bool
1.0.0
This method tests for !=
.
impl PartialOrd<Integer> for Rational
[src]
fn partial_cmp(&self, other: &Integer) -> Option<Ordering>
This method returns an ordering between self
and other
values if one exists. Read more
fn lt(&self, other: &Rhs) -> bool
1.0.0
This method tests less than (for self
and other
) and is used by the <
operator. Read more
fn le(&self, other: &Rhs) -> bool
1.0.0
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
fn gt(&self, other: &Rhs) -> bool
1.0.0
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
fn ge(&self, other: &Rhs) -> bool
1.0.0
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]
fn eq(&self, other: &u32) -> bool
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, other: &Rhs) -> bool
1.0.0
This method tests for !=
.
impl PartialOrd<u32> for Rational
[src]
fn partial_cmp(&self, other: &u32) -> Option<Ordering>
This method returns an ordering between self
and other
values if one exists. Read more
fn lt(&self, other: &Rhs) -> bool
1.0.0
This method tests less than (for self
and other
) and is used by the <
operator. Read more
fn le(&self, other: &Rhs) -> bool
1.0.0
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
fn gt(&self, other: &Rhs) -> bool
1.0.0
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
fn ge(&self, other: &Rhs) -> bool
1.0.0
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]
fn eq(&self, other: &i32) -> bool
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, other: &Rhs) -> bool
1.0.0
This method tests for !=
.
impl PartialOrd<i32> for Rational
[src]
fn partial_cmp(&self, other: &i32) -> Option<Ordering>
This method returns an ordering between self
and other
values if one exists. Read more
fn lt(&self, other: &Rhs) -> bool
1.0.0
This method tests less than (for self
and other
) and is used by the <
operator. Read more
fn le(&self, other: &Rhs) -> bool
1.0.0
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
fn gt(&self, other: &Rhs) -> bool
1.0.0
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
fn ge(&self, other: &Rhs) -> bool
1.0.0
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]
fn eq(&self, other: &(u32, u32)) -> bool
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, other: &Rhs) -> bool
1.0.0
This method tests for !=
.
impl PartialOrd<(u32, u32)> for Rational
[src]
fn partial_cmp(&self, other: &(u32, u32)) -> Option<Ordering>
This method returns an ordering between self
and other
values if one exists. Read more
fn lt(&self, other: &Rhs) -> bool
1.0.0
This method tests less than (for self
and other
) and is used by the <
operator. Read more
fn le(&self, other: &Rhs) -> bool
1.0.0
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
fn gt(&self, other: &Rhs) -> bool
1.0.0
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
fn ge(&self, other: &Rhs) -> bool
1.0.0
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]
fn eq(&self, other: &(i32, u32)) -> bool
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, other: &Rhs) -> bool
1.0.0
This method tests for !=
.
impl PartialOrd<(i32, u32)> for Rational
[src]
fn partial_cmp(&self, other: &(i32, u32)) -> Option<Ordering>
This method returns an ordering between self
and other
values if one exists. Read more
fn lt(&self, other: &Rhs) -> bool
1.0.0
This method tests less than (for self
and other
) and is used by the <
operator. Read more
fn le(&self, other: &Rhs) -> bool
1.0.0
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
fn gt(&self, other: &Rhs) -> bool
1.0.0
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
fn ge(&self, other: &Rhs) -> bool
1.0.0
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]
fn eq(&self, other: &(u32, i32)) -> bool
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, other: &Rhs) -> bool
1.0.0
This method tests for !=
.
impl PartialOrd<(u32, i32)> for Rational
[src]
fn partial_cmp(&self, other: &(u32, i32)) -> Option<Ordering>
This method returns an ordering between self
and other
values if one exists. Read more
fn lt(&self, other: &Rhs) -> bool
1.0.0
This method tests less than (for self
and other
) and is used by the <
operator. Read more
fn le(&self, other: &Rhs) -> bool
1.0.0
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
fn gt(&self, other: &Rhs) -> bool
1.0.0
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
fn ge(&self, other: &Rhs) -> bool
1.0.0
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]
fn eq(&self, other: &(i32, i32)) -> bool
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, other: &Rhs) -> bool
1.0.0
This method tests for !=
.
impl PartialOrd<(i32, i32)> for Rational
[src]
fn partial_cmp(&self, other: &(i32, i32)) -> Option<Ordering>
This method returns an ordering between self
and other
values if one exists. Read more
fn lt(&self, other: &Rhs) -> bool
1.0.0
This method tests less than (for self
and other
) and is used by the <
operator. Read more
fn le(&self, other: &Rhs) -> bool
1.0.0
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
fn gt(&self, other: &Rhs) -> bool
1.0.0
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
fn ge(&self, other: &Rhs) -> bool
1.0.0
This method tests greater than or equal to (for self
and other
) and is used by the >=
operator. Read more