pub struct f256 { /* private fields */ }
Expand description
A 256-bit floating point type (specifically, the “binary256” type defined in IEEE 754-2008).
For details see above.
Implementations§
Source§impl f256
impl f256
Source§impl f256
impl f256
Source§impl f256
impl f256
Sourcepub fn log(&self, base: &Self) -> Self
pub fn log(&self, base: &Self) -> Self
Returns the logarithm of the number with respect to an arbitrary base.
The result might not be correctly rounded owing to implementation details; self.log2() can produce more accurate results for base 2, and self.log10() can produce more accurate results for base 10.
Source§impl f256
impl f256
Sourcepub const MANTISSA_DIGITS: u32 = 237u32
pub const MANTISSA_DIGITS: u32 = 237u32
Number of significant digits in base 2: 237.
Sourcepub const SIGNIFICANT_DIGITS: u32 = 237u32
pub const SIGNIFICANT_DIGITS: u32 = 237u32
Number of significant digits in base 2: 237.
Sourcepub const DIGITS: u32 = 71u32
pub const DIGITS: u32 = 71u32
Approximate number of significant digits in base 10: ⌊log₁₀(2²³⁷)⌋.
Sourcepub const EPSILON: Self = EPSILON
pub const EPSILON: Self = EPSILON
The difference between 1.0
and the next larger representable number:
2⁻²³⁶ ≈ 9.055679 × 10⁻⁷².
Sourcepub const MIN_POSITIVE: Self = MIN_POSITIVE
pub const MIN_POSITIVE: Self = MIN_POSITIVE
Smallest positive normal f256
value: 2⁻²⁶²¹⁴² ≈ 2.4824 × 10⁻⁷⁸⁹¹³.
Sourcepub const MIN_GT_ZERO: Self = MIN_GT_ZERO
pub const MIN_GT_ZERO: Self = MIN_GT_ZERO
Smallest positive subnormal f256
value: 2⁻²⁶²³⁷⁸ ≈ 2.248 × 10⁻⁷⁸⁹⁸⁴.
Sourcepub const MAX_EXP: i32 = 262_144i32
pub const MAX_EXP: i32 = 262_144i32
Maximum possible power of 2 exponent: Eₘₐₓ + 1 = 2¹⁸ = 262144.
Sourcepub const MIN_EXP: i32 = -262_141i32
pub const MIN_EXP: i32 = -262_141i32
One greater than the minimum possible normal power of 2 exponent: Eₘᵢₙ + 1 = -262141.
Sourcepub const MAX_10_EXP: i32 = 78_913i32
pub const MAX_10_EXP: i32 = 78_913i32
Maximum possible power of 10 exponent: ⌊(Eₘₐₓ + 1) × log₁₀(2)⌋.
Sourcepub const MIN_10_EXP: i32 = -78_912i32
pub const MIN_10_EXP: i32 = -78_912i32
Minimum possible normal power of 10 exponent: ⌊(Eₘᵢₙ + 1) × log₁₀(2)⌋.
Sourcepub const NAN: Self = NAN
pub const NAN: Self = NAN
Not a Number (NaN).
Note that IEEE-745 doesn’t define just a single NaN value; a plethora of bit patterns are considered to be NaN. Furthermore, the standard makes a difference between a “signaling” and a “quiet” NaN, and allows inspecting its “payload” (the unspecified bits in the bit pattern). This implementation does not make such a difference and uses exactly one bit pattern for NaN.
Sourcepub const NEG_INFINITY: Self = NEG_INFINITY
pub const NEG_INFINITY: Self = NEG_INFINITY
Negative infinity (−∞).
Sourcepub const fn is_infinite(self) -> bool
pub const fn is_infinite(self) -> bool
Returns true
if this value is positive infinity or negative
infinity, and false
otherwise.
Sourcepub const fn is_subnormal(self) -> bool
pub const fn is_subnormal(self) -> bool
Returns true
if the number is subnormal.
Sourcepub const fn is_normal(self) -> bool
pub const fn is_normal(self) -> bool
Returns true
if the number is neither zero, infinite, subnormal, or
NaN.
Sourcepub const fn classify(&self) -> FpCategory
pub const fn classify(&self) -> FpCategory
Returns the floating point category of the number. If only one property is going to be tested, it is generally faster to use the specific predicate instead.
Sourcepub const fn is_special(self) -> bool
pub const fn is_special(self) -> bool
Returns true
if self
is either not a number, infinite or equal to
zero.
Sourcepub const fn is_sign_positive(self) -> bool
pub const fn is_sign_positive(self) -> bool
Returns true
if self
has a positive sign, including +0.0
,
positive infinity and NaN.
Sourcepub const fn is_sign_negative(self) -> bool
pub const fn is_sign_negative(self) -> bool
Returns true
if self
has a negative sign, including -0.0
and
negative infinity.
Sourcepub fn ulp(&self) -> Self
pub fn ulp(&self) -> Self
Returns the unit in the last place of self
.
ULP denotes the magnitude of the last significand digit of self
.
Sourcepub fn recip(self) -> Self
pub fn recip(self) -> Self
Returns the reciprocal (multiplicative inverse) of self
.
§Examples
let f = f256::from(16);
let r = f256::from(0.0625);
assert_eq!(f.recip(), r);
assert_eq!(f256::INFINITY.recip(), f256::ZERO);
assert_eq!(f256::NEG_ZERO.recip(), f256::NEG_INFINITY);
assert!(f256::NAN.recip().is_nan());
Sourcepub fn to_degrees(self) -> Self
pub fn to_degrees(self) -> Self
Converts radians to degrees.
Returns self * (180 / π)
Sourcepub fn to_radians(self) -> Self
pub fn to_radians(self) -> Self
Converts degrees to radians.
Returns self * (π / 180)
Sourcepub fn max(self, other: Self) -> Self
pub fn max(self, other: Self) -> Self
Returns the maximum of the two numbers, ignoring NaN.
If one of the arguments is NaN, then the other argument is returned. This follows the IEEE-754 2008 semantics for maxNum, except for handling of signaling NaNs; this function handles all NaNs the same way and avoids maxNum’s problems with associativity.
Sourcepub fn min(self, other: Self) -> Self
pub fn min(self, other: Self) -> Self
Returns the minimum of the two numbers, ignoring NaN.
If one of the arguments is NaN, then the other argument is returned. This follows the IEEE-754 2008 semantics for minNum, except for handling of signaling NaNs; this function handles all NaNs the same way and avoids minNum’s problems with associativity.
Sourcepub const fn to_bits(&self) -> (u128, u128)
pub const fn to_bits(&self) -> (u128, u128)
Raw transmutation to (u128, u128)
((self.bits.hi.0, self.bits.lo.0),
each in native endian order).
Sourcepub const fn from_bits(bits: (u128, u128)) -> Self
pub const fn from_bits(bits: (u128, u128)) -> Self
Raw transmutation from (u128, u128)
((self.bits.hi.0,
self.bits.lo.0), each in native endian order).
Sourcepub const fn to_be_bytes(self) -> [u8; 32]
pub const fn to_be_bytes(self) -> [u8; 32]
Return the memory representation of this floating point number as a byte array in big-endian (network) byte order.
Sourcepub const fn to_le_bytes(self) -> [u8; 32]
pub const fn to_le_bytes(self) -> [u8; 32]
Return the memory representation of this floating point number as a byte array in little-endian byte order.
Sourcepub const fn to_ne_bytes(self) -> [u8; 32]
pub const fn to_ne_bytes(self) -> [u8; 32]
Return the memory representation of this floating point number as a byte array in native byte order.
Sourcepub const fn from_be_bytes(bytes: [u8; 32]) -> Self
pub const fn from_be_bytes(bytes: [u8; 32]) -> Self
Create a floating point value from its representation as a byte array in big endian.
Sourcepub const fn from_le_bytes(bytes: [u8; 32]) -> Self
pub const fn from_le_bytes(bytes: [u8; 32]) -> Self
Create a floating point value from its representation as a byte array in little endian.
Sourcepub const fn from_ne_bytes(bytes: [u8; 32]) -> Self
pub const fn from_ne_bytes(bytes: [u8; 32]) -> Self
Create a floating point value from its representation as a byte array in native endian.
Sourcepub fn total_cmp(&self, other: &Self) -> Ordering
pub fn total_cmp(&self, other: &Self) -> Ordering
Return the ordering between self
and other
.
Unlike the standard partial comparison between floating point numbers,
this comparison always produces an ordering in accordance to
the totalOrder
predicate as defined in the IEEE 754 (2008 revision)
floating point standard. The values are ordered in the following
sequence:
- negative NaN
- negative infinity
- negative numbers
- negative subnormal numbers
- negative zero
- positive zero
- positive subnormal numbers
- positive numbers
- positive infinity
- positive NaN.
The ordering established by this function does not always agree with
the PartialOrd
and PartialEq
implementations of f256
.
For example, they consider negative and positive zero equal, while
total_cmp
doesn’t.
Sourcepub fn clamp(self, min: Self, max: Self) -> Self
pub fn clamp(self, min: Self, max: Self) -> Self
Restrict a value to a certain interval unless it is NaN.
Returns max
if self
is greater than max
, and min
if self
is
less than min
. Otherwise this returns self
.
Note that this function returns NaN if the initial value was NaN as well.
§Panics
Panics if min > max
, min
is NaN, or max
is NaN.
§Examples
let min = f256::EPSILON;
let max = f256::TWO;
let f = f256::ONE;
assert_eq!(f.clamp(min, max), f);
assert_eq!((-f).clamp(min, max), min);
assert_eq!(f256::INFINITY.clamp(f256::MIN, f256::MAX), f256::MAX);
assert!(f256::NAN.clamp(f256::NEG_INFINITY, f256::INFINITY).is_nan());
Sourcepub const fn abs(&self) -> Self
pub const fn abs(&self) -> Self
Computes the absolute value of self
.
§Examples
let f = f256::from(138);
assert_eq!(f.abs(), f);
assert_eq!((-f).abs(), f);
assert_eq!(f256::MIN.abs(), f256::MAX);
assert_eq!(f256::NEG_INFINITY.abs(), f256::INFINITY);
assert!(f256::NAN.abs().is_nan());
Sourcepub fn trunc(&self) -> Self
pub fn trunc(&self) -> Self
Returns the integer part of self
. This means that non-integer
numbers are always truncated towards zero.
§Examples
let f = f256::from(177);
let g = f256::from(177.503_f64);
let h = -g;
assert_eq!(f.trunc(), f);
assert_eq!(g.trunc(), f);
assert_eq!(h.trunc(), -f);
Sourcepub fn fract(&self) -> Self
pub fn fract(&self) -> Self
Returns the fractional part of self
.
§Examples
let f = f256::from(177);
let g = f256::from(177.503_f64);
let h = -g;
assert_eq!(f.fract(), f256::ZERO);
assert_eq!(g.fract(), g - f);
assert_eq!(h.fract(), f - g);
Sourcepub fn ceil(&self) -> Self
pub fn ceil(&self) -> Self
Returns the smallest integer greater than or equal to self
.
§Examples
let f = f256::from(28);
let g = f256::from(27.04_f64);
let h = -g;
assert_eq!(f.ceil(), f);
assert_eq!(g.ceil(), f);
assert_eq!(h.ceil(), f256::ONE - f);
Sourcepub fn floor(&self) -> Self
pub fn floor(&self) -> Self
Returns the largest integer less than or equal to self
.
§Examples
let f = f256::from(57);
let g = f256::from(57.009_f64);
let h = -g;
assert_eq!(f.floor(), f);
assert_eq!(g.floor(), f);
assert_eq!(h.floor(), -f - f256::ONE);
Sourcepub fn round(&self) -> Self
pub fn round(&self) -> Self
Returns the nearest integer to self
. Rounds half-way cases away from
zero.
§Examples
let f = f256::from(27);
let g = f256::from(26.704_f64);
let h = f256::from(26.5_f64);
assert_eq!(f.round(), f);
assert_eq!(g.round(), f);
assert_eq!(h.round(), f);
Sourcepub fn round_tie_even(&self) -> Self
pub fn round_tie_even(&self) -> Self
Returns the nearest integer to self
. Rounds half-way cases to the
nearest even.
§Examples
let f = f256::from(28);
let g = f256::from(27.704_f64);
let h = f256::from(27.5_f64);
let h = f256::from(28.5_f64);
assert_eq!(f.round_tie_even(), f);
assert_eq!(g.round_tie_even(), f);
assert_eq!(h.round_tie_even(), f);
Sourcepub fn mul_add(self, f: Self, a: Self) -> Self
pub fn mul_add(self, f: Self, a: Self) -> Self
Fused multiply-add.
Computes (self * f) + a
with only one rounding error, yielding a
more accurate result than a non-fused multiply-add.
Sourcepub fn sum_of_squares(self, other: Self) -> Self
pub fn sum_of_squares(self, other: Self) -> Self
Fused sum of squares.
Computes (self * self) + (other * other)
with only one rounding
error, yielding a more accurate result than a non-fused sum of
squares.
Sourcepub fn square_add(self, a: Self) -> Self
pub fn square_add(self, a: Self) -> Self
Fused square-add.
Computes (self * self) + a
with only one rounding error, yielding a
more accurate result than a non-fused square-add.
Trait Implementations§
Source§impl<T> AddAssign<T> for f256
impl<T> AddAssign<T> for f256
Source§fn add_assign(&mut self, rhs: T)
fn add_assign(&mut self, rhs: T)
+=
operation. Read moreSource§impl<T> DivAssign<T> for f256
impl<T> DivAssign<T> for f256
Source§fn div_assign(&mut self, rhs: T)
fn div_assign(&mut self, rhs: T)
/=
operation. Read moreSource§impl<T> MulAssign<T> for f256
impl<T> MulAssign<T> for f256
Source§fn mul_assign(&mut self, rhs: T)
fn mul_assign(&mut self, rhs: T)
*=
operation. Read moreSource§impl PartialOrd for f256
impl PartialOrd for f256
Source§impl<T> RemAssign<T> for f256
impl<T> RemAssign<T> for f256
Source§fn rem_assign(&mut self, rhs: T)
fn rem_assign(&mut self, rhs: T)
%=
operation. Read moreSource§impl<T> SubAssign<T> for f256
impl<T> SubAssign<T> for f256
Source§fn sub_assign(&mut self, rhs: T)
fn sub_assign(&mut self, rhs: T)
-=
operation. Read more