pub struct Rational(/* private fields */);Expand description
An arbitrary-precision exact rational number, always kept in lowest terms.
It supports the usual arithmetic (+ - * /, on values and references), comparisons,
Display (3/2, -4, never a decimal) and parsing from the same
notation.
use simplex_trace::{Rational, ratio};
let a = ratio(3, 2);
let b: Rational = "7/4".parse().unwrap();
let c = Rational::from(5);
let d = &a + &b * c;
assert_eq!(d, ratio(41, 4));
assert_eq!(d.to_string(), "41/4");
assert_eq!(d.to_i64_parts(), Some((41, 4)));
assert_eq!(d.to_f64(), 10.25);The representation is an implementation detail, so the crate can change it (for example
to speed up small numbers) without breaking users. With the num-interop feature it
converts to and from num_rational::BigRational.
With the serde feature it is a JSON string such as "3/2". JSON numbers are rejected,
so nothing is ever rounded.
Implementations§
Source§impl Rational
impl Rational
Sourcepub fn new(numer: i64, denom: i64) -> Self
pub fn new(numer: i64, denom: i64) -> Self
The fraction numer / denom, reduced. Panics if denom == 0.
pub fn zero() -> Self
pub fn one() -> Self
pub fn is_zero(&self) -> bool
pub fn is_positive(&self) -> bool
pub fn is_negative(&self) -> bool
Sourcepub fn is_integer(&self) -> bool
pub fn is_integer(&self) -> bool
true when the denominator is 1.
pub fn abs(&self) -> Self
Sourcepub fn to_i64_parts(&self) -> Option<(i64, i64)>
pub fn to_i64_parts(&self) -> Option<(i64, i64)>
Numerator and (positive) denominator in lowest terms, if both fit in an i64: for
example to draw a stacked fraction.
Source§impl Rational
impl Rational
Sourcepub fn as_big_rational(&self) -> &BigRational
pub fn as_big_rational(&self) -> &BigRational
The underlying num_rational::BigRational.
Trait Implementations§
Source§impl AddAssign for Rational
impl AddAssign for Rational
Source§fn add_assign(&mut self, rhs: Rational)
fn add_assign(&mut self, rhs: Rational)
+= operation. Read moreSource§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<'de> Deserialize<'de> for Rational
Available on crate feature serde only.
impl<'de> Deserialize<'de> for Rational
serde only.Source§fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error>
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error>
Source§impl DivAssign for Rational
impl DivAssign for Rational
Source§fn div_assign(&mut self, rhs: Rational)
fn div_assign(&mut self, rhs: Rational)
/= 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 moreimpl Eq for Rational
Source§impl From<Ratio<BigInt>> for Rational
Available on crate feature num-interop only.
impl From<Ratio<BigInt>> for Rational
num-interop only.Source§fn from(r: BigRational) -> Self
fn from(r: BigRational) -> Self
Source§impl From<Rational> for BigRational
Available on crate feature num-interop only.
impl From<Rational> for BigRational
num-interop only.Source§impl FromStr for Rational
Parses n or n/d (integers, d != 0), such as "3", "-1/2". Decimals such as
"0.5" are rejected: write "1/2".
impl FromStr for Rational
Parses n or n/d (integers, d != 0), such as "3", "-1/2". Decimals such as
"0.5" are rejected: write "1/2".
Source§impl IntoRational for Rational
impl IntoRational for Rational
fn into_rational(self) -> Rational
Source§impl MulAssign for Rational
impl MulAssign for Rational
Source§fn mul_assign(&mut self, rhs: Rational)
fn mul_assign(&mut self, rhs: Rational)
*= 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 Ord for Rational
impl Ord for Rational
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
1.21.0 (const: unstable) · Source§fn min(self, other: Self) -> Selfwhere
Self: Sized,
fn min(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl PartialOrd for Rational
impl PartialOrd for Rational
impl StructuralPartialEq for Rational
Source§impl SubAssign for Rational
impl SubAssign for Rational
Source§fn sub_assign(&mut self, rhs: Rational)
fn sub_assign(&mut self, rhs: Rational)
-= 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 more