Struct dashu_float::Repr
source · [−]pub struct Repr<const BASE: Word> { /* private fields */ }
Expand description
Underlying representation of an arbitrary precision floating number.
The floating point number is represented as significand * base^exponent
, where the
type of the significand is IBig, and the type of exponent is isize. The representation
is always normalized (nonzero signficand is not divisible by the base, or zero signficand
with zero exponent).
When it’s used together with a Context, its precision will be limited so that
|signficand| < base^precision
. However, the precision limit is not always enforced.
In rare cases, the significand can have one more digit than the precision limit.
Infinity
This struct supports representing the infinity, but the infinity is only supposed to be used as sentinels. That is, only equality test and comparison are implemented for the infinity. Any other operations on the infinity will lead to panic. If an operation result is too large or too small, the operation will panic instead of returning an infinity.
Implementations
sourceimpl<const B: Word> Repr<B>
impl<const B: Word> Repr<B>
sourcepub const fn neg_infinity() -> Self
pub const fn neg_infinity() -> Self
Create a Repr instance representing the negative infinity
sourcepub const fn is_infinite(&self) -> bool
pub const fn is_infinite(&self) -> bool
sourcepub const fn sign(&self) -> Sign
pub const fn sign(&self) -> Sign
Get the sign of the number
Examples
assert_eq!(Repr::<2>::zero().sign(), Sign::Positive);
assert_eq!(Repr::<2>::neg_one().sign(), Sign::Negative);
assert_eq!(Repr::<10>::neg_infinity().sign(), Sign::Negative);
sourcepub fn digits(&self) -> usize
pub fn digits(&self) -> usize
Get the number of digits (under base B
) in the significand.
If the number is 0, then 0 is returned (instead of 1).
Examples
assert_eq!(Repr::<2>::zero().digits(), 0);
assert_eq!(Repr::<2>::one().digits(), 1);
assert_eq!(Repr::<10>::one().digits(), 1);
assert_eq!(Repr::<10>::new(100.into(), 0).digits(), 1); // 1e2
assert_eq!(Repr::<10>::new(101.into(), 0).digits(), 3);
sourcepub fn new(significand: IBig, exponent: isize) -> Self
pub fn new(significand: IBig, exponent: isize) -> Self
Create a Repr from the significand and exponent. This constructor will normalize the representation.
Examples
let a = Repr::<2>::new(400.into(), -2);
assert_eq!(a.significand(), &25);
assert_eq!(a.exponent(), 2);
let b = Repr::<10>::new(400.into(), -2);
assert_eq!(b.significand(), &4);
assert_eq!(b.exponent(), 0);
sourcepub fn significand(&self) -> &IBig
pub fn significand(&self) -> &IBig
Get the significand of the representation
sourcepub fn into_parts(self) -> (IBig, isize)
pub fn into_parts(self) -> (IBig, isize)
Convert the float number into raw (signficand, exponent)
parts
Examples
use dashu_int::IBig;
let a = Repr::<2>::new(400.into(), -2);
assert_eq!(a.into_parts(), (IBig::from(25), 2));
let b = Repr::<10>::new(400.into(), -2);
assert_eq!(b.into_parts(), (IBig::from(4), 0));
Trait Implementations
sourceimpl<const B: Word> EstimatedLog2 for Repr<B>
impl<const B: Word> EstimatedLog2 for Repr<B>
sourcefn log2_bounds(&self) -> (f32, f32)
fn log2_bounds(&self) -> (f32, f32)
Estimate the bounds of the binary logarithm. Read more
sourcefn log2_est(&self) -> f32
fn log2_est(&self) -> f32
Estimate the value of the binary logarithm. It’s calculated as the average of log2_bounds by default. Read more
sourceimpl<const B: Word> Ord for Repr<B>
impl<const B: Word> Ord for Repr<B>
1.21.0 · sourcefn max(self, other: Self) -> Self
fn max(self, other: Self) -> Self
Compares and returns the maximum of two values. Read more
1.21.0 · sourcefn min(self, other: Self) -> Self
fn min(self, other: Self) -> Self
Compares and returns the minimum of two values. Read more
1.50.0 · sourcefn clamp(self, min: Self, max: Self) -> Selfwhere
Self: PartialOrd<Self>,
fn clamp(self, min: Self, max: Self) -> Selfwhere
Self: PartialOrd<Self>,
Restrict a value to a certain interval. Read more
sourceimpl<const BASE: Word> PartialEq<Repr<BASE>> for Repr<BASE>
impl<const BASE: Word> PartialEq<Repr<BASE>> for Repr<BASE>
sourceimpl<const B: Word> PartialOrd<Repr<B>> for Repr<B>
impl<const B: Word> PartialOrd<Repr<B>> for Repr<B>
sourcefn partial_cmp(&self, other: &Self) -> Option<Ordering>
fn partial_cmp(&self, other: &Self) -> Option<Ordering>
This method returns an ordering between self
and other
values if one exists. Read more
1.0.0 · sourcefn lt(&self, other: &Rhs) -> bool
fn lt(&self, other: &Rhs) -> bool
This method tests less than (for self
and other
) and is used by the <
operator. Read more
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
impl<const BASE: Word> Eq for Repr<BASE>
impl<const BASE: Word> StructuralEq for Repr<BASE>
impl<const BASE: Word> StructuralPartialEq for Repr<BASE>
Auto Trait Implementations
impl<const BASE: u32> RefUnwindSafe for Repr<BASE>
impl<const BASE: u32> Send for Repr<BASE>
impl<const BASE: u32> Sync for Repr<BASE>
impl<const BASE: u32> Unpin for Repr<BASE>
impl<const BASE: u32> UnwindSafe for Repr<BASE>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more