Struct num_bigfloat::BigFloat
source · pub struct BigFloat { /* private fields */ }
Expand description
Number representation.
Implementations§
source§impl BigFloat
impl BigFloat
sourcepub fn from_bytes(bytes: &[u8], sign: i8, exponent: i8) -> Self
pub fn from_bytes(bytes: &[u8], sign: i8, exponent: i8) -> Self
Creates a BigFloat value from a sequence of bytes
. Each byte must represent a decimal digit.
First byte is the most significant. bytes
can be of any length.
If bytes
is longer than required, then the remaining bytes are ignored.
If the “sign” is negative, then the resulting BigFloat will be negative.
§Examples
let n1 = BigFloat::from_bytes(&[1,2,3,4,2,0,0,0], 1, -3);
let n2 = BigFloat::from_u32(12342);
assert!(n1.cmp(&n2) == Some(0));
sourcepub fn from_f64(f: f64) -> Self
pub fn from_f64(f: f64) -> Self
Creates a BigFloat from f64. The conversion is not guaranteed to be lossless since BigFloat and f64 have different bases.
sourcepub fn from_f32(f: f32) -> Self
pub fn from_f32(f: f32) -> Self
Creates a BigFloat from f32. The conversion is not guaranteed to be lossless since BigFloat and f64 have different bases.
sourcepub fn to_i64(&self) -> Option<i64>
pub fn to_i64(&self) -> Option<i64>
Converts BigFloat to i64.
The function retursn None if self
is Inf, NaN, or out of range of i64.
sourcepub fn to_i128(&self) -> Option<i128>
pub fn to_i128(&self) -> Option<i128>
Converts BigFloat to i128.
The function retursn None if self
is Inf, NaN, or out of range of i128.
sourcepub fn to_u64(&self) -> Option<u64>
pub fn to_u64(&self) -> Option<u64>
Converts absolute value of self
to u64.
The function retursn None if self
is Inf, NaN, or out of range of u64.
sourcepub fn to_u128(&self) -> Option<u128>
pub fn to_u128(&self) -> Option<u128>
Converts absolute value of self
to u128.
The function retursn None if self
is Inf, NaN, or out of range of u128.
sourcepub fn get_mantissa_bytes(&self, bytes: &mut [u8])
pub fn get_mantissa_bytes(&self, bytes: &mut [u8])
Returns the mantissa of the BigFloat in bytes
. Each byte represents a decimal digit.
The first byte is the most significant. bytes
can be of any length.
If the length of bytes
is less than the number of decimal positions filled in the mantissa,
then the rest of the mantissa will be omitted.
The length of the mantissa can be determined using get_mantissa_len
.
If self
is Inf or NaN, nothing is returned.
§Examples
let n = BigFloat::from_f64(123.42);
let mut m = [0; 40];
n.get_mantissa_bytes(&mut m);
// compare m[0..10] to [1,2,3,4,2,0,0,0,0,0]
assert!(m[0..10].iter().zip([1,2,3,4,2,0,0,0,0,0].iter()).filter(|x| { x.0 != x.1 }).count() == 0);
sourcepub fn get_mantissa_len(&self) -> usize
pub fn get_mantissa_len(&self) -> usize
Returns the number of decimal places filled in the mantissa.
If self
is Inf or NaN, 0 is returned.
sourcepub fn get_sign(&self) -> i8
pub fn get_sign(&self) -> i8
Returns 1 if BigFloat is positive, -1 otherwise.
If self
is NaN, 0 is returned.
sourcepub fn get_exponent(&self) -> i8
pub fn get_exponent(&self) -> i8
Returns the exponent part of the number.
If self
is Inf or NaN, 0 is returned.
sourcepub fn set_exponent(&mut self, e: i8)
pub fn set_exponent(&mut self, e: i8)
Sets the exponent part of the number. The function has no effect on Inf and NaN values.
sourcepub fn to_raw_parts(&self) -> Option<([i16; 10], i16, i8, i8)>
pub fn to_raw_parts(&self) -> Option<([i16; 10], i16, i8, i8)>
Returns the raw parts of the number: the mantissa, the number of decimal places in the mantissa,
the sign, and the exponent.
If self
is Inf or NaN, None is returned.
sourcepub fn from_raw_parts(
mantissa: [i16; 10],
mantissa_len: i16,
sign: i8,
exponent: i8
) -> Self
pub fn from_raw_parts( mantissa: [i16; 10], mantissa_len: i16, sign: i8, exponent: i8 ) -> Self
Creates a BigFloat from the raw parts. to_raw_parts
can be used to get the raw parts of a number.
sourcepub fn is_inf_pos(&self) -> bool
pub fn is_inf_pos(&self) -> bool
Returns true if self
is positive infinity.
sourcepub fn is_inf_neg(&self) -> bool
pub fn is_inf_neg(&self) -> bool
Returns true if self
is negative infinity.
sourcepub fn sub(&self, d2: &Self) -> Self
pub fn sub(&self, d2: &Self) -> Self
Subtracts d2
from self
and return the result of the subtraction.
sourcepub fn mul(&self, d2: &Self) -> Self
pub fn mul(&self, d2: &Self) -> Self
Multiplies self
by d2
and returns the result of the multiplication.
sourcepub fn div(&self, d2: &Self) -> Self
pub fn div(&self, d2: &Self) -> Self
Divides self
by d2
and returns the result of the division.
sourcepub fn cmp(&self, d2: &BigFloat) -> Option<i16>
pub fn cmp(&self, d2: &BigFloat) -> Option<i16>
Compares self
to d2
.
Returns positive if self
> d2
, negative if self
< d2
, zero if self
== d2
, None if self
or d2
is NaN.
sourcepub fn is_positive(&self) -> bool
pub fn is_positive(&self) -> bool
Returns true if self
is positive.
The function returns false if self
is NaN.
sourcepub fn is_negative(&self) -> bool
pub fn is_negative(&self) -> bool
Returns true if self
is negative.
The function returns false if self
is NaN.
sourcepub fn is_subnormal(&self) -> bool
pub fn is_subnormal(&self) -> bool
Returns true if self
is subnormal.
A number is considered subnormal if not all digits of the mantissa are used, and the exponent has the minimum possible value.
sourcepub fn clamp(&self, min: &Self, max: &Self) -> Self
pub fn clamp(&self, min: &Self, max: &Self) -> Self
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.
sourcepub fn max(&self, d1: &Self) -> Self
pub fn max(&self, d1: &Self) -> Self
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.
sourcepub fn min(&self, d1: &Self) -> Self
pub fn min(&self, d1: &Self) -> Self
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.
sourcepub fn signum(&self) -> Self
pub fn signum(&self) -> Self
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.
sourcepub fn parse(s: &str) -> Option<Self>
pub fn parse(s: &str) -> Option<Self>
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 num_bigfloat::BigFloat;
let n = BigFloat::parse("0.0").unwrap();
assert!(n.to_f64() == 0.0);
let n = BigFloat::parse("1.123e-123").unwrap();
assert!((n.to_f64() - 1.123e-123).abs() < f64::EPSILON);
let n = BigFloat::parse("-Inf").unwrap();
assert!(n.to_f64() == f64::NEG_INFINITY);
let n = BigFloat::parse("NaN").unwrap();
assert!(n.to_f64().is_nan());
sourcepub fn random_normal(exp_from: i8, exp_to: i8) -> Result<Self, Error>
pub fn random_normal(exp_from: i8, exp_to: i8) -> Result<Self, Error>
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.
Function does not follow any specific distribution law.
The intended use of this function is for testing.
§Errors
InvalidArgument - when exp_from
is greater than exp_to
.
sourcepub fn classify(&self) -> FpCategory
pub fn classify(&self) -> FpCategory
Returns category of self
as defined by core::num::FpCategory
.
source§impl BigFloat
impl BigFloat
sourcepub fn round(&self, n: usize, rm: RoundingMode) -> Self
pub fn round(&self, n: usize, rm: RoundingMode) -> Self
Returns a rounded number with n
decimal positions in the fractional part of the number using the rounding mode rm
.
sourcepub fn sin(&self) -> Self
pub fn sin(&self) -> Self
Returns the sine of self
. The function takes an angle in radians as an argument.
sourcepub fn cos(&self) -> Self
pub fn cos(&self) -> Self
Returns the cosine of self
. The function takes an angle in radians as an argument.
sourcepub fn tan(&self) -> Self
pub fn tan(&self) -> Self
Returns the tangent of self
. The function takes an angle in radians as an argument.
sourcepub fn asin(&self) -> Self
pub fn asin(&self) -> Self
Returns the arcsine of self
. The result is an angle in radians ranging from -pi/2 to pi/2.
sourcepub fn acos(&self) -> Self
pub fn acos(&self) -> Self
Returns the arccosine of self
. The result is an angle in radians ranging from 0 to pi.
Trait Implementations§
source§impl AddAssign<&BigFloat> for BigFloat
impl AddAssign<&BigFloat> for BigFloat
source§fn add_assign(&mut self, rhs: &BigFloat)
fn add_assign(&mut self, rhs: &BigFloat)
+=
operation. Read moresource§impl AddAssign for BigFloat
impl AddAssign for BigFloat
source§fn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
+=
operation. Read moresource§impl<'de> Deserialize<'de> for BigFloat
impl<'de> Deserialize<'de> for BigFloat
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
source§impl DivAssign<&BigFloat> for BigFloat
impl DivAssign<&BigFloat> for BigFloat
source§fn div_assign(&mut self, rhs: &BigFloat)
fn div_assign(&mut self, rhs: &BigFloat)
/=
operation. Read moresource§impl DivAssign for BigFloat
impl DivAssign for BigFloat
source§fn div_assign(&mut self, rhs: Self)
fn div_assign(&mut self, rhs: Self)
/=
operation. Read moresource§impl Euclid for BigFloat
impl Euclid for BigFloat
source§fn div_euclid(&self, v: &BigFloat) -> BigFloat
fn div_euclid(&self, v: &BigFloat) -> BigFloat
rem_euclid
. Read moresource§fn rem_euclid(&self, v: &BigFloat) -> BigFloat
fn rem_euclid(&self, v: &BigFloat) -> BigFloat
self (mod v)
. Read moresource§fn div_rem_euclid(&self, v: &Self) -> (Self, Self)
fn div_rem_euclid(&self, v: &Self) -> (Self, Self)
source§impl Float for BigFloat
impl Float for BigFloat
source§fn neg_zero() -> Self
fn neg_zero() -> Self
This function is provided only for compatibility since -0.0
is not implemented.
source§fn is_sign_positive(self) -> bool
fn is_sign_positive(self) -> bool
Note: BigFloat NaN has no sign.
source§fn is_sign_negative(self) -> bool
fn is_sign_negative(self) -> bool
Note: BigFloat NaN has no sign.
source§fn mul_add(self, a: Self, b: Self) -> Self
fn mul_add(self, a: Self, b: Self) -> Self
This function is provided only for compatibility. It is not faster than separate multiplication and addition.
source§fn powi(self, n: i32) -> Self
fn powi(self, n: i32) -> Self
This function is provided only for compatibility. It is not faster than powf
.
source§fn sin_cos(self) -> (Self, Self)
fn sin_cos(self) -> (Self, Self)
This function is provided only for compatibility.
source§fn integer_decode(self) -> (u64, i16, i8)
fn integer_decode(self) -> (u64, i16, i8)
This function converts BigFloat to f64 and decomposes it.
source§fn neg_infinity() -> Self
fn neg_infinity() -> Self
source§fn min_value() -> Self
fn min_value() -> Self
source§fn min_positive_value() -> Self
fn min_positive_value() -> Self
source§fn max_value() -> Self
fn max_value() -> Self
source§fn is_infinite(self) -> bool
fn is_infinite(self) -> bool
true
if this value is positive infinity or negative infinity and
false otherwise. Read moresource§fn classify(self) -> FpCategory
fn classify(self) -> FpCategory
source§fn ceil(self) -> Self
fn ceil(self) -> Self
source§fn round(self) -> Self
fn round(self) -> Self
0.0
. Read moresource§fn log(self, base: Self) -> Self
fn log(self, base: Self) -> Self
source§fn hypot(self, other: Self) -> Self
fn hypot(self, other: Self) -> Self
x
and y
. Read moresource§fn asin(self) -> Self
fn asin(self) -> Self
source§fn acos(self) -> Self
fn acos(self) -> Self
source§fn atan(self) -> Self
fn atan(self) -> Self
source§fn to_degrees(self) -> Self
fn to_degrees(self) -> Self
source§fn to_radians(self) -> Self
fn to_radians(self) -> Self
source§impl FloatConst for BigFloat
impl FloatConst for BigFloat
source§fn FRAC_1_SQRT_2() -> Self
fn FRAC_1_SQRT_2() -> Self
1.0 / sqrt(2.0)
.source§fn FRAC_2_SQRT_PI() -> Self
fn FRAC_2_SQRT_PI() -> Self
2.0 / sqrt(π)
.source§impl FromPrimitive for BigFloat
impl FromPrimitive for BigFloat
source§fn from_i64(n: i64) -> Option<Self>
fn from_i64(n: i64) -> Option<Self>
i64
to return an optional value of this type. If the
value cannot be represented by this type, then None
is returned.source§fn from_u64(n: u64) -> Option<Self>
fn from_u64(n: u64) -> Option<Self>
u64
to return an optional value of this type. If the
value cannot be represented by this type, then None
is returned.source§fn from_i128(n: i128) -> Option<Self>
fn from_i128(n: i128) -> Option<Self>
i128
to return an optional value of this type. If the
value cannot be represented by this type, then None
is returned. Read moresource§fn from_u128(n: u128) -> Option<Self>
fn from_u128(n: u128) -> Option<Self>
u128
to return an optional value of this type. If the
value cannot be represented by this type, then None
is returned. Read moresource§fn from_f32(n: f32) -> Option<Self>
fn from_f32(n: f32) -> Option<Self>
f32
to return an optional value of this type. If the
value cannot be represented by this type, then None
is returned.source§fn from_f64(n: f64) -> Option<Self>
fn from_f64(n: f64) -> Option<Self>
f64
to return an optional value of this type. If the
value cannot be represented by this type, then None
is returned. Read moresource§fn from_isize(n: isize) -> Option<Self>
fn from_isize(n: isize) -> Option<Self>
isize
to return an optional value of this type. If the
value cannot be represented by this type, then None
is returned.source§fn from_i8(n: i8) -> Option<Self>
fn from_i8(n: i8) -> Option<Self>
i8
to return an optional value of this type. If the
value cannot be represented by this type, then None
is returned.source§fn from_i16(n: i16) -> Option<Self>
fn from_i16(n: i16) -> Option<Self>
i16
to return an optional value of this type. If the
value cannot be represented by this type, then None
is returned.source§fn from_i32(n: i32) -> Option<Self>
fn from_i32(n: i32) -> Option<Self>
i32
to return an optional value of this type. If the
value cannot be represented by this type, then None
is returned.source§fn from_usize(n: usize) -> Option<Self>
fn from_usize(n: usize) -> Option<Self>
usize
to return an optional value of this type. If the
value cannot be represented by this type, then None
is returned.source§fn from_u8(n: u8) -> Option<Self>
fn from_u8(n: u8) -> Option<Self>
u8
to return an optional value of this type. If the
value cannot be represented by this type, then None
is returned.source§impl MulAdd for BigFloat
impl MulAdd for BigFloat
This trait is provided only for compatibility. It does not provide performance benefits.
source§impl MulAddAssign for BigFloat
impl MulAddAssign for BigFloat
This trait is provided only for compatibility. It does not provide performance benefits.
source§fn mul_add_assign(&mut self, a: Self, b: Self)
fn mul_add_assign(&mut self, a: Self, b: Self)
*self = (*self * a) + b
source§impl MulAssign<&BigFloat> for BigFloat
impl MulAssign<&BigFloat> for BigFloat
source§fn mul_assign(&mut self, rhs: &BigFloat)
fn mul_assign(&mut self, rhs: &BigFloat)
*=
operation. Read moresource§impl MulAssign for BigFloat
impl MulAssign for BigFloat
source§fn mul_assign(&mut self, rhs: Self)
fn mul_assign(&mut self, rhs: Self)
*=
operation. Read moresource§impl Num for BigFloat
impl Num for BigFloat
type FromStrRadixErr = Error
source§fn from_str_radix(str: &str, radix: u32) -> Result<Self, Self::FromStrRadixErr>
fn from_str_radix(str: &str, radix: u32) -> Result<Self, Self::FromStrRadixErr>
2..=36
). Read moresource§impl PartialEq for BigFloat
impl PartialEq for BigFloat
source§impl PartialOrd for BigFloat
impl PartialOrd for BigFloat
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 Signed for BigFloat
impl Signed for BigFloat
source§fn is_positive(&self) -> bool
fn is_positive(&self) -> bool
source§fn is_negative(&self) -> bool
fn is_negative(&self) -> bool
source§impl SubAssign<&BigFloat> for BigFloat
impl SubAssign<&BigFloat> for BigFloat
source§fn sub_assign(&mut self, rhs: &BigFloat)
fn sub_assign(&mut self, rhs: &BigFloat)
-=
operation. Read moresource§impl SubAssign for BigFloat
impl SubAssign for BigFloat
source§fn sub_assign(&mut self, rhs: Self)
fn sub_assign(&mut self, rhs: Self)
-=
operation. Read moresource§impl ToPrimitive for BigFloat
impl ToPrimitive for BigFloat
source§fn to_i64(&self) -> Option<i64>
fn to_i64(&self) -> Option<i64>
self
to an i64
. If the value cannot be
represented by an i64
, then None
is returned.source§fn to_u64(&self) -> Option<u64>
fn to_u64(&self) -> Option<u64>
self
to a u64
. If the value cannot be
represented by a u64
, then None
is returned.source§fn to_i128(&self) -> Option<i128>
fn to_i128(&self) -> Option<i128>
self
to an i128
. If the value cannot be
represented by an i128
(i64
under the default implementation), then
None
is returned. Read moresource§fn to_u128(&self) -> Option<u128>
fn to_u128(&self) -> Option<u128>
self
to a u128
. If the value cannot be
represented by a u128
(u64
under the default implementation), then
None
is returned. Read moresource§fn to_f64(&self) -> Option<f64>
fn to_f64(&self) -> Option<f64>
self
to an f64
. Overflows may map to positive
or negative inifinity, otherwise None
is returned if the value cannot
be represented by an f64
. Read moresource§fn to_isize(&self) -> Option<isize>
fn to_isize(&self) -> Option<isize>
self
to an isize
. If the value cannot be
represented by an isize
, then None
is returned.source§fn to_i8(&self) -> Option<i8>
fn to_i8(&self) -> Option<i8>
self
to an i8
. If the value cannot be
represented by an i8
, then None
is returned.source§fn to_i16(&self) -> Option<i16>
fn to_i16(&self) -> Option<i16>
self
to an i16
. If the value cannot be
represented by an i16
, then None
is returned.source§fn to_i32(&self) -> Option<i32>
fn to_i32(&self) -> Option<i32>
self
to an i32
. If the value cannot be
represented by an i32
, then None
is returned.source§fn to_usize(&self) -> Option<usize>
fn to_usize(&self) -> Option<usize>
self
to a usize
. If the value cannot be
represented by a usize
, then None
is returned.source§fn to_u8(&self) -> Option<u8>
fn to_u8(&self) -> Option<u8>
self
to a u8
. If the value cannot be
represented by a u8
, then None
is returned.source§fn to_u16(&self) -> Option<u16>
fn to_u16(&self) -> Option<u16>
self
to a u16
. If the value cannot be
represented by a u16
, then None
is returned.impl Copy for BigFloat
impl Eq for BigFloat
Auto Trait Implementations§
impl RefUnwindSafe for BigFloat
impl Send for BigFloat
impl Sync for BigFloat
impl Unpin for BigFloat
impl UnwindSafe for BigFloat
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> LowerBounded for Twhere
T: Bounded,
impl<T> LowerBounded for Twhere
T: Bounded,
source§impl<T> Real for Twhere
T: Float,
impl<T> Real for Twhere
T: Float,
source§fn min_positive_value() -> T
fn min_positive_value() -> T
source§fn round(self) -> T
fn round(self) -> T
0.0
. Read moresource§fn is_sign_positive(self) -> bool
fn is_sign_positive(self) -> bool
true
if self
is positive, including +0.0
,
Float::infinity()
, and with newer versions of Rust f64::NAN
. Read moresource§fn is_sign_negative(self) -> bool
fn is_sign_negative(self) -> bool
true
if self
is negative, including -0.0
,
Float::neg_infinity()
, and with newer versions of Rust -f64::NAN
. Read moresource§fn mul_add(self, a: T, b: T) -> T
fn mul_add(self, a: T, b: T) -> T
(self * a) + b
with only one rounding
error, yielding a more accurate result than an unfused multiply-add. Read moresource§fn log(self, base: T) -> T
fn log(self, base: T) -> T
source§fn to_degrees(self) -> T
fn to_degrees(self) -> T
source§fn to_radians(self) -> T
fn to_radians(self) -> T
source§fn hypot(self, other: T) -> T
fn hypot(self, other: T) -> T
x
and y
. Read moresource§fn asin(self) -> T
fn asin(self) -> T
source§fn acos(self) -> T
fn acos(self) -> T
source§fn atan(self) -> T
fn atan(self) -> T
source§fn exp_m1(self) -> T
fn exp_m1(self) -> T
e^(self) - 1
in a way that is accurate even if the
number is close to zero. Read more