Skip to main content

f16

Primitive Type f16 

🔬This is a nightly-only experimental API. (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.

See also the std::f16::consts module.

Implementations§

Source§

impl f16

Source

pub const RADIX: u32 = 2

🔬This is a nightly-only experimental API. (f16 #116909)

The radix or base of the internal representation of f16.

Source

pub const MANTISSA_DIGITS: u32 = 11

🔬This is a nightly-only experimental API. (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.

Source

pub const DIGITS: u32 = 3

🔬This is a nightly-only experimental API. (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).

Source

pub const EPSILON: f16 = 9.7656e-4_f16

🔬This is a nightly-only experimental API. (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.

Source

pub const MIN: f16 = -6.5504e+4_f16

🔬This is a nightly-only experimental API. (f16 #116909)

Smallest finite f16 value.

Equal to −MAX.

Source

pub const MIN_POSITIVE: f16 = 6.1035e-5_f16

🔬This is a nightly-only experimental API. (f16 #116909)

Smallest positive normal f16 value.

Equal to 2MIN_EXP − 1.

Source

pub const MAX: f16 = 6.5504e+4_f16

🔬This is a nightly-only experimental API. (f16 #116909)

Largest finite f16 value.

Equal to (1 − 2MANTISSA_DIGITS) 2MAX_EXP.

Source

pub const MIN_EXP: i32 = -13

🔬This is a nightly-only experimental API. (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.

Source

pub const MAX_EXP: i32 = 16

🔬This is a nightly-only experimental API. (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.

Source

pub const MIN_10_EXP: i32 = -4

🔬This is a nightly-only experimental API. (f16 #116909)

Minimum x for which 10x is normal.

Equal to ceil(log10 MIN_POSITIVE).

Source

pub const MAX_10_EXP: i32 = 4

🔬This is a nightly-only experimental API. (f16 #116909)

Maximum x for which 10x is normal.

Equal to floor(log10 MAX).

Source

pub const NAN: f16

🔬This is a nightly-only experimental API. (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.

Source

pub const INFINITY: f16

🔬This is a nightly-only experimental API. (f16 #116909)

Infinity (∞).

Source

pub const NEG_INFINITY: f16

🔬This is a nightly-only experimental API. (f16 #116909)

Negative infinity (−∞).

Source

pub const fn classify(self) -> FpCategory

🔬This is a nightly-only experimental API. (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.

#![feature(f16)]

use std::num::FpCategory;

let num = 12.4_f16;
let inf = f16::INFINITY;

assert_eq!(num.classify(), FpCategory::Normal);
assert_eq!(inf.classify(), FpCategory::Infinite);
Source

pub const fn to_bits(self) -> u16

🔬This is a nightly-only experimental API. (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.

#![feature(f16)]

assert_ne!((1f16).to_bits(), 1f16 as u16); // to_bits() is not casting!
assert_eq!((12.5f16).to_bits(), 0x4a40);
Source

pub const fn from_bits(v: u16) -> Self

🔬This is a nightly-only experimental API. (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.

#![feature(f16)]

let v = f16::from_bits(0x4a40);
assert_eq!(v, 12.5);
Source

pub const fn abs(self) -> Self

🔬This is a nightly-only experimental API. (f16 #116909)

Computes the absolute value of self.

This function always returns the precise result.

§Examples
#![feature(f16)]

let x = 3.5_f16;
let y = -3.5_f16;

assert_eq!(x.abs(), x);
assert_eq!(y.abs(), -y);

assert!(f16::NAN.abs().is_nan());

Trait Implementations§

1.0.0 (const: unstable) · Source§

impl Add<&f16> for &f16

Source§

type Output = <f16 as Add>::Output

The resulting type after applying the + operator.
Source§

fn add(self, other: &f16) -> <f16 as Add<f16>>::Output

Performs the + operation. Read more
1.0.0 (const: unstable) · Source§

impl Add<&f16> for f16

Source§

type Output = <f16 as Add>::Output

The resulting type after applying the + operator.
Source§

fn add(self, other: &f16) -> <f16 as Add<f16>>::Output

Performs the + operation. Read more
1.0.0 (const: unstable) · Source§

impl Add<f16> for &f16

Source§

type Output = <f16 as Add>::Output

The resulting type after applying the + operator.
Source§

fn add(self, other: f16) -> <f16 as Add<f16>>::Output

Performs the + operation. Read more
1.0.0 (const: unstable) · Source§

impl Add for f16

Source§

type Output = f16

The resulting type after applying the + operator.
Source§

fn add(self, other: f16) -> f16

Performs the + operation. Read more
1.22.0 (const: unstable) · Source§

impl AddAssign<&f16> for f16

Source§

fn add_assign(&mut self, other: &f16)

Performs the += operation. Read more
1.8.0 (const: unstable) · Source§

impl AddAssign for f16

Source§

fn add_assign(&mut self, other: f16)

Performs the += operation. Read more
1.0.0 (const: unstable) · Source§

impl Clone for f16

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)
where Self:,

Performs copy-assignment from source. Read more
1.0.0 · Source§

impl Debug for f16

Source§

fn fmt(&self, fmt: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
1.0.0 (const: unstable) · Source§

impl Default for f16

Source§

fn default() -> f16

Returns the default value of 0.0

1.0.0 · Source§

impl Display for f16

Source§

fn fmt(&self, fmt: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
1.0.0 (const: unstable) · Source§

impl Div<&f16> for &f16

Source§

type Output = <f16 as Div>::Output

The resulting type after applying the / operator.
Source§

fn div(self, other: &f16) -> <f16 as Div<f16>>::Output

Performs the / operation. Read more
1.0.0 (const: unstable) · Source§

impl Div<&f16> for f16

Source§

type Output = <f16 as Div>::Output

The resulting type after applying the / operator.
Source§

fn div(self, other: &f16) -> <f16 as Div<f16>>::Output

Performs the / operation. Read more
1.0.0 (const: unstable) · Source§

impl Div<f16> for &f16

Source§

type Output = <f16 as Div>::Output

The resulting type after applying the / operator.
Source§

fn div(self, other: f16) -> <f16 as Div<f16>>::Output

Performs the / operation. Read more
1.0.0 (const: unstable) · Source§

impl Div for f16

Source§

type Output = f16

The resulting type after applying the / operator.
Source§

fn div(self, other: f16) -> f16

Performs the / operation. Read more
1.22.0 (const: unstable) · Source§

impl DivAssign<&f16> for f16

Source§

fn div_assign(&mut self, other: &f16)

Performs the /= operation. Read more
1.8.0 (const: unstable) · Source§

impl DivAssign for f16

Source§

fn div_assign(&mut self, other: f16)

Performs the /= operation. Read more
1.0.0 · Source§

impl LowerExp for f16

Source§

fn fmt(&self, fmt: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
1.0.0 (const: unstable) · Source§

impl Mul<&f16> for &f16

Source§

type Output = <f16 as Mul>::Output

The resulting type after applying the * operator.
Source§

fn mul(self, other: &f16) -> <f16 as Mul<f16>>::Output

Performs the * operation. Read more
1.0.0 (const: unstable) · Source§

impl Mul<&f16> for f16

Source§

type Output = <f16 as Mul>::Output

The resulting type after applying the * operator.
Source§

fn mul(self, other: &f16) -> <f16 as Mul<f16>>::Output

Performs the * operation. Read more
1.0.0 (const: unstable) · Source§

impl Mul<f16> for &f16

Source§

type Output = <f16 as Mul>::Output

The resulting type after applying the * operator.
Source§

fn mul(self, other: f16) -> <f16 as Mul<f16>>::Output

Performs the * operation. Read more
1.0.0 (const: unstable) · Source§

impl Mul for f16

Source§

type Output = f16

The resulting type after applying the * operator.
Source§

fn mul(self, other: f16) -> f16

Performs the * operation. Read more
1.22.0 (const: unstable) · Source§

impl MulAssign<&f16> for f16

Source§

fn mul_assign(&mut self, other: &f16)

Performs the *= operation. Read more
1.8.0 (const: unstable) · Source§

impl MulAssign for f16

Source§

fn mul_assign(&mut self, other: f16)

Performs the *= operation. Read more
1.0.0 (const: unstable) · Source§

impl Neg for &f16

Source§

type Output = <f16 as Neg>::Output

The resulting type after applying the - operator.
Source§

fn neg(self) -> <f16 as Neg>::Output

Performs the unary - operation. Read more
1.0.0 (const: unstable) · Source§

impl Neg for f16

Source§

type Output = f16

The resulting type after applying the - operator.
Source§

fn neg(self) -> f16

Performs the unary - operation. Read more
1.0.0 (const: unstable) · Source§

impl PartialEq for f16

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
Source§

fn ne(&self, other: &Self) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
1.0.0 (const: unstable) · Source§

impl PartialOrd for f16

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
Source§

fn lt(&self, other: &Self) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
Source§

fn le(&self, other: &Self) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
Source§

fn gt(&self, other: &Self) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
Source§

fn ge(&self, other: &Self) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
1.0.0 (const: unstable) · Source§

impl Rem<&f16> for &f16

Source§

type Output = <f16 as Rem>::Output

The resulting type after applying the % operator.
Source§

fn rem(self, other: &f16) -> <f16 as Rem<f16>>::Output

Performs the % operation. Read more
1.0.0 (const: unstable) · Source§

impl Rem<&f16> for f16

Source§

type Output = <f16 as Rem>::Output

The resulting type after applying the % operator.
Source§

fn rem(self, other: &f16) -> <f16 as Rem<f16>>::Output

Performs the % operation. Read more
1.0.0 (const: unstable) · Source§

impl Rem<f16> for &f16

Source§

type Output = <f16 as Rem>::Output

The resulting type after applying the % operator.
Source§

fn rem(self, other: f16) -> <f16 as Rem<f16>>::Output

Performs the % operation. Read more
1.0.0 (const: unstable) · Source§

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

let x: f32 = 50.50;
let y: f32 = 8.125;
let remainder = x - (x / y).trunc() * y;

// The answer to both operations is 1.75
assert_eq!(x % y, remainder);
Source§

type Output = f16

The resulting type after applying the % operator.
Source§

fn rem(self, other: f16) -> f16

Performs the % operation. Read more
1.22.0 (const: unstable) · Source§

impl RemAssign<&f16> for f16

Source§

fn rem_assign(&mut self, other: &f16)

Performs the %= operation. Read more
1.8.0 (const: unstable) · Source§

impl RemAssign for f16

Source§

fn rem_assign(&mut self, other: f16)

Performs the %= operation. Read more
1.0.0 (const: unstable) · Source§

impl Sub<&f16> for &f16

Source§

type Output = <f16 as Sub>::Output

The resulting type after applying the - operator.
Source§

fn sub(self, other: &f16) -> <f16 as Sub<f16>>::Output

Performs the - operation. Read more
1.0.0 (const: unstable) · Source§

impl Sub<&f16> for f16

Source§

type Output = <f16 as Sub>::Output

The resulting type after applying the - operator.
Source§

fn sub(self, other: &f16) -> <f16 as Sub<f16>>::Output

Performs the - operation. Read more
1.0.0 (const: unstable) · Source§

impl Sub<f16> for &f16

Source§

type Output = <f16 as Sub>::Output

The resulting type after applying the - operator.
Source§

fn sub(self, other: f16) -> <f16 as Sub<f16>>::Output

Performs the - operation. Read more
1.0.0 (const: unstable) · Source§

impl Sub for f16

Source§

type Output = f16

The resulting type after applying the - operator.
Source§

fn sub(self, other: f16) -> f16

Performs the - operation. Read more
1.22.0 (const: unstable) · Source§

impl SubAssign<&f16> for f16

Source§

fn sub_assign(&mut self, other: &f16)

Performs the -= operation. Read more
1.8.0 (const: unstable) · Source§

impl SubAssign for f16

Source§

fn sub_assign(&mut self, other: f16)

Performs the -= operation. Read more
1.12.0 · Source§

impl<'a> Sum<&'a f16> for f16

Source§

fn sum<I: Iterator<Item = &'a Self>>(iter: I) -> Self

Takes an iterator and generates Self from the elements by “summing up” the items.
1.12.0 · Source§

impl Sum for f16

Source§

fn sum<I: Iterator<Item = Self>>(iter: I) -> Self

Takes an iterator and generates Self from the elements by “summing up” the items.
1.0.0 · Source§

impl UpperExp for f16

Source§

fn fmt(&self, fmt: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
1.0.0 · Source§

impl Copy for f16

Auto Trait Implementations§

§

impl Freeze for f16

§

impl Send for f16

§

impl Sync for f16

§

impl Unpin for f16

Blanket Implementations§

Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.