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§
source§impl<const B: Word> Repr<B>
impl<const B: Word> Repr<B>
sourcepub fn to_f32(&self) -> Rounded<f32>
pub fn to_f32(&self) -> Rounded<f32>
Convert the float number representation to a f32 with the default IEEE 754 rounding mode.
The default IEEE 754 rounding mode is HalfEven (rounding to nearest, ties to even). To convert the float number with a specific rounding mode, please use FBig::to_f32.
§Examples
assert_eq!(Repr::<2>::one().to_f32(), Exact(1.0));
assert_eq!(Repr::<10>::infinity().to_f32(), Inexact(f32::INFINITY, NoOp));
sourcepub fn to_f64(&self) -> Rounded<f64>
pub fn to_f64(&self) -> Rounded<f64>
Convert the float number representation to a f64 with the default IEEE 754 rounding mode.
The default IEEE 754 rounding mode is HalfEven (rounding to nearest, ties to even). To convert the float number with a specific rounding mode, please use FBig::to_f64.
§Examples
assert_eq!(Repr::<2>::one().to_f64(), Exact(1.0));
assert_eq!(Repr::<10>::infinity().to_f64(), Inexact(f64::INFINITY, NoOp));
sourcepub fn to_int(&self) -> Rounded<IBig>
pub fn to_int(&self) -> Rounded<IBig>
Convert the float number representation to a IBig.
The fractional part is always rounded to zero. To convert with other rounding modes, please use FBig::to_int().
§Warning
If the float number has a very large exponent, it will be evaluated and result in allocating an huge integer and it might eat up all your memory.
To get a rough idea of how big the number is, it’s recommended to use EstimatedLog2.
§Examples
assert_eq!(Repr::<2>::neg_one().to_int(), Exact(IBig::NEG_ONE));
§Panics
Panics if the number is infinte.
source§impl<const B: Word> Repr<B>
impl<const B: Word> Repr<B>
sourcepub fn from_str_native(src: &str) -> Result<(Self, usize), ParseError>
👎Deprecated since 0.5.0: from_str_native will be removed in v0.5. Use core::str::FromStr instead.
pub fn from_str_native(src: &str) -> Result<(Self, usize), ParseError>
Convert a string in the native base (i.e. radix B
) to Repr.
Upon success, this method returns an Repr and the number of digits (in radix B
)
contained in the string.
This method is the underlying implementation of FBig::from_str_native, see the docs for that function for details.
source§impl<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 fn is_int(&self) -> bool
pub fn is_int(&self) -> bool
Determine if the number can be regarded as an integer.
Note that this function returns false when the number is infinite.
§Examples
assert!(Repr::<2>::zero().is_int());
assert!(Repr::<10>::one().is_int());
assert!(!Repr::<16>::new(123.into(), -1).is_int());
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(), &IBig::from(25));
assert_eq!(a.exponent(), 2);
let b = Repr::<10>::new(400.into(), -2);
assert_eq!(b.significand(), &IBig::from(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§
source§impl<'de, const B: Word> Deserialize<'de> for Repr<B>
impl<'de, const B: Word> Deserialize<'de> for Repr<B>
source§fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
source§impl<const B: Word> EstimatedLog2 for Repr<B>
impl<const B: Word> EstimatedLog2 for Repr<B>
source§impl<'a> FromSql<'a> for Repr<10>
impl<'a> FromSql<'a> for Repr<10>
source§fn accepts(ty: &Type) -> bool
fn accepts(ty: &Type) -> bool
Type
.source§fn from_sql(
ty: &Type,
raw: &'a [u8]
) -> Result<Self, Box<dyn Error + Sync + Send>>
fn from_sql( ty: &Type, raw: &'a [u8] ) -> Result<Self, Box<dyn Error + Sync + Send>>
Type
in its binary format. Read moresource§impl<const B: Word> NumOrd<IBig> for Repr<B>
impl<const B: Word> NumOrd<IBig> for Repr<B>
source§impl<const B: Word> NumOrd<Repr<B>> for IBig
impl<const B: Word> NumOrd<Repr<B>> for IBig
source§impl<const B: Word> NumOrd<Repr<B>> for UBig
impl<const B: Word> NumOrd<Repr<B>> for UBig
source§impl<const B: Word> NumOrd<Repr<B>> for f32
impl<const B: Word> NumOrd<Repr<B>> for f32
source§fn num_partial_cmp(&self, other: &Repr<B>) -> Option<Ordering>
fn num_partial_cmp(&self, other: &Repr<B>) -> Option<Ordering>
source§impl<const B: Word> NumOrd<Repr<B>> for f64
impl<const B: Word> NumOrd<Repr<B>> for f64
source§fn num_partial_cmp(&self, other: &Repr<B>) -> Option<Ordering>
fn num_partial_cmp(&self, other: &Repr<B>) -> Option<Ordering>
source§impl<const B: Word> NumOrd<Repr<B>> for i128
impl<const B: Word> NumOrd<Repr<B>> for i128
source§fn num_partial_cmp(&self, other: &Repr<B>) -> Option<Ordering>
fn num_partial_cmp(&self, other: &Repr<B>) -> Option<Ordering>
source§impl<const B: Word> NumOrd<Repr<B>> for i16
impl<const B: Word> NumOrd<Repr<B>> for i16
source§fn num_partial_cmp(&self, other: &Repr<B>) -> Option<Ordering>
fn num_partial_cmp(&self, other: &Repr<B>) -> Option<Ordering>
source§impl<const B: Word> NumOrd<Repr<B>> for i32
impl<const B: Word> NumOrd<Repr<B>> for i32
source§fn num_partial_cmp(&self, other: &Repr<B>) -> Option<Ordering>
fn num_partial_cmp(&self, other: &Repr<B>) -> Option<Ordering>
source§impl<const B: Word> NumOrd<Repr<B>> for i64
impl<const B: Word> NumOrd<Repr<B>> for i64
source§fn num_partial_cmp(&self, other: &Repr<B>) -> Option<Ordering>
fn num_partial_cmp(&self, other: &Repr<B>) -> Option<Ordering>
source§impl<const B: Word> NumOrd<Repr<B>> for i8
impl<const B: Word> NumOrd<Repr<B>> for i8
source§fn num_partial_cmp(&self, other: &Repr<B>) -> Option<Ordering>
fn num_partial_cmp(&self, other: &Repr<B>) -> Option<Ordering>
source§impl<const B: Word> NumOrd<Repr<B>> for isize
impl<const B: Word> NumOrd<Repr<B>> for isize
source§fn num_partial_cmp(&self, other: &Repr<B>) -> Option<Ordering>
fn num_partial_cmp(&self, other: &Repr<B>) -> Option<Ordering>
source§impl<const B: Word> NumOrd<Repr<B>> for u128
impl<const B: Word> NumOrd<Repr<B>> for u128
source§fn num_partial_cmp(&self, other: &Repr<B>) -> Option<Ordering>
fn num_partial_cmp(&self, other: &Repr<B>) -> Option<Ordering>
source§impl<const B: Word> NumOrd<Repr<B>> for u16
impl<const B: Word> NumOrd<Repr<B>> for u16
source§fn num_partial_cmp(&self, other: &Repr<B>) -> Option<Ordering>
fn num_partial_cmp(&self, other: &Repr<B>) -> Option<Ordering>
source§impl<const B: Word> NumOrd<Repr<B>> for u32
impl<const B: Word> NumOrd<Repr<B>> for u32
source§fn num_partial_cmp(&self, other: &Repr<B>) -> Option<Ordering>
fn num_partial_cmp(&self, other: &Repr<B>) -> Option<Ordering>
source§impl<const B: Word> NumOrd<Repr<B>> for u64
impl<const B: Word> NumOrd<Repr<B>> for u64
source§fn num_partial_cmp(&self, other: &Repr<B>) -> Option<Ordering>
fn num_partial_cmp(&self, other: &Repr<B>) -> Option<Ordering>
source§impl<const B: Word> NumOrd<Repr<B>> for u8
impl<const B: Word> NumOrd<Repr<B>> for u8
source§fn num_partial_cmp(&self, other: &Repr<B>) -> Option<Ordering>
fn num_partial_cmp(&self, other: &Repr<B>) -> Option<Ordering>
source§impl<const B: Word> NumOrd<Repr<B>> for usize
impl<const B: Word> NumOrd<Repr<B>> for usize
source§fn num_partial_cmp(&self, other: &Repr<B>) -> Option<Ordering>
fn num_partial_cmp(&self, other: &Repr<B>) -> Option<Ordering>
source§impl<const B1: Word, const B2: Word> NumOrd<Repr<B2>> for Repr<B1>
impl<const B1: Word, const B2: Word> NumOrd<Repr<B2>> for Repr<B1>
source§impl<const B: Word> NumOrd<UBig> for Repr<B>
impl<const B: Word> NumOrd<UBig> for Repr<B>
source§impl<const B: Word> NumOrd<f32> for Repr<B>
impl<const B: Word> NumOrd<f32> for Repr<B>
source§fn num_partial_cmp(&self, other: &f32) -> Option<Ordering>
fn num_partial_cmp(&self, other: &f32) -> Option<Ordering>
source§impl<const B: Word> NumOrd<f64> for Repr<B>
impl<const B: Word> NumOrd<f64> for Repr<B>
source§fn num_partial_cmp(&self, other: &f64) -> Option<Ordering>
fn num_partial_cmp(&self, other: &f64) -> Option<Ordering>
source§impl<const B: Word> NumOrd<i128> for Repr<B>
impl<const B: Word> NumOrd<i128> for Repr<B>
source§fn num_partial_cmp(&self, other: &i128) -> Option<Ordering>
fn num_partial_cmp(&self, other: &i128) -> Option<Ordering>
source§impl<const B: Word> NumOrd<i16> for Repr<B>
impl<const B: Word> NumOrd<i16> for Repr<B>
source§fn num_partial_cmp(&self, other: &i16) -> Option<Ordering>
fn num_partial_cmp(&self, other: &i16) -> Option<Ordering>
source§impl<const B: Word> NumOrd<i32> for Repr<B>
impl<const B: Word> NumOrd<i32> for Repr<B>
source§fn num_partial_cmp(&self, other: &i32) -> Option<Ordering>
fn num_partial_cmp(&self, other: &i32) -> Option<Ordering>
source§impl<const B: Word> NumOrd<i64> for Repr<B>
impl<const B: Word> NumOrd<i64> for Repr<B>
source§fn num_partial_cmp(&self, other: &i64) -> Option<Ordering>
fn num_partial_cmp(&self, other: &i64) -> Option<Ordering>
source§impl<const B: Word> NumOrd<i8> for Repr<B>
impl<const B: Word> NumOrd<i8> for Repr<B>
source§fn num_partial_cmp(&self, other: &i8) -> Option<Ordering>
fn num_partial_cmp(&self, other: &i8) -> Option<Ordering>
source§impl<const B: Word> NumOrd<isize> for Repr<B>
impl<const B: Word> NumOrd<isize> for Repr<B>
source§fn num_partial_cmp(&self, other: &isize) -> Option<Ordering>
fn num_partial_cmp(&self, other: &isize) -> Option<Ordering>
source§impl<const B: Word> NumOrd<u128> for Repr<B>
impl<const B: Word> NumOrd<u128> for Repr<B>
source§fn num_partial_cmp(&self, other: &u128) -> Option<Ordering>
fn num_partial_cmp(&self, other: &u128) -> Option<Ordering>
source§impl<const B: Word> NumOrd<u16> for Repr<B>
impl<const B: Word> NumOrd<u16> for Repr<B>
source§fn num_partial_cmp(&self, other: &u16) -> Option<Ordering>
fn num_partial_cmp(&self, other: &u16) -> Option<Ordering>
source§impl<const B: Word> NumOrd<u32> for Repr<B>
impl<const B: Word> NumOrd<u32> for Repr<B>
source§fn num_partial_cmp(&self, other: &u32) -> Option<Ordering>
fn num_partial_cmp(&self, other: &u32) -> Option<Ordering>
source§impl<const B: Word> NumOrd<u64> for Repr<B>
impl<const B: Word> NumOrd<u64> for Repr<B>
source§fn num_partial_cmp(&self, other: &u64) -> Option<Ordering>
fn num_partial_cmp(&self, other: &u64) -> Option<Ordering>
source§impl<const B: Word> NumOrd<u8> for Repr<B>
impl<const B: Word> NumOrd<u8> for Repr<B>
source§fn num_partial_cmp(&self, other: &u8) -> Option<Ordering>
fn num_partial_cmp(&self, other: &u8) -> Option<Ordering>
source§impl<const B: Word> NumOrd<usize> for Repr<B>
impl<const B: Word> NumOrd<usize> for Repr<B>
source§fn num_partial_cmp(&self, other: &usize) -> Option<Ordering>
fn num_partial_cmp(&self, other: &usize) -> Option<Ordering>
source§impl<const B: Word> Ord for Repr<B>
impl<const B: Word> Ord for Repr<B>
source§impl<const BASE: Word> PartialEq for Repr<BASE>
impl<const BASE: Word> PartialEq for Repr<BASE>
source§impl<const B: Word> PartialOrd for Repr<B>
impl<const B: Word> PartialOrd for Repr<B>
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 ToSql for Repr<10>
impl ToSql for Repr<10>
source§fn accepts(ty: &Type) -> bool
fn accepts(ty: &Type) -> bool
Type
.source§fn to_sql(
&self,
ty: &Type,
out: &mut BytesMut
) -> Result<IsNull, Box<dyn Error + Sync + Send>>
fn to_sql( &self, ty: &Type, out: &mut BytesMut ) -> Result<IsNull, Box<dyn Error + Sync + Send>>
self
into the binary format of the specified
Postgres Type
, appending it to out
. Read moresource§fn to_sql_checked(
&self,
ty: &Type,
out: &mut BytesMut
) -> Result<IsNull, Box<dyn Error + Sync + Send>>
fn to_sql_checked( &self, ty: &Type, out: &mut BytesMut ) -> Result<IsNull, Box<dyn Error + Sync + Send>>
source§fn encode_format(&self, _ty: &Type) -> Format
fn encode_format(&self, _ty: &Type) -> Format
impl<const BASE: Word> Eq for Repr<BASE>
impl<const BASE: Word> StructuralPartialEq for Repr<BASE>
Auto Trait Implementations§
impl<const BASE: u64> RefUnwindSafe for Repr<BASE>
impl<const BASE: u64> Send for Repr<BASE>
impl<const BASE: u64> Sync for Repr<BASE>
impl<const BASE: u64> Unpin for Repr<BASE>
impl<const BASE: u64> UnwindSafe for Repr<BASE>
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
source§impl<T> BorrowToSql for Twhere
T: ToSql,
impl<T> BorrowToSql for Twhere
T: ToSql,
source§fn borrow_to_sql(&self) -> &dyn ToSql
fn borrow_to_sql(&self) -> &dyn ToSql
self
as a ToSql
trait object.source§impl<T, ST, DB> FromStaticSqlRow<ST, DB> for T
impl<T, ST, DB> FromStaticSqlRow<ST, DB> for T
source§impl<T> IntoSql for T
impl<T> IntoSql for T
source§fn into_sql<T>(self) -> Self::Expressionwhere
Self: AsExpression<T> + Sized,
fn into_sql<T>(self) -> Self::Expressionwhere
Self: AsExpression<T> + Sized,
self
to an expression for Diesel’s query builder. Read moresource§fn as_sql<'a, T>(&'a self) -> <&'a Self as AsExpression<T>>::Expressionwhere
&'a Self: AsExpression<T>,
fn as_sql<'a, T>(&'a self) -> <&'a Self as AsExpression<T>>::Expressionwhere
&'a Self: AsExpression<T>,
&self
to an expression for Diesel’s query builder. Read moresource§impl<T> IntoSql for T
impl<T> IntoSql for T
source§fn into_sql<T>(self) -> Self::Expression
fn into_sql<T>(self) -> Self::Expression
self
to an expression for Diesel’s query builder. Read moresource§fn as_sql<'a, T>(&'a self) -> <&'a Self as AsExpression<T>>::Expression
fn as_sql<'a, T>(&'a self) -> <&'a Self as AsExpression<T>>::Expression
&self
to an expression for Diesel’s query builder. Read more