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§

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

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

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.

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.

The base of the representation. It’s exposed as an IBig constant.

Create a Repr instance representing value zero

Create a Repr instance representing value one

Create a Repr instance representing value negative one

Create a Repr instance representing the (positive) infinity

Create a Repr instance representing the negative infinity

Determine if the Repr represents zero

Examples
assert!(Repr::<2>::zero().is_zero());
assert!(!Repr::<10>::one().is_zero());

Determine if the Repr represents one

Examples
assert!(Repr::<2>::zero().is_zero());
assert!(!Repr::<10>::one().is_zero());

Determine if the Repr represents the (±)infinity

Examples
assert!(Repr::<2>::infinity().is_infinite());
assert!(Repr::<10>::neg_infinity().is_infinite());
assert!(!Repr::<10>::one().is_infinite());

Determine if the Repr represents a finite number

Examples
assert!(Repr::<2>::zero().is_finite());
assert!(Repr::<10>::one().is_finite());
assert!(!Repr::<16>::infinity().is_finite());

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

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

Fast over-estimation of digits

Examples
assert_eq!(Repr::<2>::zero().digits_ub(), 0);
assert_eq!(Repr::<2>::one().digits_ub(), 1);
assert_eq!(Repr::<10>::one().digits_ub(), 1);
assert_eq!(Repr::<2>::new(31.into(), 0).digits_ub(), 5);
assert_eq!(Repr::<10>::new(99.into(), 0).digits_ub(), 2);

Fast under-estimation of digits

Examples
assert_eq!(Repr::<2>::zero().digits_lb(), 0);
assert_eq!(Repr::<2>::one().digits_lb(), 0);
assert_eq!(Repr::<10>::one().digits_lb(), 0);
assert!(Repr::<10>::new(1001.into(), 0).digits_lb() <= 3);

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

Get the significand of the representation

Get the exponent of the representation

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§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Deserialize this value from the given Serde deserializer. Read more
Formats the value using the given formatter. Read more
Estimate the bounds of the binary logarithm. Read more
Estimate the value of the binary logarithm. It’s calculated as the average of log2_bounds by default. Read more
Determines if a value of this type can be created from the specified Postgres Type. Read more
Creates a new value of this type from a buffer of data of the specified Postgres Type in its binary format. Read more
Creates a new value of this type from a NULL SQL value. Read more
A convenience function that delegates to from_sql and from_sql_null depending on the value of raw. Read more
See the trait documentation.
See the trait documentation.
A specialized variant of from_sql for handling null values. Read more
The resulting type after applying the - operator.
Performs the unary - operation. Read more
This method returns an Ordering between self and other. Read more
Compares and returns the maximum of two values. Read more
Compares and returns the minimum of two values. Read more
Restrict a value to a certain interval. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
This method returns an ordering between self and other values if one exists. Read more
This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Serialize this value into the given Serde serializer. Read more
See the trait documentation.
See the trait documentation.
Determines if a value of this type can be converted to the specified Postgres Type. Read more
Converts the value of self into the binary format of the specified Postgres Type, appending it to out. Read more
An adaptor method used internally by Rust-Postgres. Read more
Specify the encode format
Zero out this object from memory using Rust intrinsics which ensure the zeroization operation is not “optimized away” by the compiler. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
Returns a reference to self as a ToSql trait object.

Returns the argument unchanged.

See the trait documentation

Calls U::from(self).

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

Convert self to an expression for Diesel’s query builder. Read more
Convert &self to an expression for Diesel’s query builder. Read more
Convert self to an expression for Diesel’s query builder. Read more
Convert &self to an expression for Diesel’s query builder. Read more
Should always be Self
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
Converts the given value to a String. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.