pub struct Rational { /* private fields */ }
Expand description
A rational number (a fraction of two integers).
Implementations§
source§impl Rational
impl Rational
sourcepub fn new_checked<N, D>(numerator: N, denominator: D) -> Option<Self>where
Self: From<N> + From<D>,
pub fn new_checked<N, D>(numerator: N, denominator: D) -> Option<Self>where
Self: From<N> + From<D>,
Construct a new Rational, returning None
if the denominator is 0.
sourcepub fn from_mixed<T>(whole: i128, fract: T) -> Selfwhere
Self: From<T>,
pub fn from_mixed<T>(whole: i128, fract: T) -> Selfwhere
Self: From<T>,
Create a Rational
from a mixed fraction.
Example
assert_eq!(Rational::from_mixed(1, (1, 2)), Rational::new(3, 2));
assert_eq!(Rational::from_mixed(-1, (-1, 2)), Rational::new(-3, 2));
sourcepub fn integer(n: i128) -> Self
pub fn integer(n: i128) -> Self
Shorthand for creating an integer Rational
, eg. 5/1.
Example
assert_eq!(Rational::integer(5), Rational::new(5, 1));
sourcepub fn numerator(&self) -> i128
pub fn numerator(&self) -> i128
Get the numerator in this Rational
.
Example
let r = Rational::new(4, 6);
assert_eq!(r.numerator(), 2); // `r` has been reduced to 2/3
sourcepub fn set_numerator(&mut self, numerator: i128)
pub fn set_numerator(&mut self, numerator: i128)
Set the numerator of this Rational
. It is then automatically reduced.
Example
let mut r = Rational::new(4, 5);
r.set_numerator(10);
assert_eq!(r, Rational::new(2, 1)); // 10/5 reduces to 2/1
sourcepub fn denominator(&self) -> i128
pub fn denominator(&self) -> i128
Get the denominator in this Rational
.
Example
let r = Rational::new(4, 6);
assert_eq!(r.denominator(), 3); // `r` has been reduced to 2/3
sourcepub fn set_denominator(&mut self, denominator: i128)
pub fn set_denominator(&mut self, denominator: i128)
sourcepub fn inverse_checked(self) -> Option<Self>
pub fn inverse_checked(self) -> Option<Self>
Returns the inverse of this Rational
, or None
if the denominator of the inverse is 0.
sourcepub fn inverse(self) -> Self
pub fn inverse(self) -> Self
Returns the inverse of this Rational
.
Panics
- If the numerator is 0, since then the inverse will be divided by 0.
sourcepub fn decimal_value(self) -> f64
pub fn decimal_value(self) -> f64
Returns the decimal value of this Rational
.
Equivalent to f64::from(self)
.
pub fn checked_add<T>(self, other: T) -> Option<Self>where
Self: From<T>,
pub fn checked_mul<T>(self, other: T) -> Option<Self>where
Self: From<T>,
pub fn checked_sub<T>(self, other: T) -> Option<Self>where
Self: From<T>,
pub fn checked_div<T>(self, other: T) -> Option<Self>where
Self: From<T>,
sourcepub fn pow(self, exp: i32) -> Self
pub fn pow(self, exp: i32) -> Self
Raises self to the power of exp
.
Notes
Unlike the pow
methods in std
, this supports negative exponents, returning the inverse of the result.
The exponent still needs to be an integer, since a rational number raised to the power of another rational number may be irrational.
Panics
- If the numerator is 0 and
exp
is negative (since a negative exponent will result in an inversed fraction).
Example
assert_eq!(Rational::new(2, 3).pow(2), Rational::new(4, 9));
assert_eq!(Rational::new(1, 4).pow(-2), Rational::new(16, 1));
pub fn checked_pow(self, exp: i32) -> Option<Self>
sourcepub fn abs(self) -> Self
pub fn abs(self) -> Self
Computes the absolute value of self
.
Example
assert_eq!(Rational::new(-5, 3).abs(), Rational::new(5, 3));
sourcepub fn is_integer(&self) -> bool
pub fn is_integer(&self) -> bool
Returns true
if self
is an integer.
This is a shorthand for self.denominator() == 1
.
Example
assert!(Rational::new(2, 1).is_integer());
assert!(!Rational::new(1, 2).is_integer());
sourcepub fn is_negative(&self) -> bool
pub fn is_negative(&self) -> bool
Returns true
if self
is negative.
Example
assert!(Rational::new(-1, 2).is_negative());
assert!(Rational::new(1, -2).is_negative());
assert!(!Rational::new(1, 2).is_negative());
sourcepub fn mixed_fraction(self) -> (i128, Self)
pub fn mixed_fraction(self) -> (i128, Self)
Returns a tuple representing self
as a mixed fraction.
Notes
The result is a tuple (whole: i128, fraction: Rational)
, such that whole + fraction == self
.
This means that while you might write -7/2 as a mixed fraction: -3½, the result will be a tuple (-3, -1/2).
Example
assert_eq!(Rational::new(7, 3).mixed_fraction(), (2, Rational::new(1, 3)));
let (mixed, fract) = Rational::new(-7, 2).mixed_fraction();
assert_eq!((mixed, fract), (-3, Rational::new(-1, 2)));
assert_eq!(mixed + fract, Rational::new(-7, 2));
Trait Implementations§
source§impl AddAssign<Rational> for Rational
impl AddAssign<Rational> for Rational
source§fn add_assign(&mut self, rhs: Rational)
fn add_assign(&mut self, rhs: Rational)
+=
operation. Read moresource§impl AddAssign<i128> for Rational
impl AddAssign<i128> for Rational
source§fn add_assign(&mut self, rhs: i128)
fn add_assign(&mut self, rhs: i128)
+=
operation. Read moresource§impl AddAssign<i16> for Rational
impl AddAssign<i16> for Rational
source§fn add_assign(&mut self, rhs: i16)
fn add_assign(&mut self, rhs: i16)
+=
operation. Read moresource§impl AddAssign<i32> for Rational
impl AddAssign<i32> for Rational
source§fn add_assign(&mut self, rhs: i32)
fn add_assign(&mut self, rhs: i32)
+=
operation. Read moresource§impl AddAssign<i64> for Rational
impl AddAssign<i64> for Rational
source§fn add_assign(&mut self, rhs: i64)
fn add_assign(&mut self, rhs: i64)
+=
operation. Read moresource§impl AddAssign<i8> for Rational
impl AddAssign<i8> for Rational
source§fn add_assign(&mut self, rhs: i8)
fn add_assign(&mut self, rhs: i8)
+=
operation. Read moresource§impl AddAssign<isize> for Rational
impl AddAssign<isize> for Rational
source§fn add_assign(&mut self, rhs: isize)
fn add_assign(&mut self, rhs: isize)
+=
operation. Read moresource§impl AddAssign<u16> for Rational
impl AddAssign<u16> for Rational
source§fn add_assign(&mut self, rhs: u16)
fn add_assign(&mut self, rhs: u16)
+=
operation. Read moresource§impl AddAssign<u32> for Rational
impl AddAssign<u32> for Rational
source§fn add_assign(&mut self, rhs: u32)
fn add_assign(&mut self, rhs: u32)
+=
operation. Read moresource§impl AddAssign<u64> for Rational
impl AddAssign<u64> for Rational
source§fn add_assign(&mut self, rhs: u64)
fn add_assign(&mut self, rhs: u64)
+=
operation. Read moresource§impl AddAssign<u8> for Rational
impl AddAssign<u8> for Rational
source§fn add_assign(&mut self, rhs: u8)
fn add_assign(&mut self, rhs: u8)
+=
operation. Read moresource§impl DivAssign<Rational> for Rational
impl DivAssign<Rational> for Rational
source§fn div_assign(&mut self, rhs: Rational)
fn div_assign(&mut self, rhs: Rational)
/=
operation. Read moresource§impl DivAssign<i128> for Rational
impl DivAssign<i128> for Rational
source§fn div_assign(&mut self, rhs: i128)
fn div_assign(&mut self, rhs: i128)
/=
operation. Read moresource§impl DivAssign<i16> for Rational
impl DivAssign<i16> for Rational
source§fn div_assign(&mut self, rhs: i16)
fn div_assign(&mut self, rhs: i16)
/=
operation. Read moresource§impl DivAssign<i32> for Rational
impl DivAssign<i32> for Rational
source§fn div_assign(&mut self, rhs: i32)
fn div_assign(&mut self, rhs: i32)
/=
operation. Read moresource§impl DivAssign<i64> for Rational
impl DivAssign<i64> for Rational
source§fn div_assign(&mut self, rhs: i64)
fn div_assign(&mut self, rhs: i64)
/=
operation. Read moresource§impl DivAssign<i8> for Rational
impl DivAssign<i8> for Rational
source§fn div_assign(&mut self, rhs: i8)
fn div_assign(&mut self, rhs: i8)
/=
operation. Read moresource§impl DivAssign<isize> for Rational
impl DivAssign<isize> for Rational
source§fn div_assign(&mut self, rhs: isize)
fn div_assign(&mut self, rhs: isize)
/=
operation. Read moresource§impl DivAssign<u16> for Rational
impl DivAssign<u16> for Rational
source§fn div_assign(&mut self, rhs: u16)
fn div_assign(&mut self, rhs: u16)
/=
operation. Read moresource§impl DivAssign<u32> for Rational
impl DivAssign<u32> for Rational
source§fn div_assign(&mut self, rhs: u32)
fn div_assign(&mut self, rhs: u32)
/=
operation. Read moresource§impl DivAssign<u64> for Rational
impl DivAssign<u64> for Rational
source§fn div_assign(&mut self, rhs: u64)
fn div_assign(&mut self, rhs: u64)
/=
operation. Read moresource§impl DivAssign<u8> for Rational
impl DivAssign<u8> for Rational
source§fn div_assign(&mut self, rhs: u8)
fn div_assign(&mut self, rhs: u8)
/=
operation. Read moresource§impl MulAssign<Rational> for Rational
impl MulAssign<Rational> for Rational
source§fn mul_assign(&mut self, rhs: Rational)
fn mul_assign(&mut self, rhs: Rational)
*=
operation. Read moresource§impl MulAssign<i128> for Rational
impl MulAssign<i128> for Rational
source§fn mul_assign(&mut self, rhs: i128)
fn mul_assign(&mut self, rhs: i128)
*=
operation. Read moresource§impl MulAssign<i16> for Rational
impl MulAssign<i16> for Rational
source§fn mul_assign(&mut self, rhs: i16)
fn mul_assign(&mut self, rhs: i16)
*=
operation. Read moresource§impl MulAssign<i32> for Rational
impl MulAssign<i32> for Rational
source§fn mul_assign(&mut self, rhs: i32)
fn mul_assign(&mut self, rhs: i32)
*=
operation. Read moresource§impl MulAssign<i64> for Rational
impl MulAssign<i64> for Rational
source§fn mul_assign(&mut self, rhs: i64)
fn mul_assign(&mut self, rhs: i64)
*=
operation. Read moresource§impl MulAssign<i8> for Rational
impl MulAssign<i8> for Rational
source§fn mul_assign(&mut self, rhs: i8)
fn mul_assign(&mut self, rhs: i8)
*=
operation. Read moresource§impl MulAssign<isize> for Rational
impl MulAssign<isize> for Rational
source§fn mul_assign(&mut self, rhs: isize)
fn mul_assign(&mut self, rhs: isize)
*=
operation. Read moresource§impl MulAssign<u16> for Rational
impl MulAssign<u16> for Rational
source§fn mul_assign(&mut self, rhs: u16)
fn mul_assign(&mut self, rhs: u16)
*=
operation. Read moresource§impl MulAssign<u32> for Rational
impl MulAssign<u32> for Rational
source§fn mul_assign(&mut self, rhs: u32)
fn mul_assign(&mut self, rhs: u32)
*=
operation. Read moresource§impl MulAssign<u64> for Rational
impl MulAssign<u64> for Rational
source§fn mul_assign(&mut self, rhs: u64)
fn mul_assign(&mut self, rhs: u64)
*=
operation. Read moresource§impl MulAssign<u8> for Rational
impl MulAssign<u8> for Rational
source§fn mul_assign(&mut self, rhs: u8)
fn mul_assign(&mut self, rhs: u8)
*=
operation. Read moresource§impl Ord for Rational
impl Ord for Rational
source§impl PartialEq<Rational> for Rational
impl PartialEq<Rational> for Rational
source§impl PartialOrd<Rational> for Rational
impl PartialOrd<Rational> for Rational
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl RemAssign<Rational> for Rational
impl RemAssign<Rational> for Rational
source§fn rem_assign(&mut self, rhs: Rational)
fn rem_assign(&mut self, rhs: Rational)
%=
operation. Read moresource§impl RemAssign<i128> for Rational
impl RemAssign<i128> for Rational
source§fn rem_assign(&mut self, rhs: i128)
fn rem_assign(&mut self, rhs: i128)
%=
operation. Read moresource§impl RemAssign<i16> for Rational
impl RemAssign<i16> for Rational
source§fn rem_assign(&mut self, rhs: i16)
fn rem_assign(&mut self, rhs: i16)
%=
operation. Read moresource§impl RemAssign<i32> for Rational
impl RemAssign<i32> for Rational
source§fn rem_assign(&mut self, rhs: i32)
fn rem_assign(&mut self, rhs: i32)
%=
operation. Read moresource§impl RemAssign<i64> for Rational
impl RemAssign<i64> for Rational
source§fn rem_assign(&mut self, rhs: i64)
fn rem_assign(&mut self, rhs: i64)
%=
operation. Read moresource§impl RemAssign<i8> for Rational
impl RemAssign<i8> for Rational
source§fn rem_assign(&mut self, rhs: i8)
fn rem_assign(&mut self, rhs: i8)
%=
operation. Read moresource§impl RemAssign<isize> for Rational
impl RemAssign<isize> for Rational
source§fn rem_assign(&mut self, rhs: isize)
fn rem_assign(&mut self, rhs: isize)
%=
operation. Read moresource§impl RemAssign<u16> for Rational
impl RemAssign<u16> for Rational
source§fn rem_assign(&mut self, rhs: u16)
fn rem_assign(&mut self, rhs: u16)
%=
operation. Read moresource§impl RemAssign<u32> for Rational
impl RemAssign<u32> for Rational
source§fn rem_assign(&mut self, rhs: u32)
fn rem_assign(&mut self, rhs: u32)
%=
operation. Read moresource§impl RemAssign<u64> for Rational
impl RemAssign<u64> for Rational
source§fn rem_assign(&mut self, rhs: u64)
fn rem_assign(&mut self, rhs: u64)
%=
operation. Read moresource§impl RemAssign<u8> for Rational
impl RemAssign<u8> for Rational
source§fn rem_assign(&mut self, rhs: u8)
fn rem_assign(&mut self, rhs: u8)
%=
operation. Read moresource§impl SubAssign<Rational> for Rational
impl SubAssign<Rational> for Rational
source§fn sub_assign(&mut self, rhs: Rational)
fn sub_assign(&mut self, rhs: Rational)
-=
operation. Read moresource§impl SubAssign<i128> for Rational
impl SubAssign<i128> for Rational
source§fn sub_assign(&mut self, rhs: i128)
fn sub_assign(&mut self, rhs: i128)
-=
operation. Read moresource§impl SubAssign<i16> for Rational
impl SubAssign<i16> for Rational
source§fn sub_assign(&mut self, rhs: i16)
fn sub_assign(&mut self, rhs: i16)
-=
operation. Read moresource§impl SubAssign<i32> for Rational
impl SubAssign<i32> for Rational
source§fn sub_assign(&mut self, rhs: i32)
fn sub_assign(&mut self, rhs: i32)
-=
operation. Read moresource§impl SubAssign<i64> for Rational
impl SubAssign<i64> for Rational
source§fn sub_assign(&mut self, rhs: i64)
fn sub_assign(&mut self, rhs: i64)
-=
operation. Read moresource§impl SubAssign<i8> for Rational
impl SubAssign<i8> for Rational
source§fn sub_assign(&mut self, rhs: i8)
fn sub_assign(&mut self, rhs: i8)
-=
operation. Read moresource§impl SubAssign<isize> for Rational
impl SubAssign<isize> for Rational
source§fn sub_assign(&mut self, rhs: isize)
fn sub_assign(&mut self, rhs: isize)
-=
operation. Read moresource§impl SubAssign<u16> for Rational
impl SubAssign<u16> for Rational
source§fn sub_assign(&mut self, rhs: u16)
fn sub_assign(&mut self, rhs: u16)
-=
operation. Read moresource§impl SubAssign<u32> for Rational
impl SubAssign<u32> for Rational
source§fn sub_assign(&mut self, rhs: u32)
fn sub_assign(&mut self, rhs: u32)
-=
operation. Read moresource§impl SubAssign<u64> for Rational
impl SubAssign<u64> for Rational
source§fn sub_assign(&mut self, rhs: u64)
fn sub_assign(&mut self, rhs: u64)
-=
operation. Read moresource§impl SubAssign<u8> for Rational
impl SubAssign<u8> for Rational
source§fn sub_assign(&mut self, rhs: u8)
fn sub_assign(&mut self, rhs: u8)
-=
operation. Read more