pub struct Exact { /* private fields */ }Expand description
Implementations§
Source§impl Exact
impl Exact
Sourcepub fn new(value: Rat) -> Exact
pub fn new(value: Rat) -> Exact
Wraps an already-computed Rat. Always exact: it denotes itself.
use the_q::{Exact, Rat};
let half = Exact::new(Rat::new(1, 2).unwrap());
assert_eq!(half.value(), Rat::new(1, 2).unwrap());Sourcepub fn value(&self) -> Rat
pub fn value(&self) -> Rat
The wrapped value.
use the_q::{Exact, Rat};
let e = Exact::new(Rat::new(3, 4).unwrap());
assert_eq!(e.value(), Rat::new(3, 4).unwrap());Sourcepub fn is_nonneg(&self) -> bool
pub fn is_nonneg(&self) -> bool
Whether the wrapped value is nonnegative.
use the_q::{Exact, Rat};
assert!(Exact::new(Rat::new(1, 2).unwrap()).is_nonneg());
assert!(Exact::new(Rat::zero()).is_nonneg());
assert!(!Exact::new(Rat::new(-1, 2).unwrap()).is_nonneg());Sourcepub fn le(a: Exact, b: Exact) -> bool
pub fn le(a: Exact, b: Exact) -> bool
a <= b.
use the_q::{Exact, Rat};
let a = Exact::new(Rat::new(1, 3).unwrap());
let b = Exact::new(Rat::new(1, 2).unwrap());
assert!(Exact::le(a, b));
assert!(!Exact::le(b, a));
assert!(Exact::le(a, a));Sourcepub fn checked_add(a: Exact, b: Exact) -> Option<Exact>
pub fn checked_add(a: Exact, b: Exact) -> Option<Exact>
a + b, or None if the exact sum needs rounding.
use the_q::{Exact, Rat};
let half = Exact::new(Rat::new(1, 2).unwrap());
let sum = Exact::checked_add(half, half).expect("1/2 + 1/2 is exact");
assert_eq!(sum.value(), Rat::new(1, 1).unwrap());
// The reduced denominator of this sum is about 9.2e18, past the
// budget -- `None`, not a silently rounded answer.
let a = Exact::new(Rat::new(1, 3_037_000_493).unwrap());
let b = Exact::new(Rat::new(1, 3_037_000_499).unwrap());
assert_eq!(Exact::checked_add(a, b), None);Sourcepub fn checked_sub(a: Exact, b: Exact) -> Option<Exact>
pub fn checked_sub(a: Exact, b: Exact) -> Option<Exact>
a - b, or None if the exact difference needs rounding.
use the_q::{Exact, Rat};
let a = Exact::new(Rat::new(3, 4).unwrap());
let b = Exact::new(Rat::new(1, 4).unwrap());
let diff = Exact::checked_sub(a, b).expect("3/4 - 1/4 is exact");
assert_eq!(diff.value(), Rat::new(1, 2).unwrap());Sourcepub fn checked_mul(a: Exact, b: Exact) -> Option<Exact>
pub fn checked_mul(a: Exact, b: Exact) -> Option<Exact>
a * b, or None if the exact product needs rounding.
use the_q::{Exact, Rat};
let a = Exact::new(Rat::new(2, 3).unwrap());
let b = Exact::new(Rat::new(3, 4).unwrap());
let product = Exact::checked_mul(a, b).expect("(2/3) * (3/4) is exact");
assert_eq!(product.value(), Rat::new(1, 2).unwrap());Sourcepub fn checked_div(a: Exact, b: Exact) -> Option<Exact>
pub fn checked_div(a: Exact, b: Exact) -> Option<Exact>
a / b, or None if b is zero or the exact quotient needs rounding.
use the_q::{Exact, Rat};
let a = Exact::new(Rat::new(1, 2).unwrap());
let b = Exact::new(Rat::new(1, 3).unwrap());
let quotient = Exact::checked_div(a, b).expect("(1/2) / (1/3) is exact");
assert_eq!(quotient.value(), Rat::new(3, 2).unwrap());
// Division by zero is `None`, the same as any other operand that
// leaves the exact path -- `checked_div` does not distinguish the two
// causes (the `Result`-returning `div` does; see below).
assert_eq!(Exact::checked_div(a, Exact::new(Rat::zero())), None);Source§impl Exact
impl Exact
Sourcepub fn add(a: Exact, b: Exact) -> Result<Exact, ExactError>
pub fn add(a: Exact, b: Exact) -> Result<Exact, ExactError>
a + b, or ExactError::Inexact if the exact sum needs rounding.
use the_q::{Exact, ExactError, Rat};
let half = Exact::new(Rat::new(1, 2).unwrap());
assert_eq!(Exact::add(half, half).unwrap().value(), Rat::new(1, 1).unwrap());
let a = Exact::new(Rat::new(1, 3_037_000_493).unwrap());
let b = Exact::new(Rat::new(1, 3_037_000_499).unwrap());
assert_eq!(Exact::add(a, b), Err(ExactError::Inexact));Sourcepub fn sub(a: Exact, b: Exact) -> Result<Exact, ExactError>
pub fn sub(a: Exact, b: Exact) -> Result<Exact, ExactError>
a - b, or ExactError::Inexact if the exact difference needs
rounding.
use the_q::{Exact, Rat};
let a = Exact::new(Rat::new(3, 4).unwrap());
let b = Exact::new(Rat::new(1, 4).unwrap());
assert_eq!(Exact::sub(a, b).unwrap().value(), Rat::new(1, 2).unwrap());Sourcepub fn mul(a: Exact, b: Exact) -> Result<Exact, ExactError>
pub fn mul(a: Exact, b: Exact) -> Result<Exact, ExactError>
a * b, or ExactError::Inexact if the exact product needs
rounding.
use the_q::{Exact, Rat};
let a = Exact::new(Rat::new(2, 3).unwrap());
let b = Exact::new(Rat::new(3, 4).unwrap());
assert_eq!(Exact::mul(a, b).unwrap().value(), Rat::new(1, 2).unwrap());Sourcepub fn div(a: Exact, b: Exact) -> Result<Exact, ExactError>
pub fn div(a: Exact, b: Exact) -> Result<Exact, ExactError>
a / b, or ExactError::DivisionByZero / ExactError::Inexact.
use the_q::{Exact, ExactError, Rat};
let a = Exact::new(Rat::new(1, 2).unwrap());
let b = Exact::new(Rat::new(1, 3).unwrap());
assert_eq!(Exact::div(a, b).unwrap().value(), Rat::new(3, 2).unwrap());
assert_eq!(
Exact::div(a, Exact::new(Rat::zero())),
Err(ExactError::DivisionByZero),
);Trait Implementations§
impl Copy for Exact
impl Eq for Exact
impl StructuralPartialEq for Exact
Source§impl TryFrom<BigDecimal> for Exact
Available on crate feature bigdecimal only.
impl TryFrom<BigDecimal> for Exact
Available on crate feature
bigdecimal only.Source§type Error = ExactError
type Error = ExactError
The type returned in the event of a conversion error.
Auto Trait Implementations§
impl Freeze for Exact
impl RefUnwindSafe for Exact
impl Send for Exact
impl Sync for Exact
impl Unpin for Exact
impl UnsafeUnpin for Exact
impl UnwindSafe for Exact
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CheckedAs for T
impl<T> CheckedAs for T
Source§fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
Casts the value.
Source§impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
Source§fn checked_cast_from(src: Src) -> Option<Dst>
fn checked_cast_from(src: Src) -> Option<Dst>
Casts the value.
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Src, Dst> LosslessTryInto<Dst> for Srcwhere
Dst: LosslessTryFrom<Src>,
impl<Src, Dst> LosslessTryInto<Dst> for Srcwhere
Dst: LosslessTryFrom<Src>,
Source§fn lossless_try_into(self) -> Option<Dst>
fn lossless_try_into(self) -> Option<Dst>
Performs the conversion.
Source§impl<Src, Dst> LossyInto<Dst> for Srcwhere
Dst: LossyFrom<Src>,
impl<Src, Dst> LossyInto<Dst> for Srcwhere
Dst: LossyFrom<Src>,
Source§fn lossy_into(self) -> Dst
fn lossy_into(self) -> Dst
Performs the conversion.
Source§impl<T> OverflowingAs for T
impl<T> OverflowingAs for T
Source§fn overflowing_as<Dst>(self) -> (Dst, bool)where
T: OverflowingCast<Dst>,
fn overflowing_as<Dst>(self) -> (Dst, bool)where
T: OverflowingCast<Dst>,
Casts the value.
Source§impl<Src, Dst> OverflowingCastFrom<Src> for Dstwhere
Src: OverflowingCast<Dst>,
impl<Src, Dst> OverflowingCastFrom<Src> for Dstwhere
Src: OverflowingCast<Dst>,
Source§fn overflowing_cast_from(src: Src) -> (Dst, bool)
fn overflowing_cast_from(src: Src) -> (Dst, bool)
Casts the value.
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<T> SaturatingAs for T
impl<T> SaturatingAs for T
Source§fn saturating_as<Dst>(self) -> Dstwhere
T: SaturatingCast<Dst>,
fn saturating_as<Dst>(self) -> Dstwhere
T: SaturatingCast<Dst>,
Casts the value.
Source§impl<Src, Dst> SaturatingCastFrom<Src> for Dstwhere
Src: SaturatingCast<Dst>,
impl<Src, Dst> SaturatingCastFrom<Src> for Dstwhere
Src: SaturatingCast<Dst>,
Source§fn saturating_cast_from(src: Src) -> Dst
fn saturating_cast_from(src: Src) -> Dst
Casts the value.
Source§impl<T> StrictAs for T
impl<T> StrictAs for T
Source§fn strict_as<Dst>(self) -> Dstwhere
T: StrictCast<Dst>,
fn strict_as<Dst>(self) -> Dstwhere
T: StrictCast<Dst>,
Casts the value.
Source§impl<Src, Dst> StrictCastFrom<Src> for Dstwhere
Src: StrictCast<Dst>,
impl<Src, Dst> StrictCastFrom<Src> for Dstwhere
Src: StrictCast<Dst>,
Source§fn strict_cast_from(src: Src) -> Dst
fn strict_cast_from(src: Src) -> Dst
Casts the value.
Source§impl<T> UnwrappedAs for T
impl<T> UnwrappedAs for T
Source§fn unwrapped_as<Dst>(self) -> Dstwhere
T: UnwrappedCast<Dst>,
fn unwrapped_as<Dst>(self) -> Dstwhere
T: UnwrappedCast<Dst>,
Casts the value.
Source§impl<Src, Dst> UnwrappedCastFrom<Src> for Dstwhere
Src: UnwrappedCast<Dst>,
impl<Src, Dst> UnwrappedCastFrom<Src> for Dstwhere
Src: UnwrappedCast<Dst>,
Source§fn unwrapped_cast_from(src: Src) -> Dst
fn unwrapped_cast_from(src: Src) -> Dst
Casts the value.
Source§impl<T> WrappingAs for T
impl<T> WrappingAs for T
Source§fn wrapping_as<Dst>(self) -> Dstwhere
T: WrappingCast<Dst>,
fn wrapping_as<Dst>(self) -> Dstwhere
T: WrappingCast<Dst>,
Casts the value.
Source§impl<Src, Dst> WrappingCastFrom<Src> for Dstwhere
Src: WrappingCast<Dst>,
impl<Src, Dst> WrappingCastFrom<Src> for Dstwhere
Src: WrappingCast<Dst>,
Source§fn wrapping_cast_from(src: Src) -> Dst
fn wrapping_cast_from(src: Src) -> Dst
Casts the value.