Skip to main content

Exact

Struct Exact 

Source
pub struct Exact { /* private fields */ }
Expand description

A Rat wrapper whose add/sub/mul/div succeed only when the result needs no rounding.

ⓘ
use the_q::{Exact, Rat};
let _ = Exact { value: Rat::one() };
ⓘ
use the_q::{Exact, Rat};
let mut e = Exact::new(Rat::one());
e.value = Rat::zero();

Implementations§

Source§

impl Exact

Source

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());
Source

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());
Source

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());
Source

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));
Source

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);
Source

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());
Source

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());
Source

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

Source

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));
Source

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());
Source

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());
Source

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§

Source§

impl Clone for Exact

Source§

fn clone(&self) -> Exact

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Exact

Source§

impl Debug for Exact

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for Exact

Source§

impl Hash for Exact

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Exact

Source§

fn eq(&self, other: &Exact) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Exact

Source§

impl TryFrom<BigDecimal> for Exact

Available on crate feature bigdecimal only.
Source§

fn try_from(v: BigDecimal) -> Result<Self, Self::Error>

Source§

type Error = ExactError

The type returned in the event of a conversion error.
Source§

impl TryFrom<BigInt> for Exact

Available on crate feature num-bigint only.
Source§

fn try_from(v: BigInt) -> Result<Self, Self::Error>

Source§

type Error = ExactError

The type returned in the event of a conversion error.
Source§

impl TryFrom<Decimal> for Exact

Available on crate feature rust_decimal only.
Source§

fn try_from(v: Decimal) -> Result<Self, Self::Error>

Source§

type Error = ExactError

The type returned in the event of a conversion error.
Source§

impl TryFrom<Ratio<BigInt>> for Exact

Available on crate feature num-rational only.
Source§

fn try_from(v: BigRational) -> Result<Self, Self::Error>

Source§

type Error = ExactError

The type returned in the event of a conversion error.
Source§

impl TryFrom<Ratio<i64>> for Exact

Available on crate feature num-rational only.
Source§

fn try_from(v: Ratio<i64>) -> Result<Self, Self::Error>

Source§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Az for T

Source§

fn az<Dst>(self) -> Dst
where T: Cast<Dst>,

Casts the value.
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<Src, Dst> CastFrom<Src> for Dst
where Src: Cast<Dst>,

Source§

fn cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CheckedAs for T

Source§

fn checked_as<Dst>(self) -> Option<Dst>
where T: CheckedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> CheckedCastFrom<Src> for Dst
where Src: CheckedCast<Dst>,

Source§

fn checked_cast_from(src: Src) -> Option<Dst>

Casts the value.
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<Src, Dst> LosslessTryInto<Dst> for Src
where Dst: LosslessTryFrom<Src>,

Source§

fn lossless_try_into(self) -> Option<Dst>

Performs the conversion.
Source§

impl<Src, Dst> LossyInto<Dst> for Src
where Dst: LossyFrom<Src>,

Source§

fn lossy_into(self) -> Dst

Performs the conversion.
Source§

impl<T> OverflowingAs for T

Source§

fn overflowing_as<Dst>(self) -> (Dst, bool)
where T: OverflowingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> OverflowingCastFrom<Src> for Dst
where Src: OverflowingCast<Dst>,

Source§

fn overflowing_cast_from(src: Src) -> (Dst, bool)

Casts the value.
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> SaturatingAs for T

Source§

fn saturating_as<Dst>(self) -> Dst
where T: SaturatingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> SaturatingCastFrom<Src> for Dst
where Src: SaturatingCast<Dst>,

Source§

fn saturating_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> StrictAs for T

Source§

fn strict_as<Dst>(self) -> Dst
where T: StrictCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> StrictCastFrom<Src> for Dst
where Src: StrictCast<Dst>,

Source§

fn strict_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> UnwrappedAs for T

Source§

fn unwrapped_as<Dst>(self) -> Dst
where T: UnwrappedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> UnwrappedCastFrom<Src> for Dst
where Src: UnwrappedCast<Dst>,

Source§

fn unwrapped_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> WrappingAs for T

Source§

fn wrapping_as<Dst>(self) -> Dst
where T: WrappingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> WrappingCastFrom<Src> for Dst
where Src: WrappingCast<Dst>,

Source§

fn wrapping_cast_from(src: Src) -> Dst

Casts the value.