pub trait Float: Number + Neg<Output = Self> {
type Unsigned: UnsignedInteger;
Show 21 associated constants and 22 methods
const ZERO: Self;
const ONE: Self;
const TWO: Self;
const MAX: Self;
const MIN: Self;
const INFINITY: Self;
const NEG_INFINITY: Self;
const NAN: Self;
const BITS: usize;
const SIGN_MASK: Self::Unsigned;
const EXPONENT_MASK: Self::Unsigned;
const HIDDEN_BIT_MASK: Self::Unsigned;
const MANTISSA_MASK: Self::Unsigned;
const CARRY_MASK: Self::Unsigned;
const INFINITY_BITS: Self::Unsigned;
const NEGATIVE_INFINITY_BITS: Self::Unsigned;
const EXPONENT_SIZE: i32;
const MANTISSA_SIZE: i32;
const EXPONENT_BIAS: i32;
const DENORMAL_EXPONENT: i32;
const MAX_EXPONENT: i32;
// Required methods
fn to_bits(self) -> Self::Unsigned;
fn from_bits(u: Self::Unsigned) -> Self;
fn ln(self) -> Self;
fn floor(self) -> Self;
fn is_sign_positive(self) -> bool;
fn is_sign_negative(self) -> bool;
// Provided methods
fn is_denormal(self) -> bool { ... }
fn is_special(self) -> bool { ... }
fn is_nan(self) -> bool { ... }
fn is_inf(self) -> bool { ... }
fn is_odd(self) -> bool { ... }
fn is_even(self) -> bool { ... }
fn needs_negative_sign(self) -> bool { ... }
fn exponent(self) -> i32 { ... }
fn mantissa(self) -> Self::Unsigned { ... }
fn next(self) -> Self { ... }
fn next_positive(self) -> Self { ... }
fn prev(self) -> Self { ... }
fn prev_positive(self) -> Self { ... }
fn round_positive_even(self) -> Self { ... }
fn max_finite(self, f: Self) -> Self { ... }
fn min_finite(self, f: Self) -> Self { ... }
}
parse-floats
or write-floats
only.Expand description
The trait for floating-point numbers
.
Floating-point numbers are numbers that may contain a fraction and are stored internally as the significant digits and an exponent of base 2.
Required Associated Constants§
Sourceconst INFINITY: Self
const INFINITY: Self
Infinity (∞
).
See f64::INFINITY
.
Sourceconst NEG_INFINITY: Self
const NEG_INFINITY: Self
Negative infinity (−∞
).
See f64::NEG_INFINITY
.
Sourceconst EXPONENT_MASK: Self::Unsigned
const EXPONENT_MASK: Self::Unsigned
Bitmask to extract the biased exponent, including the hidden bit.
Sourceconst HIDDEN_BIT_MASK: Self::Unsigned
const HIDDEN_BIT_MASK: Self::Unsigned
Bitmask to extract the hidden bit in the exponent, which is an implicit 1 in the significant digits.
Sourceconst MANTISSA_MASK: Self::Unsigned
const MANTISSA_MASK: Self::Unsigned
Bitmask to extract the mantissa (significant digits), excluding the hidden bit.
Sourceconst CARRY_MASK: Self::Unsigned
const CARRY_MASK: Self::Unsigned
Mask to determine if a full-carry occurred (1 in bit above hidden bit).
Sourceconst INFINITY_BITS: Self::Unsigned
const INFINITY_BITS: Self::Unsigned
Positive infinity as bits.
Sourceconst NEGATIVE_INFINITY_BITS: Self::Unsigned
const NEGATIVE_INFINITY_BITS: Self::Unsigned
Positive infinity as bits.
Sourceconst EXPONENT_SIZE: i32
const EXPONENT_SIZE: i32
The number of bits in the exponent.
Sourceconst MANTISSA_SIZE: i32
const MANTISSA_SIZE: i32
Size of the significand (mantissa) without the hidden bit.
Sourceconst EXPONENT_BIAS: i32
const EXPONENT_BIAS: i32
Bias of the exponent. See exponent bias
.
Sourceconst DENORMAL_EXPONENT: i32
const DENORMAL_EXPONENT: i32
Exponent portion of a denormal
float.
Sourceconst MAX_EXPONENT: i32
const MAX_EXPONENT: i32
Maximum (unbiased) exponent value in the float.
Required Associated Types§
Sourcetype Unsigned: UnsignedInteger
type Unsigned: UnsignedInteger
Unsigned type of the same size.
Required Methods§
Sourcefn to_bits(self) -> Self::Unsigned
fn to_bits(self) -> Self::Unsigned
Raw transmutation to the unsigned integral type.
See f64::to_bits
.
Sourcefn from_bits(u: Self::Unsigned) -> Self
fn from_bits(u: Self::Unsigned) -> Self
Raw transmutation from the unsigned integral type.
See f64::from_bits
.
Sourcefn floor(self) -> Self
fn floor(self) -> Self
Returns the largest integer less than or equal to self
.
See f64::floor
.
Sourcefn is_sign_positive(self) -> bool
fn is_sign_positive(self) -> bool
Returns true if self
has a positive sign, including +0.0
,
NaNs with positive sign bit and positive infinity.
Sourcefn is_sign_negative(self) -> bool
fn is_sign_negative(self) -> bool
Returns true if self
has a negative sign, including -0.0
,
NaNs with negative sign bit and negative infinity.
Provided Methods§
Sourcefn is_denormal(self) -> bool
fn is_denormal(self) -> bool
Returns true if the float is denormal
.
Denormal (subnormal) numbers fall below the range of numbers
that can be stored as mantissa * 2^exp
, and therefore
always have the minimum exponent.
Sourcefn is_special(self) -> bool
fn is_special(self) -> bool
Returns true if the float is NaN, positive infinity, or negative infinity.
Sourcefn needs_negative_sign(self) -> bool
fn needs_negative_sign(self) -> bool
Returns true if the float needs a negative sign when serializing it.
This is true if it’s -0.0
or it’s below 0 and not NaN. But inf values
need the sign.
Sourcefn next(self) -> Self
fn next(self) -> Self
Get next greater float.
§Examples
use lexical_util::num::Float;
assert_eq!(1f32.next(), 1.0000001);
assert_eq!((-0.0f32).next(), 0.0); // +0.0
assert_eq!(0f32.next(), 1e-45);
Sourcefn next_positive(self) -> Self
fn next_positive(self) -> Self
Get next greater float for a positive float.
Value must be >= 0.0 and < INFINITY.
Sourcefn prev(self) -> Self
fn prev(self) -> Self
Get previous greater float, such that self.prev().next() == self
.
§Examples
use lexical_util::num::Float;
assert_eq!(1f32.prev(), 0.99999994);
assert_eq!(0.0f32.prev(), 0.0); // -0.0
assert_eq!((-0.0f32).prev(), -1e-45);
Sourcefn prev_positive(self) -> Self
fn prev_positive(self) -> Self
Get previous greater float for a positive float. Value must be > 0.0.
Sourcefn round_positive_even(self) -> Self
fn round_positive_even(self) -> Self
Round a positive number to even.
Sourcefn max_finite(self, f: Self) -> Self
fn max_finite(self, f: Self) -> Self
Get the max of two finite numbers.
This assumes that both floats form a total ord
,
that is, x < y
is always y >= x
. Non-finite floats,
such as NaN, break this criteria, but finite floats enable
simpler (and faster) comparison criteria while remaining
accurate.
Sourcefn min_finite(self, f: Self) -> Self
fn min_finite(self, f: Self) -> Self
Get the min of two finite numbers.
This assumes that both floats form a total ord
,
that is, x < y
is always y >= x
. Non-finite floats,
such as NaN, break this criteria, but finite floats enable
simpler (and faster) comparison criteria while remaining
accurate.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.
Implementations on Foreign Types§
Source§impl Float for f32
impl Float for f32
const ZERO: f32 = 0f32
const ONE: f32 = 1f32
const TWO: f32 = 2f32
const MAX: f32 = 3.40282347E+38f32
const MIN: f32 = -3.40282347E+38f32
const INFINITY: f32 = +Inf_f32
const NEG_INFINITY: f32 = -Inf_f32
const NAN: f32 = NaN_f32
const BITS: usize = 32usize
const SIGN_MASK: Self::Unsigned = {transmute(0x80000000): <f32 as num::Float>::Unsigned}
const EXPONENT_MASK: Self::Unsigned = {transmute(0x7f800000): <f32 as num::Float>::Unsigned}
const HIDDEN_BIT_MASK: Self::Unsigned = {transmute(0x00800000): <f32 as num::Float>::Unsigned}
const MANTISSA_MASK: Self::Unsigned = {transmute(0x007fffff): <f32 as num::Float>::Unsigned}
const CARRY_MASK: Self::Unsigned = {transmute(0x01000000): <f32 as num::Float>::Unsigned}
const INFINITY_BITS: Self::Unsigned = {transmute(0x7f800000): <f32 as num::Float>::Unsigned}
const NEGATIVE_INFINITY_BITS: Self::Unsigned = {transmute(0xff800000): <f32 as num::Float>::Unsigned}
const EXPONENT_SIZE: i32 = 8i32
const MANTISSA_SIZE: i32 = 23i32
const EXPONENT_BIAS: i32 = 150i32
const DENORMAL_EXPONENT: i32 = -149i32
const MAX_EXPONENT: i32 = 105i32
type Unsigned = u32
fn to_bits(self) -> u32
fn from_bits(u: u32) -> f32
fn ln(self) -> f32
fn floor(self) -> f32
fn is_sign_positive(self) -> bool
fn is_sign_negative(self) -> bool
Source§impl Float for f64
impl Float for f64
const ZERO: f64 = 0f64
const ONE: f64 = 1f64
const TWO: f64 = 2f64
const MAX: f64 = 1.7976931348623157E+308f64
const MIN: f64 = -1.7976931348623157E+308f64
const INFINITY: f64 = +Inf_f64
const NEG_INFINITY: f64 = -Inf_f64
const NAN: f64 = NaN_f64
const BITS: usize = 64usize
const SIGN_MASK: Self::Unsigned = {transmute(0x8000000000000000): <f64 as num::Float>::Unsigned}
const EXPONENT_MASK: Self::Unsigned = {transmute(0x7ff0000000000000): <f64 as num::Float>::Unsigned}
const HIDDEN_BIT_MASK: Self::Unsigned = {transmute(0x0010000000000000): <f64 as num::Float>::Unsigned}
const MANTISSA_MASK: Self::Unsigned = {transmute(0x000fffffffffffff): <f64 as num::Float>::Unsigned}
const CARRY_MASK: Self::Unsigned = {transmute(0x0020000000000000): <f64 as num::Float>::Unsigned}
const INFINITY_BITS: Self::Unsigned = {transmute(0x7ff0000000000000): <f64 as num::Float>::Unsigned}
const NEGATIVE_INFINITY_BITS: Self::Unsigned = {transmute(0xfff0000000000000): <f64 as num::Float>::Unsigned}
const EXPONENT_SIZE: i32 = 11i32
const MANTISSA_SIZE: i32 = 52i32
const EXPONENT_BIAS: i32 = 1_075i32
const DENORMAL_EXPONENT: i32 = -1_074i32
const MAX_EXPONENT: i32 = 972i32
type Unsigned = u64
fn to_bits(self) -> u64
fn from_bits(u: u64) -> f64
fn ln(self) -> f64
fn floor(self) -> f64
fn is_sign_positive(self) -> bool
fn is_sign_negative(self) -> bool
Implementors§
Source§impl Float for bf16
Available on crate feature f16
only.
impl Float for bf16
f16
only.const ZERO: Self
const ONE: Self
const TWO: Self
const MAX: Self
const MIN: Self
const INFINITY: Self
const NEG_INFINITY: Self
const NAN: Self
const BITS: usize = 16usize
const SIGN_MASK: Self::Unsigned = {transmute(0x8000): <float16::bf16 as num::Float>::Unsigned}
const EXPONENT_MASK: Self::Unsigned = {transmute(0x7f80): <float16::bf16 as num::Float>::Unsigned}
const HIDDEN_BIT_MASK: Self::Unsigned = {transmute(0x0080): <float16::bf16 as num::Float>::Unsigned}
const MANTISSA_MASK: Self::Unsigned = {transmute(0x007f): <float16::bf16 as num::Float>::Unsigned}
const CARRY_MASK: Self::Unsigned = {transmute(0x0100): <float16::bf16 as num::Float>::Unsigned}
const INFINITY_BITS: Self::Unsigned = {transmute(0x7f80): <float16::bf16 as num::Float>::Unsigned}
const NEGATIVE_INFINITY_BITS: Self::Unsigned = {transmute(0xff80): <float16::bf16 as num::Float>::Unsigned}
const EXPONENT_SIZE: i32 = 8i32
const MANTISSA_SIZE: i32 = 7i32
const EXPONENT_BIAS: i32 = 134i32
const DENORMAL_EXPONENT: i32 = -133i32
const MAX_EXPONENT: i32 = 121i32
type Unsigned = u16
Source§impl Float for f16
Available on crate feature f16
only.
impl Float for f16
f16
only.