Primitive Type f16
f16 #116909)Expand description
A 16-bit floating-point type (specifically, the “binary16” type defined in IEEE 754-2008).
This type is very similar to f32 but has decreased precision because it uses half as many
bits. Please see the documentation for f32 or Wikipedia on half-precision
values for more information.
Note that most common platforms will not support f16 in hardware without enabling extra target
features, with the notable exception of Apple Silicon (also known as M1, M2, etc.) processors.
Hardware support on x86/x86-64 requires the avx512fp16 or avx10.1 features, while RISC-V requires
Zfh, and Arm/AArch64 requires FEAT_FP16. Usually the fallback implementation will be to use f32
hardware if it exists, and convert between f16 and f32 when performing math.
Implementations§
Source§impl f16
impl f16
Sourcepub const RADIX: u32 = 2
🔬This is a nightly-only experimental API. (f16 #116909)
pub const RADIX: u32 = 2
f16 #116909)The radix or base of the internal representation of f16.
Sourcepub const MANTISSA_DIGITS: u32 = 11
🔬This is a nightly-only experimental API. (f16 #116909)
pub const MANTISSA_DIGITS: u32 = 11
f16 #116909)Number of significant digits in base 2.
Note that the size of the mantissa in the bitwise representation is one smaller than this since the leading 1 is not stored explicitly.
Sourcepub const DIGITS: u32 = 3
🔬This is a nightly-only experimental API. (f16 #116909)
pub const DIGITS: u32 = 3
f16 #116909)Approximate number of significant digits in base 10.
This is the maximum x such that any decimal number with x
significant digits can be converted to f16 and back without loss.
Equal to floor(log10 2MANTISSA_DIGITS − 1).
Sourcepub const EPSILON: f16 = 9.7656e-4_f16
🔬This is a nightly-only experimental API. (f16 #116909)
pub const EPSILON: f16 = 9.7656e-4_f16
f16 #116909)Machine epsilon value for f16.
This is the difference between 1.0 and the next larger representable number.
Equal to 21 − MANTISSA_DIGITS.
Sourcepub const MIN: f16 = -6.5504e+4_f16
🔬This is a nightly-only experimental API. (f16 #116909)
pub const MIN: f16 = -6.5504e+4_f16
f16 #116909)Smallest finite f16 value.
Equal to −MAX.
Sourcepub const MIN_POSITIVE: f16 = 6.1035e-5_f16
🔬This is a nightly-only experimental API. (f16 #116909)
pub const MIN_POSITIVE: f16 = 6.1035e-5_f16
f16 #116909)Smallest positive normal f16 value.
Equal to 2MIN_EXP − 1.
Sourcepub const MAX: f16 = 6.5504e+4_f16
🔬This is a nightly-only experimental API. (f16 #116909)
pub const MAX: f16 = 6.5504e+4_f16
f16 #116909)Largest finite f16 value.
Equal to
(1 − 2−MANTISSA_DIGITS) 2MAX_EXP.
Sourcepub const MIN_EXP: i32 = -13
🔬This is a nightly-only experimental API. (f16 #116909)
pub const MIN_EXP: i32 = -13
f16 #116909)One greater than the minimum possible normal power of 2 exponent for a significand bounded by 1 ≤ x < 2 (i.e. the IEEE definition).
This corresponds to the exact minimum possible normal power of 2 exponent for a significand bounded by 0.5 ≤ x < 1 (i.e. the C definition). In other words, all normal numbers representable by this type are greater than or equal to 0.5 × 2MIN_EXP.
Sourcepub const MAX_EXP: i32 = 16
🔬This is a nightly-only experimental API. (f16 #116909)
pub const MAX_EXP: i32 = 16
f16 #116909)One greater than the maximum possible power of 2 exponent for a significand bounded by 1 ≤ x < 2 (i.e. the IEEE definition).
This corresponds to the exact maximum possible power of 2 exponent for a significand bounded by 0.5 ≤ x < 1 (i.e. the C definition). In other words, all numbers representable by this type are strictly less than 2MAX_EXP.
Sourcepub const MIN_10_EXP: i32 = -4
🔬This is a nightly-only experimental API. (f16 #116909)
pub const MIN_10_EXP: i32 = -4
f16 #116909)Minimum x for which 10x is normal.
Equal to ceil(log10 MIN_POSITIVE).
Sourcepub const MAX_10_EXP: i32 = 4
🔬This is a nightly-only experimental API. (f16 #116909)
pub const MAX_10_EXP: i32 = 4
f16 #116909)Maximum x for which 10x is normal.
Equal to floor(log10 MAX).
Sourcepub const NAN: f16
🔬This is a nightly-only experimental API. (f16 #116909)
pub const NAN: f16
f16 #116909)Not a Number (NaN).
Note that IEEE 754 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) and its sign. See the specification of NaN bit patterns for more info.
This constant is guaranteed to be a quiet NaN (on targets that follow the Rust assumptions that the quiet/signaling bit being set to 1 indicates a quiet NaN). Beyond that, nothing is guaranteed about the specific bit pattern chosen here: both payload and sign are arbitrary. The concrete bit pattern may change across Rust versions and target platforms.
Sourcepub const NEG_INFINITY: f16
🔬This is a nightly-only experimental API. (f16 #116909)
pub const NEG_INFINITY: f16
f16 #116909)Negative infinity (−∞).
Sourcepub const fn classify(self) -> FpCategory
🔬This is a nightly-only experimental API. (f16 #116909)
pub const fn classify(self) -> FpCategory
f16 #116909)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 to_bits(self) -> u16
🔬This is a nightly-only experimental API. (f16 #116909)
pub const fn to_bits(self) -> u16
f16 #116909)Raw transmutation to u16.
This is currently identical to transmute::<f16, u16>(self) on all platforms.
See from_bits for some discussion of the
portability of this operation (there are almost no issues).
Note that this function is distinct from as casting, which attempts to
preserve the numeric value, and not the bitwise value.
Sourcepub const fn from_bits(v: u16) -> Self
🔬This is a nightly-only experimental API. (f16 #116909)
pub const fn from_bits(v: u16) -> Self
f16 #116909)Raw transmutation from u16.
This is currently identical to transmute::<u16, f16>(v) on all platforms.
It turns out this is incredibly portable, for two reasons:
- Floats and Ints have the same endianness on all supported platforms.
- IEEE 754 very precisely specifies the bit layout of floats.
However there is one caveat: prior to the 2008 version of IEEE 754, how to interpret the NaN signaling bit wasn’t actually specified. Most platforms (notably x86 and ARM) picked the interpretation that was ultimately standardized in 2008, but some didn’t (notably MIPS). As a result, all signaling NaNs on MIPS are quiet NaNs on x86, and vice-versa.
Rather than trying to preserve signaling-ness cross-platform, this implementation favors preserving the exact bits. This means that any payloads encoded in NaNs will be preserved even if the result of this method is sent over the network from an x86 machine to a MIPS one.
If the results of this method are only manipulated by the same architecture that produced them, then there is no portability concern.
If the input isn’t NaN, then there is no portability concern.
If you don’t care about signalingness (very likely), then there is no portability concern.
Note that this function is distinct from as casting, which attempts to
preserve the numeric value, and not the bitwise value.
Trait Implementations§
1.22.0 (const: unstable) · Source§impl AddAssign<&f16> for f16
impl AddAssign<&f16> for f16
Source§fn add_assign(&mut self, other: &f16)
fn add_assign(&mut self, other: &f16)
+= operation. Read more1.8.0 (const: unstable) · Source§impl AddAssign for f16
impl AddAssign for f16
Source§fn add_assign(&mut self, other: f16)
fn add_assign(&mut self, other: f16)
+= operation. Read more1.22.0 (const: unstable) · Source§impl DivAssign<&f16> for f16
impl DivAssign<&f16> for f16
Source§fn div_assign(&mut self, other: &f16)
fn div_assign(&mut self, other: &f16)
/= operation. Read more1.8.0 (const: unstable) · Source§impl DivAssign for f16
impl DivAssign for f16
Source§fn div_assign(&mut self, other: f16)
fn div_assign(&mut self, other: f16)
/= operation. Read more1.22.0 (const: unstable) · Source§impl MulAssign<&f16> for f16
impl MulAssign<&f16> for f16
Source§fn mul_assign(&mut self, other: &f16)
fn mul_assign(&mut self, other: &f16)
*= operation. Read more1.8.0 (const: unstable) · Source§impl MulAssign for f16
impl MulAssign for f16
Source§fn mul_assign(&mut self, other: f16)
fn mul_assign(&mut self, other: f16)
*= operation. Read more1.0.0 (const: unstable) · Source§impl PartialOrd for f16
impl PartialOrd for f16
1.0.0 (const: unstable) · Source§impl Rem for f16
The remainder from the division of two floats.
impl Rem for f16
The remainder from the division of two floats.
The remainder has the same sign as the dividend and is computed as:
x - (x / y).trunc() * y.
§Examples
1.22.0 (const: unstable) · Source§impl RemAssign<&f16> for f16
impl RemAssign<&f16> for f16
Source§fn rem_assign(&mut self, other: &f16)
fn rem_assign(&mut self, other: &f16)
%= operation. Read more1.8.0 (const: unstable) · Source§impl RemAssign for f16
impl RemAssign for f16
Source§fn rem_assign(&mut self, other: f16)
fn rem_assign(&mut self, other: f16)
%= operation. Read more1.22.0 (const: unstable) · Source§impl SubAssign<&f16> for f16
impl SubAssign<&f16> for f16
Source§fn sub_assign(&mut self, other: &f16)
fn sub_assign(&mut self, other: &f16)
-= operation. Read more1.8.0 (const: unstable) · Source§impl SubAssign for f16
impl SubAssign for f16
Source§fn sub_assign(&mut self, other: f16)
fn sub_assign(&mut self, other: f16)
-= operation. Read more