Skip to main content

Float16

Struct Float16 

Source
pub struct Float16(/* private fields */);
Expand description

IEEE 754 binary16 (half-precision) floating-point type.

This is a 16-bit floating-point format with:

  • 1 sign bit
  • 5 exponent bits (bias = 15)
  • 10 mantissa bits (with implicit leading 1 for normalized values)

Special values:

  • ±0: exponent = 0, mantissa = 0
  • ±Inf: exponent = 31, mantissa = 0
  • NaN: exponent = 31, mantissa ≠ 0
  • Subnormals: exponent = 0, mantissa ≠ 0

Implementations§

Source§

impl float16

Source

pub const ZERO: float16

Positive zero (+0.0).

Source

pub const NEG_ZERO: float16

Negative zero (-0.0).

Source

pub const INFINITY: float16

Positive infinity.

Source

pub const NEG_INFINITY: float16

Negative infinity.

Source

pub const NAN: float16

Quiet NaN (canonical).

Source

pub const MAX: float16

Maximum finite value (65504.0).

Source

pub const MIN_POSITIVE: float16

Minimum positive normal value (2^-14 ≈ 6.104e-5).

Source

pub const MIN_POSITIVE_SUBNORMAL: float16

Minimum positive subnormal value (2^-24 ≈ 5.96e-8).

Source

pub const fn from_bits(bits: u16) -> float16

Create a float16 from raw bits.

This is a const function that performs no validation.

Source

pub const fn to_bits(self) -> u16

Extract the raw bit representation.

Source

pub fn from_f32(value: f32) -> float16

Convert f32 to float16 using IEEE 754 round-to-nearest, ties-to-even.

Handles:

  • NaN → NaN (preserves payload bits where possible, ensures quiet NaN)
  • ±Inf → ±Inf
  • ±0 → ±0 (preserves sign)
  • Overflow → ±Inf
  • Underflow → subnormal or ±0
  • Normal values → rounded to nearest representable value
Source

pub fn to_f32(self) -> f32

Convert float16 to f32 (exact conversion).

All float16 values are exactly representable in f32.

Source

pub fn is_nan(self) -> bool

Returns true if this value is NaN.

Source

pub fn is_infinite(self) -> bool

Returns true if this value is positive or negative infinity.

Source

pub fn is_finite(self) -> bool

Returns true if this value is finite (not NaN or infinity).

Source

pub fn is_normal(self) -> bool

Returns true if this value is a normal number (not zero, subnormal, infinite, or NaN).

Source

pub fn is_subnormal(self) -> bool

Returns true if this value is subnormal.

Source

pub fn is_zero(self) -> bool

Returns true if this value is ±0.

Source

pub fn is_sign_negative(self) -> bool

Returns true if the sign bit is set (negative).

Source

pub fn is_sign_positive(self) -> bool

Returns true if the sign bit is not set (positive).

Source

pub fn eq_value(self, other: float16) -> bool

IEEE-754 numeric equality: NaN != NaN, +0 == -0.

Source

pub fn partial_cmp_value(self, other: float16) -> Option<Ordering>

IEEE-754 partial comparison: returns None if either value is NaN.

Source

pub fn total_cmp(self, other: float16) -> Ordering

Total ordering comparison (including NaN).

This matches the behavior of f32::total_cmp.

Source

pub fn add(self, rhs: float16) -> float16

Add two float16 values (via f32).

Source

pub fn sub(self, rhs: float16) -> float16

Subtract two float16 values (via f32).

Source

pub fn mul(self, rhs: float16) -> float16

Multiply two float16 values (via f32).

Source

pub fn div(self, rhs: float16) -> float16

Divide two float16 values (via f32).

Source

pub fn neg(self) -> float16

Negate this float16 value.

Source

pub fn abs(self) -> float16

Absolute value.

Trait Implementations§

Source§

impl Add for float16

Source§

type Output = float16

The resulting type after applying the + operator.
Source§

fn add(self, rhs: float16) -> float16

Performs the + operation. Read more
Source§

impl Clone for float16

Source§

fn clone(&self) -> float16

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Copy for float16

Source§

impl Debug for float16

Source§

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

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

impl Default for float16

Source§

fn default() -> float16

Returns the “default value” for a type. Read more
Source§

impl Display for float16

Source§

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

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

impl Div for float16

Source§

type Output = float16

The resulting type after applying the / operator.
Source§

fn div(self, rhs: float16) -> float16

Performs the / operation. Read more
Source§

impl Eq for float16

Source§

impl Hash for float16

Source§

fn hash<H>(&self, state: &mut H)
where H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Mul for float16

Source§

type Output = float16

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: float16) -> float16

Performs the * operation. Read more
Source§

impl Neg for float16

Source§

type Output = float16

The resulting type after applying the - operator.
Source§

fn neg(self) -> float16

Performs the unary - operation. Read more
Source§

impl PartialEq for float16

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl PartialOrd for float16

Source§

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

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

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

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

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

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

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

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

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

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

impl Serializer for float16

Source§

type Target = float16

Source§

fn write_data( value: &float16, context: &mut WriteContext<'_>, ) -> Result<(), Error>

Write the target body only.
Source§

fn read_data(context: &mut ReadContext<'_>) -> Result<float16, Error>

Read the target body only.
Source§

fn default_value(_: &mut ReadContext<'_>) -> Result<float16, Error>

Construct a target for a null or missing local value.
Source§

fn read_arc_any( context: &mut ReadContext<'_>, ) -> Result<Arc<dyn Any + Send + Sync>, Error>

Read directly into the final owner for sync dynamic carriers.
Source§

impl Sub for float16

Source§

type Output = float16

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: float16) -> float16

Performs the - operation. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> ForyObject for T
where T: Any,

Source§

fn as_any(&self) -> &(dyn Any + 'static)

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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.