pub struct Ratio { /* private fields */ }
Implementations§
source§impl Ratio
impl Ratio
source§impl Ratio
impl Ratio
sourcepub fn from_ieee754_f32(n: f32) -> Result<Self, ConversionError>
pub fn from_ieee754_f32(n: f32) -> Result<Self, ConversionError>
If you don’t know what ieee754
is, you’re okay to use this function.
Though the ieee 754 standard distinguishes negative 0 and positive 0, it doesn’t distinguish between them.
It returns an error if n
is NaN or Inf.
sourcepub fn to_ieee754_f32(&self) -> Result<f32, ConversionError>
pub fn to_ieee754_f32(&self) -> Result<f32, ConversionError>
If you don’t know what ieee754
is, you’re okay to use this function.
This function does not return f32::NAN
or f32::INFINITY
, but it returns ConversionError::NotInRange
instead.
sourcepub fn from_ieee754_f64(n: f64) -> Result<Self, ConversionError>
pub fn from_ieee754_f64(n: f64) -> Result<Self, ConversionError>
If you don’t know what ieee754
is, you’re okay to use this function.
Though the ieee 754 standard distinguishes negative 0 and positive 0, it doesn’t distinguish between them.
It returns an error if n
is NaN or Inf.
sourcepub fn to_ieee754_f64(&self) -> Result<f64, ConversionError>
pub fn to_ieee754_f64(&self) -> Result<f64, ConversionError>
If you don’t know what ieee754
is, you’re okay to use this function.
This function does not return f64::NAN
or f64::INFINITY
, but it returns ConversionError::NotInRange
instead.
source§impl Ratio
impl Ratio
pub fn from_denom_and_numer(denom: BigInt, numer: BigInt) -> Self
pub fn from_denom_and_numer_i32(denom: i32, numer: i32) -> Self
sourcepub fn from_denom_and_numer_raw(denom: BigInt, numer: BigInt) -> Self
pub fn from_denom_and_numer_raw(denom: BigInt, numer: BigInt) -> Self
This function does not do any safety checks. Use this function only when you’re sure that the properties below are satisfied.
denom
andnumer
are coprimedenom
is positivedenom
is 1 whennumer
is 0
pub fn from_bi(n: BigInt) -> Self
pub fn from_ubi(n: UBigInt) -> Self
pub fn from_i32(n: i32) -> Self
pub fn from_i64(n: i64) -> Self
pub fn from_i128(n: i128) -> Self
pub fn from_string(s: &str) -> Result<Self, ConversionError>
sourcepub fn to_ratio_string(&self) -> String
pub fn to_ratio_string(&self) -> String
Ratio { 4, 7 } -> “4/7”.
sourcepub fn to_approx_string(&self, max_len: usize) -> String
pub fn to_approx_string(&self, max_len: usize) -> String
Ratio { 4, 7 } -> “1.75”.
The length of the returned string is less or equal to digits
.
If digits
is less than 6, it’ll count that as 6.
sourcepub fn to_scientific_notation(&self, digits_max_len: usize) -> String
pub fn to_scientific_notation(&self, digits_max_len: usize) -> String
‘9.8e5’
source§impl Ratio
impl Ratio
pub fn neg(&self) -> Self
pub fn neg_mut(&mut self)
pub fn abs(&self) -> Self
pub fn abs_mut(&mut self)
pub fn truncate(&self) -> Self
pub fn truncate_mut(&mut self)
pub fn truncate_bi(&self) -> BigInt
sourcepub fn truncate_and_frac(&self) -> (BigInt, Self)
pub fn truncate_and_frac(&self) -> (BigInt, Self)
If you need both self.truncate_bi
and self.frac
, use this method. It’s way cheaper.
source§impl Ratio
impl Ratio
sourcepub fn from_raw(
denom: Vec<u32>,
denom_neg: bool,
numer: Vec<u32>,
numer_neg: bool
) -> Self
pub fn from_raw( denom: Vec<u32>, denom_neg: bool, numer: Vec<u32>, numer_neg: bool ) -> Self
This function DOES NOT check whether denom
and numer
are coprime.
Avoid using this function except when converting the result of Ratio::into_raw
to Ratio
.
In most cases, it’s safer to use Ratio::from_denom_and_numer
.
pub fn into_raw(self) -> (Vec<u32>, bool, Vec<u32>, bool)
pub fn zero() -> Self
pub fn one() -> Self
pub fn is_neg(&self) -> bool
pub fn is_one(&self) -> bool
pub fn is_zero(&self) -> bool
pub fn is_integer(&self) -> bool
sourcepub fn shrink(&mut self, limit: usize) -> Result<usize, usize>
pub fn shrink(&mut self, limit: usize) -> Result<usize, usize>
It shrinks the size of self.numer
and self.denom
until they’re less than or equal to 2^(limit * 32)
. It may lose accuracy.
If denom
and numer
are already small enough, it returns Ok(0)
.
If it successfully shrinks, it returns Ok(n)
where n
is how much numbers it removed.
Sometimes, the shrinked result doesn’t satisfy the limit. It returns Err(n)
in those cases where n
is how much numbers it removed.
Trait Implementations§
source§impl From<f32> for Ratio
impl From<f32> for Ratio
It returns 0 for NaN, Ratio(f32::MAX) for f32::Inf and Ratio(f32::MIN) for f32::NegInf.
source§impl From<f64> for Ratio
impl From<f64> for Ratio
It returns 0 for NaN, Ratio(f64::MAX) for f64::Inf and Ratio(f64::MIN) for f64::NegInf.
source§impl Ord for Ratio
impl Ord for Ratio
source§impl PartialEq<Ratio> for Ratio
impl PartialEq<Ratio> for Ratio
source§impl PartialOrd<Ratio> for Ratio
impl PartialOrd<Ratio> for Ratio
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 more