Struct astro_float::BigFloat

source ·
pub struct BigFloat { /* private fields */ }
Expand description

A floating point number of arbitrary precision.

Implementations§

Returns a new number with value of 0 and precision of p bits. Precision is rounded upwards to the word size.

Constructs a number with precision p from f64 value. Precision is rounded upwards to the word size.

Constructs a number with precision p from f32 value. Precision is rounded upwards to the word size.

Returns true if self is positive infinity.

Returns true if self is negative infinity.

Returns true if self is infinite.

Return true if self is not a number.

Returns the associated with NaN error, if any.

Adds d2 to self and returns the result of the operation with precision p rounded according to rm. Precision is rounded upwards to the word size.

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.

Subtracts d2 from self and returns the result of the operation with precision p rounded according to rm. Precision is rounded upwards to the word size.

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.

Multiplies d2 by self and returns the result of the operation with precision p rounded according to rm. Precision is rounded upwards to the word size.

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.

Divides self by d2 and returns the result of the operation with precision p rounded according to rm. Precision is rounded upwards to the word size.

Returns the remainder of division of |self| by |d2|. The sign of the result is set to the sign of self.

Compares self to d2. Returns positive if self > d2, negative if self < d2, zero if self == d2, None if self or d2 is NaN.

Compares the absolute value of self to the absolute value of d2. Returns positive if |self| is greater than |d2|, negative if |self| is smaller than |d2|, 0 if |self| equals to |d2|, None if self or d2 is NaN.

Reverses the sign of self.

Compute the power of self to the n with precision p. The result is rounded using the rounding mode rm. This function requires constants cache cc for computing the result. Precision is rounded upwards to the word size.

Compute the power of self to the integer n with precision p. The result is rounded using the rounding mode rm. Precision is rounded upwards to the word size.

Computes the logarithm base n of a number with precision p. The result is rounded using the rounding mode rm. This function requires constants cache cc for computing the result. Precision is rounded upwards to the word size.

Returns true if self is positive. The function returns false if self is NaN.

Returns true if self is negative. The function returns false if self is NaN.

Returns true if self is subnormal. A number is subnormal if the most significant bit of the mantissa is not equal to 1.

Returns true if self is zero.

Restricts the value of self to an interval determined by the values of min and max. The function returns max if self is greater than max, min if self is less than min, and self otherwise. If either argument is NaN or min is greater than max, the function returns NaN.

Returns the value of d1 if d1 is greater than self, or the value of self otherwise. If either argument is NaN, the function returns NaN.

Returns value of d1 if d1 is less than self, or the value of self otherwise. If either argument is NaN, the function returns NaN.

Returns a BigFloat with the value -1 if self is negative, 1 if self is positive, zero otherwise. The function returns NaN If self is NaN.

Parses a number from the string s. The function expects s to be a number in scientific format in base 10, or +-Inf, or NaN.

Examples
use astro_float::BigFloat;
use astro_float::Radix;
use astro_float::RoundingMode;

let n = BigFloat::parse("0.0", Radix::Bin, 64, RoundingMode::ToEven);
assert!(n.is_zero());

let n = BigFloat::parse("1.124e-24", Radix::Dec, 128, RoundingMode::ToEven);
assert!(n.sub(&BigFloat::from_f64(1.124e-24, 128), 128, RoundingMode::ToEven).get_exponent() <= Some(-52 - 24));

let n = BigFloat::parse("-Inf", Radix::Hex, 1, RoundingMode::None);
assert!(n.is_inf_neg());

let n = BigFloat::parse("NaN", Radix::Oct, 2, RoundingMode::None);
assert!(n.is_nan());

Returns a random normalized (not subnormal) BigFloat number with exponent in the range from exp_from to exp_to inclusive. The sign can be positive and negative. Zero is excluded. Precision is rounded upwards to the word size. Function does not follow any specific distribution law. The intended use of this function is for testing.

Returns category of self.

Computes the arctangent of a number with precision p. The result is rounded using the rounding mode rm. This function requires constants cache cc for computing the result. Precision is rounded upwards to the word size.

Computes the hyperbolic tangent of a number with precision p. The result is rounded using the rounding mode rm. This function requires constants cache cc for computing the result. Precision is rounded upwards to the word size.

Returns exponent of self or None if self is Inf or NaN.

Returns the number of significant bits used in the mantissa or None if self is Inf or NaN. Normal numbers use all bits of the mantissa. Subnormal numbers use fewer bits than the mantissa can hold.

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.

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.

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.

Returns the minimum positive normal value for the specified precision p: only the most 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.

Returns a new number with value d and the precision p. Precision is rounded upwards to the word size.

Returns a copy of the number with the sign reversed.

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.

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.

This function returns NaN in the following situations:

  • n is larger than the number of bits in m.
  • n is smaller than the number of bits in m, but m does not represent corresponding subnormal number mantissa.
  • n is smaller than the number of bits in m, but e is not the minimum possible exponent.
  • n or the size of m is too large (larger than isize::MAX / 2 + EXPONENT_MIN).

Constructs a number from the slice of words:

  • m is the mantissa.
  • s is the sign.
  • e is the exponent.

Returns the sign of self or None if self is NaN.

Sets the exponent of self. Note that if self is subnormal, the exponent may not change, but the mantissa will shift instead. See example below.

Examples
use astro_float::BigFloat;
use astro_float::EXPONENT_MIN;

// construct a subnormal value.
let mut n = BigFloat::min_positive(128);

assert_eq!(n.get_exponent().unwrap(), EXPONENT_MIN);
assert_eq!(n.get_precision().unwrap(), 1);

// increase exponent.
n.set_exponent(n.get_exponent().unwrap() + 1);

// the outcome for subnormal number.
assert_eq!(n.get_exponent().unwrap(), EXPONENT_MIN);
assert_eq!(n.get_precision().unwrap(), 2);

Returns the maximum mantissa length of self in bits regardless of whether self is normal or subnormal.

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.
  • InvalidArgument: the precision is incorrect.

Computes the reciprocal of a number with precision p. The result is rounded using the rounding mode rm. Precision is rounded upwards to the word size.

Sets the sign of self.

Returns the raw mantissa words of a number.

Converts an array of digits in radix rdx to BigFloat 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. Precision is rounded upwards to the word size.

Examples

Code below converts -0.1234567₈ × 10₈^3₈ given in radix 8 to BigFloat.

use astro_float::{BigFloat, Sign, RoundingMode, Radix};

let g = BigFloat::convert_from_radix(
    Sign::Neg,
    &[1, 2, 3, 4, 5, 6, 7, 0],
    3,
    Radix::Oct,
    64,
    RoundingMode::None);

let n = BigFloat::from_f64(-83.591552734375, 64);

assert!(n.cmp(&g) == Some(0));
Errors

On error, the function returns NaN with the following associated error:

  • MemoryAllocation: failed to allocate memory for mantissa.
  • ExponentOverflow: the resulting exponent becomes greater than the maximum allowed value for the exponent.
  • InvalidArgument: the precision is incorrect, or digits contains unacceptable digits for given radix.

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 the mantissa is the most significant digit.

Examples
use astro_float::{BigFloat, Sign, RoundingMode, Radix};

let n = BigFloat::from_f64(0.00012345678f64, 64);

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, 2]);
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.
  • InvalidArgument: self is Inf or NaN.

Returns the absolute value of self.

Returns the integer part of self.

Returns the fractional part of self.

Returns the smallest integer greater than or equal to self.

Returns the largest integer less than or equal to self.

Returns the rounded number with n binary positions in the fractional part of the number using rounding mode rm.

Computes the square root of a number with precision p. The result is rounded using the rounding mode rm. Precision is rounded upwards to the word size.

Computes the cube root of a number with precision p. The result is rounded using the rounding mode rm. Precision is rounded upwards to the word size.

Computes the natural logarithm of a number with precision p. The result is rounded using the rounding mode rm. This function requires constants cache cc for computing the result. Precision is rounded upwards to the word size.

Computes the logarithm base 2 of a number with precision p. The result is rounded using the rounding mode rm. This function requires constants cache cc for computing the result. Precision is rounded upwards to the word size.

Computes the logarithm base 10 of a number with precision p. The result is rounded using the rounding mode rm. This function requires constants cache cc for computing the result. Precision is rounded upwards to the word size.

Computes e to the power of self with precision p. The result is rounded using the rounding mode rm. This function requires constants cache cc for computing the result. Precision is rounded upwards to the word size.

Computes the sine of a number with precision p. The result is rounded using the rounding mode rm. This function requires constants cache cc for computing the result. Precision is rounded upwards to the word size.

Computes the cosine of a number with precision p. The result is rounded using the rounding mode rm. This function requires constants cache cc for computing the result. Precision is rounded upwards to the word size.

Computes the tangent of a number with precision p. The result is rounded using the rounding mode rm. This function requires constants cache cc for computing the result. Precision is rounded upwards to the word size.

Computes the arcsine of a number with precision p. The result is rounded using the rounding mode rm. This function requires constants cache cc for computing the result. Precision is rounded upwards to the word size.

Computes the arccosine of a number with precision p. The result is rounded using the rounding mode rm. This function requires constants cache cc for computing the result. Precision is rounded upwards to the word size.

Computes the hyperbolic sine of a number with precision p. The result is rounded using the rounding mode rm. This function requires constants cache cc for computing the result. Precision is rounded upwards to the word size.

Computes the hyperbolic cosine of a number with precision p. The result is rounded using the rounding mode rm. This function requires constants cache cc for computing the result. Precision is rounded upwards to the word size.

Computes the hyperbolic arcsine of a number with precision p. The result is rounded using the rounding mode rm. This function requires constants cache cc for computing the result. Precision is rounded upwards to the word size.

Computes the hyperbolic arccosine of a number with precision p. The result is rounded using the rounding mode rm. This function requires constants cache cc for computing the result. Precision is rounded upwards to the word size.

Computes the hyperbolic arctangent of a number with precision p. The result is rounded using the rounding mode rm. This function requires constants cache cc for computing the result. Precision is rounded upwards to the word size.

Constructs BigFloat with precision p from an integer value i. Precision is rounded upwards to the word size.

Constructs BigFloat with precision p from an integer value i. Precision is rounded upwards to the word size.

Constructs BigFloat with precision p from an integer value i. Precision is rounded upwards to the word size.

Constructs BigFloat with precision p from an integer value i. Precision is rounded upwards to the word size.

Constructs BigFloat with precision p from an integer value i. Precision is rounded upwards to the word size.

Constructs BigFloat with precision p from an integer value i. Precision is rounded upwards to the word size.

Constructs BigFloat with precision p from an integer value i. Precision is rounded upwards to the word size.

Constructs BigFloat with precision p from an integer value i. Precision is rounded upwards to the word size.

Constructs BigFloat with precision p from an integer value i. Precision is rounded upwards to the word size.

Constructs BigFloat with precision p from an integer value i. Precision is rounded upwards to the word size.

Trait Implementations§

Formats the value using the given formatter.
Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more
Formats the value using the given formatter. Read more
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.

Returns parsed number or NAN in case of error.

The associated error which can be returned from parsing.
The resulting type after applying the - operator.
Performs the unary - operation. Read more
The resulting type after applying the - operator.
Performs the unary - operation. Read more
Formats the value using the given formatter.
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
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
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
Formats the value using the given formatter.

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 the argument unchanged.

Calls U::from(self).

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

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.