Struct astro_float::BigFloatNumber
source · [−]pub struct BigFloatNumber { /* private fields */ }
Expand description
BigFloatNumber represents floating point number with mantissa of an arbitrary size, and exponent.
Implementations
sourceimpl BigFloatNumber
impl BigFloatNumber
sourcepub fn new(p: usize) -> Result<Self, Error>
pub fn new(p: usize) -> Result<Self, Error>
Returns a new number with value of 0 and precision of p
bits. Precision is rounded upwards to the word size.
Errors
- InvalidArgument: precision is incorrect.
- MemoryAllocation: failed to allocate memory for mantissa.
sourcepub fn max(p: usize) -> Result<Self, Error>
pub fn max(p: usize) -> Result<Self, Error>
Returns the maximum value for the specified precision p
: all bits of the mantissa are set to 1, the exponent has the maximum possible value, and the sign is positive. Precision is rounded upwards to the word size.
Errors
- InvalidArgument: precision is incorrect.
- MemoryAllocation: failed to allocate memory for mantissa.
sourcepub fn min(p: usize) -> Result<Self, Error>
pub fn min(p: usize) -> Result<Self, Error>
Returns the minimum value for the specified precision p
: all bits of the mantissa are set to 1, the exponent has the maximum possible value, and the sign is negative. Precision is rounded upwards to the word size.
Errors
- InvalidArgument: precision is incorrect.
- MemoryAllocation: failed to allocate memory for mantissa.
sourcepub fn min_positive(p: usize) -> Result<Self, Error>
pub fn min_positive(p: usize) -> Result<Self, Error>
Returns the minimum positive subnormal value for the specified precision p
: only the least significant bit of the mantissa is set to 1, the exponent has the minimum possible value, and the sign is positive. Precision is rounded upwards to the word size.
Errors
- InvalidArgument: precision is incorrect.
- MemoryAllocation: failed to allocate memory for mantissa.
sourcepub fn from_word(d: Word, p: usize) -> Result<Self, Error>
pub fn from_word(d: Word, p: usize) -> Result<Self, Error>
Returns a new number with value d
and the precision p
. Precision is rounded upwards to the word size.
Errors
- InvalidArgument: precision is incorrect.
- MemoryAllocation: failed to allocate memory for mantissa.
sourcepub fn neg(&self) -> Result<Self, Error>
pub fn neg(&self) -> Result<Self, Error>
Returns a copy of the number with the sign reversed.
Errors
- MemoryAllocation: failed to allocate memory for mantissa.
sourcepub fn add(&self, d2: &Self, rm: RoundingMode) -> Result<Self, Error>
pub fn add(&self, d2: &Self, rm: RoundingMode) -> Result<Self, Error>
Adds d2
to self
and returns the result of the operation rounded according to rm
. The resulting precision is equal to the highest precision of the two operands.
Errors
- ExponentOverflow: the resulting exponent becomes greater than the maximum allowed value for the exponent.
- MemoryAllocation: failed to allocate memory for mantissa.
sourcepub fn sub(&self, d2: &Self, rm: RoundingMode) -> Result<Self, Error>
pub fn sub(&self, d2: &Self, rm: RoundingMode) -> Result<Self, Error>
Subtracts d2
from self
and returns the result of the operation rounded according to rm
. The resulting precision is equal to the highest precision of the two operands.
Errors
- ExponentOverflow: the resulting exponent becomes greater than the maximum allowed value for the exponent.
- MemoryAllocation: failed to allocate memory for mantissa.
sourcepub fn add_full_prec(&self, d2: &Self) -> Result<Self, Error>
pub fn add_full_prec(&self, d2: &Self) -> Result<Self, Error>
Adds d2
to self
and returns the result of the operation. The resulting precision is equal to the full precision of the result. This operation can be used to emulate integer addition.
Errors
- ExponentOverflow: the resulting exponent becomes greater than the maximum allowed value for the exponent.
- MemoryAllocation: failed to allocate memory for mantissa.
sourcepub fn sub_full_prec(&self, d2: &Self) -> Result<Self, Error>
pub fn sub_full_prec(&self, d2: &Self) -> Result<Self, Error>
Subtracts d2
from self
and returns the result of the operation. The resulting precision is equal to the full precision of the result. This operation can be used to emulate integer subtraction.
Errors
- ExponentOverflow: the resulting exponent becomes greater than the maximum allowed value for the exponent.
- MemoryAllocation: failed to allocate memory for mantissa.
sourcepub fn mul(&self, d2: &Self, rm: RoundingMode) -> Result<Self, Error>
pub fn mul(&self, d2: &Self, rm: RoundingMode) -> Result<Self, Error>
Multiplies d2
by self
and returns the result of the operation rounded according to rm
. The resulting precision is equal to the highest precision of the two operands.
Errors
- ExponentOverflow: the resulting exponent becomes greater than the maximum allowed value for the exponent.
- MemoryAllocation: failed to allocate memory for mantissa.
sourcepub fn mul_full_prec(&self, d2: &Self) -> Result<Self, Error>
pub fn mul_full_prec(&self, d2: &Self) -> Result<Self, Error>
Multiplies d2
by self
and returns the result of the operation. The resulting precision is equal to the full precision of the result. This operation can be used to emulate integer multiplication.
Errors
- ExponentOverflow: the resulting exponent becomes greater than the maximum allowed value for the exponent.
- MemoryAllocation: failed to allocate memory for mantissa.
sourcepub fn div(&self, d2: &Self, rm: RoundingMode) -> Result<Self, Error>
pub fn div(&self, d2: &Self, rm: RoundingMode) -> Result<Self, Error>
Divides self
by d2
and returns the result of the operation rounded according to rm
. The resulting precision is equal to the highest precision of the two operands.
Errors
- DivisionByZero:
d2
is zero. - ExponentOverflow: the resulting exponent becomes greater than the maximum allowed value for the exponent.
- MemoryAllocation: failed to allocate memory for mantissa.
sourcepub fn cmp(&self, d2: &Self) -> i64
pub fn cmp(&self, d2: &Self) -> i64
Compares self
to d2
.
Returns positive if self
is greater than d2
, negative if self
is smaller than d2
, 0 otherwise.
sourcepub fn abs(&self) -> Result<Self, Error>
pub fn abs(&self) -> Result<Self, Error>
Returns the absolute value of a number.
Errors
- MemoryAllocation: failed to allocate memory for mantissa.
sourcepub fn from_f64(p: usize, f: f64) -> Result<Self, Error>
pub fn from_f64(p: usize, f: f64) -> Result<Self, Error>
Constructs a number with precision p
from f64 value.
Errors
- InvalidArgument: precision is incorrect or
f
is NaN. - MemoryAllocation: failed to allocate memory for mantissa.
- ExponentOverflow:
f
is Inf.
sourcepub fn from_f32(p: usize, f: f32) -> Result<Self, Error>
pub fn from_f32(p: usize, f: f32) -> Result<Self, Error>
Constructs a number with precision p
from f32 value.
Errors
- InvalidArgument: precision is incorrect or
f
is NaN. - MemoryAllocation: failed to allocate memory for mantissa.
- ExponentOverflow:
f
is Inf.
sourcepub fn is_subnormal(&self) -> bool
pub fn is_subnormal(&self) -> bool
Returns true if self
is subnormal. A number is subnormal if the most significant bit of the mantissa is not equal to 1.
sourcepub fn to_raw_parts(&self) -> (&[Word], usize, Sign, Exponent)
pub fn to_raw_parts(&self) -> (&[Word], usize, Sign, Exponent)
Decomposes self
into raw parts. The function returns a reference to a slice of words representing mantissa, numbers of significant bits in the mantissa, sign, and exponent.
sourcepub fn from_raw_parts(
m: &[Word],
n: usize,
s: Sign,
e: Exponent
) -> Result<Self, Error>
pub fn from_raw_parts(
m: &[Word],
n: usize,
s: Sign,
e: Exponent
) -> Result<Self, Error>
Constructs a number from the raw parts:
m
is the mantisaa.n
is the number of significant bits in mantissa.s
is the sign.e
is the exponent.
Errors
- MemoryAllocation: failed to allocate memory for mantissa.
sourcepub fn is_positive(&self) -> bool
pub fn is_positive(&self) -> bool
Returns true if self
is positive.
sourcepub fn is_negative(&self) -> bool
pub fn is_negative(&self) -> bool
Returns true if self
is negative.
sourcepub fn get_exponent(&self) -> Exponent
pub fn get_exponent(&self) -> Exponent
Returns the exponent of self
.
pub fn is_zero(&self) -> bool
sourcepub fn floor(&self) -> Result<Self, Error>
pub fn floor(&self) -> Result<Self, Error>
Returns the largest integer less than or equal to self
.
Errors
- MemoryAllocation: failed to allocate memory for mantissa.
sourcepub fn ceil(&self) -> Result<Self, Error>
pub fn ceil(&self) -> Result<Self, Error>
Returns the smallest integer greater than or equal to self
.
Errors
- MemoryAllocation: failed to allocate memory for mantissa.
sourcepub fn fract(&self) -> Result<Self, Error>
pub fn fract(&self) -> Result<Self, Error>
Returns fractional part of a number.
Errors
- MemoryAllocation: failed to allocate memory for mantissa.
sourcepub fn set_exponent(&mut self, e: Exponent)
pub fn set_exponent(&mut self, e: Exponent)
Sets the exponent of self
.
sourcepub fn get_mantissa_max_bit_len(&self) -> usize
pub fn get_mantissa_max_bit_len(&self) -> usize
Returns the maximum mantissa length of self
in bits.
sourcepub fn round(&mut self, n: usize, rm: RoundingMode) -> Result<Self, Error>
pub fn round(&mut self, n: usize, rm: RoundingMode) -> Result<Self, Error>
Returns the rounded number with n
binary positions in the fractional part of the number using rounding mode rm
.
Errors
- MemoryAllocation: failed to allocate memory for mantissa.
- ExponentOverflow: rounding causes exponent overflow.
sourcepub fn random_normal(
p: usize,
exp_from: Exponent,
exp_to: Exponent
) -> Result<Self, Error>
pub fn random_normal(
p: usize,
exp_from: Exponent,
exp_to: Exponent
) -> Result<Self, Error>
Generates a random normal number with precision p
and an exponent within the range from exp_from
to exp_to
.
Errors
- InvalidArgument: precision is incorrect.
- MemoryAllocation: failed to allocate memory for mantissa.
sourcepub fn set_precision(&mut self, p: usize, rm: RoundingMode) -> Result<(), Error>
pub fn set_precision(&mut self, p: usize, rm: RoundingMode) -> Result<(), Error>
Sets the precision of self
to p
.
If the new precision is smaller than the existing one, the number is rounded using specified rounding mode rm
.
Errors
- MemoryAllocation: failed to allocate memory for mantissa.
sourcepub fn reciprocal(&self, rm: RoundingMode) -> Result<Self, Error>
pub fn reciprocal(&self, rm: RoundingMode) -> Result<Self, Error>
Computes the reciprocal of a number. The result is rounded using the rounding mode rm
.
Errors
- MemoryAllocation: failed to allocate memory for mantissa.
- ExponentOverflow: rounding causes exponent overflow.
sourcepub fn get_mantissa_digits(&self) -> &[Word]
pub fn get_mantissa_digits(&self) -> &[Word]
Returns the raw mantissa words of a number.
sourceimpl BigFloatNumber
impl BigFloatNumber
sourcepub fn parse(
s: &str,
rdx: Radix,
p: usize,
rm: RoundingMode
) -> Result<Self, Error>
pub fn parse(
s: &str,
rdx: Radix,
p: usize,
rm: RoundingMode
) -> Result<Self, Error>
Parses the number from the string s
using radix rdx
, precision p
, and rounding mode rm
.
Note, since hexadecimal digits include the character “e”, the exponent part is separated
from the mantissa by “_”.
For example, a number with mantissa 123abcdef
and exponent 123
would be formatted as 123abcdef_e+123
.
Errors
- InvalidArgument: failed to parse input.
- MemoryAllocation: failed to allocate memory for mantissa.
- ExponentOverflow: the resulting exponent becomes greater than the maximum allowed value for the exponent.
sourcepub fn format(&self, rdx: Radix, rm: RoundingMode) -> Result<String, Error>
pub fn format(&self, rdx: Radix, rm: RoundingMode) -> Result<String, Error>
Formats the number using radix rdx
and rounding mode rm
.
Note, since hexadecimal digits include the character “e”, the exponent part is separated
from the mantissa by “_”.
For example, a number with mantissa 123abcdef
and exponent 123
would be formatted as 123abcdef_e+123
.
Errors
- MemoryAllocation: failed to allocate memory for mantissa.
- ExponentOverflow: the resulting exponent becomes greater than the maximum allowed value for the exponent.
sourceimpl BigFloatNumber
impl BigFloatNumber
sourceimpl BigFloatNumber
impl BigFloatNumber
sourceimpl BigFloatNumber
impl BigFloatNumber
sourceimpl BigFloatNumber
impl BigFloatNumber
sourceimpl BigFloatNumber
impl BigFloatNumber
sourceimpl BigFloatNumber
impl BigFloatNumber
sourceimpl BigFloatNumber
impl BigFloatNumber
sourceimpl BigFloatNumber
impl BigFloatNumber
sourceimpl BigFloatNumber
impl BigFloatNumber
sourceimpl BigFloatNumber
impl BigFloatNumber
sourcepub fn convert_from_radix(
sign: Sign,
digits: &[u8],
e: Exponent,
rdx: Radix,
p: usize,
rm: RoundingMode
) -> Result<Self, Error>
pub fn convert_from_radix(
sign: Sign,
digits: &[u8],
e: Exponent,
rdx: Radix,
p: usize,
rm: RoundingMode
) -> Result<Self, Error>
Converts an array of digits in radix rdx
to BigFloatNumber with precision p
.
digits
represents mantissa and is interpreted as a number smaller than 1 and greater or equal to 1/rdx
.
The first element in digits
is the most significant digit.
e
is the exponent part of the number, such that the number can be represented as digits
* rdx
^ e
.
Examples
use astro_float::{BigFloatNumber, Sign, RoundingMode, Radix};
let g = BigFloatNumber::convert_from_radix(
Sign::Neg,
&[1, 2, 3, 4, 5, 6, 7, 0],
3,
Radix::Oct,
64,
RoundingMode::None).unwrap();
let n = BigFloatNumber::from_f64(64, -83.591552734375).unwrap();
assert!(n.cmp(&g) == 0);
Errors
- MemoryAllocation: failed to allocate memory for mantissa.
- ExponentOverflow: the resulting exponent becomes greater than the maximum allowed value for the exponent.
sourcepub fn convert_to_radix(
&self,
rdx: Radix,
rm: RoundingMode
) -> Result<(Sign, Vec<u8>, Exponent), Error>
pub fn convert_to_radix(
&self,
rdx: Radix,
rm: RoundingMode
) -> Result<(Sign, Vec<u8>, Exponent), Error>
Converts self
to radix rdx
using rounding mode rm
.
The function returns sign, mantissa digits in radix rdx
, and exponent such that the converted number
can be represented as mantissa digits
* rdx
^ exponent
.
The first element in mantissa is the most significant digit.
Examples
use astro_float::{BigFloatNumber, Sign, RoundingMode, Radix};
let n = BigFloatNumber::from_f64(64, 0.00012345678f64).unwrap();
let (s, m, e) = n.convert_to_radix(Radix::Dec, RoundingMode::None).unwrap();
assert_eq!(s, Sign::Pos);
assert_eq!(m, [1, 2, 3, 4, 5, 6, 7, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 5, 4, 3]);
assert_eq!(e, -3);
Errors
- MemoryAllocation: failed to allocate memory for mantissa.
- ExponentOverflow: the resulting exponent becomes greater than the maximum allowed value for the exponent.