pub struct RBig(/* private fields */);Expand description
Implementations§
source§impl RBig
impl RBig
sourcepub fn to_f32_fast(&self) -> f32
pub fn to_f32_fast(&self) -> f32
Convert the rational number to a f32.
The rounding follows the default IEEE 754 behavior (rounds to nearest, ties to even).
The rounding will be correct at most of the time, but in rare cases the mantissa can be off by one bit. Use RBig::to_f32 for ensured correct rounding.
§Examples
assert_eq!(RBig::ONE.to_f32_fast(), 1f32);
let r = RBig::from_parts(22.into(), 7u8.into());
assert_eq!(r.to_f32_fast(), 22./7.)sourcepub fn to_f64_fast(&self) -> f64
pub fn to_f64_fast(&self) -> f64
Convert the rational number to a f64.
The rounding follows the default IEEE 754 behavior (rounds to nearest, ties to even).
The rounding will be correct at most of the time, but in rare cases the mantissa can be off by one bit. Use RBig::to_f64 for ensured correct rounding.
§Examples
assert_eq!(RBig::ONE.to_f64_fast(), 1f64);
let r = RBig::from_parts(22.into(), 7u8.into());
assert_eq!(r.to_f64_fast(), 22./7.)sourcepub fn to_f32(&self) -> Approximation<f32, Sign>
pub fn to_f32(&self) -> Approximation<f32, Sign>
Convert the rational number to a f32 with guaranteed correct rounding.
The rounding follows the default IEEE 754 behavior (rounds to nearest, ties to even).
Because of the guaranteed rounding, it might take a long time to convert when the numerator and denominator are large. In this case RBig::to_f32_fast can be used if the correct rounding is not required.
§Examples
assert_eq!(RBig::ONE.to_f32(), Exact(1f32));
let r = RBig::from_parts(22.into(), 7u8.into());
// f32 representation of 22/7 is smaller than the actual 22/7
assert_eq!(r.to_f32(), Inexact(22./7., Negative));sourcepub fn to_f64(&self) -> Approximation<f64, Sign>
pub fn to_f64(&self) -> Approximation<f64, Sign>
Convert the rational number to a f64 with guaranteed correct rounding.
The rounding follows the default IEEE 754 behavior (rounds to nearest, ties to even).
Because of the guaranteed rounding, it might take a long time to convert when the numerator and denominator are large. In this case RBig::to_f64_fast can be used if the correct rounding is not required.
§Examples
assert_eq!(RBig::ONE.to_f64(), Exact(1f64));
let r = RBig::from_parts(22.into(), 7u8.into());
// f64 representation of 22/7 is smaller than the actual 22/7
assert_eq!(r.to_f64(), Inexact(22./7., Negative));sourcepub fn to_int(&self) -> Approximation<IBig, RBig>
pub fn to_int(&self) -> Approximation<IBig, RBig>
Convert the rational number to an IBig.
The conversion rounds toward zero. It’s equivalent to RBig::trunc, but it returns the fractional part if the rational number is not an integer.
§Examples
let a = RBig::from_parts(22.into(), UBig::ONE);
assert_eq!(a.to_int(), Exact(IBig::from(22)));
let b = RBig::from_parts(22.into(), 7u8.into());
assert_eq!(b.to_int(), Inexact(
IBig::from(3), RBig::from_parts(1.into(), 7u8.into())
));source§impl RBig
impl RBig
sourcepub fn sqr(&self) -> RBig
pub fn sqr(&self) -> RBig
Compute the square of the number (self * self).
§Examples
let a = RBig::from_parts(2.into(), 3u8.into());
let a2 = RBig::from_parts(4.into(), 9u8.into());
assert_eq!(a.sqr(), a2);source§impl RBig
impl RBig
sourcepub fn from_str_radix(src: &str, radix: u32) -> Result<RBig, ParseError>
pub fn from_str_radix(src: &str, radix: u32) -> Result<RBig, ParseError>
sourcepub fn from_str_with_radix_prefix(src: &str) -> Result<(RBig, u32), ParseError>
pub fn from_str_with_radix_prefix(src: &str) -> Result<(RBig, u32), ParseError>
Convert a string with optional radix prefixes to RBig, return the parsed integer and radix. If no prefix is present, then the default radix 10 will be used for parsing.
src may contain an ‘+’ or - prefix before the radix prefix of both the
numerator and denominator.
Allowed prefixes: 0b for binary, 0o for octal, 0x for hexadecimal.
If the radix prefixes for the numerator and the denominator are not the same, then a ParseError will be returned. The radix prefix for the denominator can be omitted, and the radix for the numerator will used for parsing.
§Examples
assert_eq!(RBig::from_str_with_radix_prefix("+0o17/25")?,
(RBig::from_parts(0o17.into(), 0o25u8.into()), 8));
assert_eq!(RBig::from_str_with_radix_prefix("-0x1f/-0x1e")?,
(RBig::from_parts(0x1f.into(), 0x1eu8.into()), 16));source§impl RBig
impl RBig
sourcepub fn from_parts(numerator: IBig, denominator: UBig) -> RBig
pub fn from_parts(numerator: IBig, denominator: UBig) -> RBig
Create a rational number from a signed numerator and an unsigned denominator
§Examples
assert_eq!(RBig::from_parts(IBig::ZERO, UBig::ONE), RBig::ZERO);
assert_eq!(RBig::from_parts(IBig::ONE, UBig::ONE), RBig::ONE);
assert_eq!(RBig::from_parts(IBig::NEG_ONE, UBig::ONE), RBig::NEG_ONE);sourcepub fn into_parts(self) -> (IBig, UBig)
pub fn into_parts(self) -> (IBig, UBig)
Convert the rational number into (numerator, denumerator) parts.
§Examples
assert_eq!(RBig::ZERO.into_parts(), (IBig::ZERO, UBig::ONE));
assert_eq!(RBig::ONE.into_parts(), (IBig::ONE, UBig::ONE));
assert_eq!(RBig::NEG_ONE.into_parts(), (IBig::NEG_ONE, UBig::ONE));sourcepub fn from_parts_signed(numerator: IBig, denominator: IBig) -> RBig
pub fn from_parts_signed(numerator: IBig, denominator: IBig) -> RBig
Create a rational number from a signed numerator and a signed denominator
§Examples
assert_eq!(RBig::from_parts_signed(1.into(), 1.into()), RBig::ONE);
assert_eq!(RBig::from_parts_signed(12.into(), (-12).into()), RBig::NEG_ONE);sourcepub const fn from_parts_const(
sign: Sign,
numerator: u128,
denominator: u128,
) -> RBig
pub const fn from_parts_const( sign: Sign, numerator: u128, denominator: u128, ) -> RBig
Create a rational number in a const context
The magnitude of the numerator and the denominator is limited to a DoubleWord.
§Examples
const ONE: RBig = RBig::from_parts_const(Sign::Positive, 1, 1);
assert_eq!(ONE, RBig::ONE);
const NEG_ONE: RBig = RBig::from_parts_const(Sign::Negative, 1, 1);
assert_eq!(NEG_ONE, RBig::NEG_ONE);sourcepub fn numerator(&self) -> &IBig
pub fn numerator(&self) -> &IBig
Get the numerator of the rational number
§Examples
assert_eq!(RBig::ZERO.numerator(), &IBig::ZERO);
assert_eq!(RBig::ONE.numerator(), &IBig::ONE);sourcepub fn denominator(&self) -> &UBig
pub fn denominator(&self) -> &UBig
Get the denominator of the rational number
§Examples
assert_eq!(RBig::ZERO.denominator(), &UBig::ONE);
assert_eq!(RBig::ONE.denominator(), &UBig::ONE);sourcepub const fn as_relaxed(&self) -> &Relaxed
pub const fn as_relaxed(&self) -> &Relaxed
sourcepub const fn is_zero(&self) -> bool
pub const fn is_zero(&self) -> bool
Check whether the number is 0
§Examples
assert!(RBig::ZERO.is_zero());
assert!(!RBig::ONE.is_zero());source§impl RBig
impl RBig
sourcepub fn split_at_point(self) -> (IBig, RBig)
pub fn split_at_point(self) -> (IBig, RBig)
Split the rational number into integral and fractional parts (split at the radix point).
It’s return is equivalent to (self.trunc(), self.fract())
§Examples
assert_eq!(RBig::ONE.split_at_point(), (IBig::ONE, RBig::ZERO));
let a = RBig::from_parts(22.into(), 7u8.into());
let (trunc, fract) = a.split_at_point();
assert_eq!(trunc, IBig::from(3));
assert_eq!(fract, RBig::from_parts(1.into(), 7u8.into()));sourcepub fn ceil(&self) -> IBig
pub fn ceil(&self) -> IBig
Compute the least integer that is greater than or equal to this number.
§Examples
assert_eq!(RBig::ONE.ceil(), IBig::ONE);
let a = RBig::from_parts(22.into(), 7u8.into());
assert_eq!(a.ceil(), IBig::from(4));sourcepub fn floor(&self) -> IBig
pub fn floor(&self) -> IBig
Compute the greatest integer that is less than or equal to this number.
§Examples
assert_eq!(RBig::ONE.floor(), IBig::ONE);
let a = RBig::from_parts(22.into(), 7u8.into());
assert_eq!(a.floor(), IBig::from(3));sourcepub fn round(&self) -> IBig
pub fn round(&self) -> IBig
Compute the integer that closest to this number.
It will return the one farther from zero when the number has the 1/2 as its fractional part.
§Examples
assert_eq!(RBig::ONE.round(), IBig::ONE);
let a = RBig::from_parts(22.into(), 7u8.into());
assert_eq!(a.round(), IBig::from(3));sourcepub fn trunc(&self) -> IBig
pub fn trunc(&self) -> IBig
Returns the integral part of the rational number.
It’s guaranteed that self == self.trunc() + self.fract().
§Examples
assert_eq!(RBig::ONE.trunc(), IBig::ONE);
let a = RBig::from_parts(22.into(), 7u8.into());
assert_eq!(a.trunc(), IBig::from(3));sourcepub fn fract(&self) -> RBig
pub fn fract(&self) -> RBig
Returns the fractional part of the rational number
It’s guaranteed that self == self.trunc() + self.fract().
§Examples
assert_eq!(RBig::ONE.fract(), RBig::ZERO);
let a = RBig::from_parts(22.into(), 7u8.into());
assert_eq!(a.fract(), RBig::from_parts(1.into(), 7u8.into()));source§impl RBig
impl RBig
sourcepub const fn sign(&self) -> Sign
pub const fn sign(&self) -> Sign
Get the sign of the number. Zero value has a positive sign.
§Examples
assert_eq!(RBig::ZERO.sign(), Sign::Positive);
assert_eq!(RBig::ONE.sign(), Sign::Positive);
assert_eq!(RBig::NEG_ONE.sign(), Sign::Negative);sourcepub const fn signum(&self) -> RBig
pub const fn signum(&self) -> RBig
A number representing the sign of self.
- RBig::ONE if the number is positive (including
inf) - RBig::ZERO if the number is zero
- RBig::NEG_ONE if the number is negative (including
-inf)
§Examples
let r = RBig::from_parts((-10).into(), 5u8.into());
assert_eq!(r.signum(), RBig::NEG_ONE);source§impl RBig
impl RBig
sourcepub fn is_simpler_than(&self, other: &RBig) -> bool
pub fn is_simpler_than(&self, other: &RBig) -> bool
Determine if this rational number is simpler than the other number.
This method only make sense for canonicalized ratios.
sourcepub fn simplest_from_f32(f: f32) -> Option<RBig>
pub fn simplest_from_f32(f: f32) -> Option<RBig>
Find the simplest rational number in the rounding interval of the f32 number.
This method returns None when the floating point value is not representable by a rational number, such as infinities or nans.
See RBig::simplest_in for the definition of simplicity.
The rounding interval of a f32 value is an interval such that all numbers in this
range will rounded to this f32 value. For example the rounding interval for 1f32
is [1. - f32::EPSILON / 2, 1. + f32::EPSILON / 2]. That is, the error of result value will
be less than 1/2 ULP.
This method can be used to recover the original fraction represented as a division of f32. See the examples below.
§Examples
assert_eq!(
RBig::simplest_from_f32(1e-1).unwrap(),
RBig::from_parts(1.into(), 10u8.into())
);
assert_eq!(
RBig::simplest_from_f32(22./7.).unwrap(),
RBig::from_parts(22.into(), 7u8.into())
);sourcepub fn simplest_from_f64(f: f64) -> Option<RBig>
pub fn simplest_from_f64(f: f64) -> Option<RBig>
Find the simplest rational number in the rounding interval of the f64 number.
This method returns None when the floating point value is not representable by a rational number, such as infinities or nans.
See RBig::simplest_in for the definition of simplicity.
The rounding interval of a f64 value is an interval such that all numbers in this
range will rounded to this f64 value. For example the rounding interval for 1f64
is [1. - f64::EPSILON / 2, 1. + f64::EPSILON / 2]. That is, the error of result value will
be less than 1/2 ULP.
This method can be used to recover the original fraction represented as a division of f64. See the examples below.
§Examples
assert_eq!(
RBig::simplest_from_f64(1e-1).unwrap(),
RBig::from_parts(1.into(), 10u8.into())
);
assert_eq!(
RBig::simplest_from_f64(22./7.).unwrap(),
RBig::from_parts(22.into(), 7u8.into())
);sourcepub fn simplest_in(lower: RBig, upper: RBig) -> RBig
pub fn simplest_in(lower: RBig, upper: RBig) -> RBig
Find the simplest rational number in the open interval (lower, upper).
A rational n₁/d₁ is simpler than another rational number n₂/d₂ if:
d₁ < d₂(compare denominator)- or
|n₁| < |n₂|(then compare the magnitude of numerator) - or
n₂ < 0 < n₁(then compare the sign)
lower and upper will be swapped if necessary. If lower and upper are
the same number, then this number will be directly returned.
§Examples
let a = RBig::from_parts(1234.into(), 5678u16.into());
let b = RBig::from_parts(1235.into(), 5679u16.into());
let s = RBig::simplest_in(a, b);
// 1234/5678 < 5/23 < 1235/5679
assert_eq!(s, RBig::from_parts(5.into(), 23u8.into()));sourcepub fn nearest(&self, limit: &UBig) -> Approximation<RBig, Sign>
pub fn nearest(&self, limit: &UBig) -> Approximation<RBig, Sign>
Find the closest rational number to this number with a limit of the denominator.
If the denominator of this number is larger than the limit, then it returns the closest one
between self.next_up() and self.next_down() to self. If the denominator of this number
is already less than or equal to the limit, then Exact(self) will be returned.
The error |self - self.nearest()| will be less than 1/(2*limit), and the sign of
the error self - self.nearest() will be returned if the result is not Exact.
§Examples
let a: RBig = 3.141592653.try_into().unwrap();
assert_eq!(a.nearest(&10u8.into()), Inexact(
RBig::from_parts(22.into(), 7u8.into()),
Sign::Positive // 22/7 > 3.141592653
));sourcepub fn next_up(&self, limit: &UBig) -> RBig
pub fn next_up(&self, limit: &UBig) -> RBig
Find the closest rational number that is greater than this number and has a denominator less than limit.
It’s equivalent to finding the next element in Farey sequence of order limit. The error
|self - self.next_up()| will be less than 1/limit.
§Examples
let a: RBig = 3.141592653.try_into().unwrap();
assert_eq!(a.next_up(&10u8.into()), RBig::from_parts(22.into(), 7u8.into()));sourcepub fn next_down(&self, limit: &UBig) -> RBig
pub fn next_down(&self, limit: &UBig) -> RBig
Find the closest rational number that is less than this number and has a denominator less than limit.
It’s equivalent to finding the previous element in Farey sequence of order limit. The error
|self - self.next_down()| will be less than 1/limit.
§Examples
let a: RBig = 3.141592653.try_into().unwrap();
assert_eq!(a.next_down(&10u8.into()), RBig::from_parts(25.into(), 8u8.into()));source§impl RBig
impl RBig
sourcepub fn to_float<R, const B: u64>(
&self,
precision: usize,
) -> Approximation<FBig<R, B>, Rounding>where
R: Round,
pub fn to_float<R, const B: u64>(
&self,
precision: usize,
) -> Approximation<FBig<R, B>, Rounding>where
R: Round,
Convert the rational number to a FBig with guaranteed correct rounding.
§Examples
use dashu_float::{DBig, round::Rounding::*};
assert_eq!(RBig::ONE.to_float(1), Exact(DBig::ONE));
assert_eq!(RBig::from(1000).to_float(4), Exact(DBig::from(1000)));
assert_eq!(RBig::from_parts(1000.into(), 6u8.into()).to_float(4),
Inexact(DBig::from_parts(1667.into(), -1), AddOne));sourcepub fn simplest_from_float<R, const B: u64>(f: &FBig<R, B>) -> Option<RBig>where
R: ErrorBounds,
pub fn simplest_from_float<R, const B: u64>(f: &FBig<R, B>) -> Option<RBig>where
R: ErrorBounds,
§Examples
use dashu_float::DBig;
let f = DBig::from_str_native("4.00")? / DBig::from_str_native("3.00")?;
let r = RBig::from_str_radix("4/3", 10)?;
assert_eq!(RBig::simplest_from_float(&f), Some(r));
assert_eq!(RBig::simplest_from_float(&DBig::INFINITY), None);
Trait Implementations§
source§impl AddAssign<&RBig> for RBig
impl AddAssign<&RBig> for RBig
source§fn add_assign(&mut self, rhs: &RBig)
fn add_assign(&mut self, rhs: &RBig)
+= operation. Read moresource§impl AddAssign for RBig
impl AddAssign for RBig
source§fn add_assign(&mut self, rhs: RBig)
fn add_assign(&mut self, rhs: RBig)
+= operation. Read moresource§impl DivAssign<&RBig> for RBig
impl DivAssign<&RBig> for RBig
source§fn div_assign(&mut self, rhs: &RBig)
fn div_assign(&mut self, rhs: &RBig)
/= operation. Read moresource§impl DivAssign for RBig
impl DivAssign for RBig
source§fn div_assign(&mut self, rhs: RBig)
fn div_assign(&mut self, rhs: RBig)
/= operation. Read moresource§impl<'l, 'r> DivRemEuclid<&'r RBig> for &'l RBig
impl<'l, 'r> DivRemEuclid<&'r RBig> for &'l RBig
source§impl<'r> DivRemEuclid<&'r RBig> for RBig
impl<'r> DivRemEuclid<&'r RBig> for RBig
source§impl<'l> DivRemEuclid<RBig> for &'l RBig
impl<'l> DivRemEuclid<RBig> for &'l RBig
source§impl DivRemEuclid for RBig
impl DivRemEuclid for RBig
source§impl EstimatedLog2 for RBig
impl EstimatedLog2 for RBig
source§impl MulAssign<&RBig> for RBig
impl MulAssign<&RBig> for RBig
source§fn mul_assign(&mut self, rhs: &RBig)
fn mul_assign(&mut self, rhs: &RBig)
*= operation. Read moresource§impl MulAssign for RBig
impl MulAssign for RBig
source§fn mul_assign(&mut self, rhs: RBig)
fn mul_assign(&mut self, rhs: RBig)
*= operation. Read moresource§impl<R, const B: u64> NumOrd<FBig<R, B>> for RBigwhere
R: Round,
impl<R, const B: u64> NumOrd<FBig<R, B>> for RBigwhere
R: Round,
source§impl NumOrd<IBig> for RBig
impl NumOrd<IBig> for RBig
source§impl<R, const B: u64> NumOrd<RBig> for FBig<R, B>where
R: Round,
impl<R, const B: u64> NumOrd<RBig> for FBig<R, B>where
R: Round,
source§impl NumOrd<RBig> for IBig
impl NumOrd<RBig> for IBig
source§impl NumOrd<RBig> for Relaxed
impl NumOrd<RBig> for Relaxed
source§impl NumOrd<RBig> for UBig
impl NumOrd<RBig> for UBig
source§impl NumOrd<Relaxed> for RBig
impl NumOrd<Relaxed> for RBig
source§impl NumOrd<UBig> for RBig
impl NumOrd<UBig> for RBig
source§impl NumOrd<f32> for RBig
impl NumOrd<f32> for RBig
source§impl NumOrd<f64> for RBig
impl NumOrd<f64> for RBig
source§impl NumOrd<i128> for RBig
impl NumOrd<i128> for RBig
source§impl NumOrd<i16> for RBig
impl NumOrd<i16> for RBig
source§impl NumOrd<i32> for RBig
impl NumOrd<i32> for RBig
source§impl NumOrd<i64> for RBig
impl NumOrd<i64> for RBig
source§impl NumOrd<i8> for RBig
impl NumOrd<i8> for RBig
source§impl NumOrd<isize> for RBig
impl NumOrd<isize> for RBig
source§impl NumOrd<u128> for RBig
impl NumOrd<u128> for RBig
source§impl NumOrd<u16> for RBig
impl NumOrd<u16> for RBig
source§impl NumOrd<u32> for RBig
impl NumOrd<u32> for RBig
source§impl NumOrd<u64> for RBig
impl NumOrd<u64> for RBig
source§impl NumOrd<u8> for RBig
impl NumOrd<u8> for RBig
source§impl NumOrd<usize> for RBig
impl NumOrd<usize> for RBig
source§impl Ord for RBig
impl Ord for RBig
source§impl PartialEq for RBig
impl PartialEq for RBig
source§impl PartialOrd for RBig
impl PartialOrd for RBig
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<&RBig> for RBig
impl RemAssign<&RBig> for RBig
source§fn rem_assign(&mut self, rhs: &RBig)
fn rem_assign(&mut self, rhs: &RBig)
%= operation. Read moresource§impl RemAssign for RBig
impl RemAssign for RBig
source§fn rem_assign(&mut self, rhs: RBig)
fn rem_assign(&mut self, rhs: RBig)
%= operation. Read moresource§impl SubAssign<&RBig> for RBig
impl SubAssign<&RBig> for RBig
source§fn sub_assign(&mut self, rhs: &RBig)
fn sub_assign(&mut self, rhs: &RBig)
-= operation. Read moresource§impl SubAssign for RBig
impl SubAssign for RBig
source§fn sub_assign(&mut self, rhs: RBig)
fn sub_assign(&mut self, rhs: RBig)
-= operation. Read more