F8E4M3

Struct F8E4M3 

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

Eight bit floating point type with 4-bit exponent and 3-bit mantissa.

Implementations§

Source§

impl F8E4M3

Source

pub const fn from_bits(bits: u8) -> Self

Construct an 8-bit floating point value from the raw bits.

Source

pub const fn to_bits(&self) -> u8

Return the raw bits.

Source

pub const fn from_f64(x: f64) -> Self

Convert a f64 type into F8E4M3.

This operation is lossy.

  • If the 64-bit value is to large to fit in 8-bits, ±∞ will result.
  • NaN values are preserved.
  • 64-bit subnormal values are too tiny to be represented in 8-bits and result in ±0.
  • Exponents that underflow the minimum 8-bit exponent will result in 8-bit subnormals or ±0.
  • All other values are truncated and rounded to the nearest representable 8-bit value.
Source

pub const fn from_f32(x: f32) -> Self

Convert a f32 type into F8E4M3.

This operation is lossy.

  • If the 32-bit value is to large to fit in 8-bits, ±∞ will result.
  • NaN values are preserved.
  • 32-bit subnormal values are too tiny to be represented in 8-bits and result in ±0.
  • Exponents that underflow the minimum 8-bit exponent will result in 8-bit subnormals or ±0.
  • All other values are truncated and rounded to the nearest representable 8-bit value.
Source

pub const fn to_f16(&self) -> f16

Convert this F8E4M3 type into a f16 type.

This operation may be lossy.

  • NaN and zero values are preserved.
  • Subnormal values are normalized.
  • Otherwise, the values are mapped to the appropriate 16-bit value.
Source

pub const fn to_f32(&self) -> f32

Convert this F8E4M3 type into a f32 type.

This operation may be lossy.

  • NaN and zero values are preserved.
  • Subnormal values are normalized.
  • Otherwise, the values are mapped to the appropriate 16-bit value.
Source

pub const fn to_f64(&self) -> f64

Convert this F8E4M3 type into a f64 type.

This operation may be lossy.

  • NaN and zero values are preserved.
  • Subnormal values are normalized.
  • Otherwise, the values are mapped to the appropriate 16-bit value.
Source

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

Returns the ordering between self and other.

  • negative quiet NaN
  • negative signaling NaN
  • negative infinity
  • negative numbers
  • negative subnormal numbers
  • negative zero
  • positive zero
  • positive subnormal numbers
  • positive numbers
  • positive infinity
  • positive signaling NaN
  • positive quiet NaN.

The ordering established by this function does not always agree with the PartialOrd and PartialEq implementations. For example, they consider negative and positive zero equal, while total_cmp doesn’t.

§Example

let mut v: Vec<F8E4M3> = vec![];
v.push(F8E4M3::ONE);
v.push(F8E4M3::INFINITY);
v.push(F8E4M3::NEG_INFINITY);
v.push(F8E4M3::NAN);
v.push(F8E4M3::MAX_SUBNORMAL);
v.push(-F8E4M3::MAX_SUBNORMAL);
v.push(F8E4M3::ZERO);
v.push(F8E4M3::NEG_ZERO);
v.push(F8E4M3::NEG_ONE);
v.push(F8E4M3::MIN_POSITIVE);

v.sort_by(|a, b| a.total_cmp(&b));

assert!(v
    .into_iter()
    .zip(
        [
            F8E4M3::NEG_INFINITY,
            F8E4M3::NEG_ONE,
            -F8E4M3::MAX_SUBNORMAL,
            F8E4M3::NEG_ZERO,
            F8E4M3::ZERO,
            F8E4M3::MAX_SUBNORMAL,
            F8E4M3::MIN_POSITIVE,
            F8E4M3::ONE,
            F8E4M3::INFINITY,
            F8E4M3::NAN
        ]
        .iter()
    )
    .all(|(a, b)| a.to_bits() == b.to_bits()));
Source

pub const fn is_sign_positive(&self) -> bool

Returns true if and only if self has a positive sign, including +0.0, NaNs with a positive sign bit and +∞.

Source

pub const fn is_sign_negative(&self) -> bool

Returns true if and only if self has a negative sign, including −0.0, NaNs with a negative sign bit and −∞.

Source

pub const fn is_nan(&self) -> bool

Returns true if this value is NaN and false otherwise.

§Examples

let nan = F8E4M3::NAN;
let f = F8E4M3::from_f32(7.0_f32);

assert!(nan.is_nan());
assert!(!f.is_nan());
Source

pub const fn is_infinite(&self) -> bool

Returns true if this value is ±∞ and false otherwise.

§Examples

let f = F8E4M3::from_f32(7.0f32);
let inf = F8E4M3::INFINITY;
let neg_inf = F8E4M3::NEG_INFINITY;
let nan = F8E4M3::NAN;

assert!(!f.is_infinite());
assert!(!nan.is_infinite());

assert!(inf.is_infinite());
assert!(neg_inf.is_infinite());
Source

pub const fn is_finite(&self) -> bool

Returns true if this number is neither infinite nor NaN.

§Examples

let f = F8E4M3::from_f32(7.0f32);
let inf = F8E4M3::INFINITY;
let neg_inf = F8E4M3::NEG_INFINITY;
let nan = F8E4M3::NAN;

assert!(f.is_finite());

assert!(!nan.is_finite());
assert!(!inf.is_finite());
assert!(!neg_inf.is_finite());
Source

pub const fn is_normal(&self) -> bool

Returns true if the number is neither zero, infinite, subnormal, or NaN and false otherwise.

§Examples

let min = F8E4M3::MIN_POSITIVE;
let max = F8E4M3::MAX;
let lower_than_min = F8E4M3::from_f32(1.0e-10_f32);
let zero = F8E4M3::from_f32(0.0_f32);

assert!(min.is_normal());
assert!(max.is_normal());

assert!(!zero.is_normal());
assert!(!F8E4M3::NAN.is_normal());
assert!(!F8E4M3::INFINITY.is_normal());
// Values between `0` and `min` are Subnormal.
assert!(!lower_than_min.is_normal());
Source

pub fn min(self, other: Self) -> Self

Returns the minimum of the two numbers.

If one of the arguments is NaN, then the other argument is returned.

§Examples
let x = F8E4M3::from_f32(1.0);
let y = F8E4M3::from_f32(2.0);

assert_eq!(x.min(y), x);
Source

pub fn max(self, other: Self) -> Self

Returns the minimum of the two numbers.

If one of the arguments is NaN, then the other argument is returned.

§Examples
let x = F8E4M3::from_f32(1.0);
let y = F8E4M3::from_f32(2.0);

assert_eq!(x.min(y), x);
Source

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
assert!(F8E4M3::from_f32(-3.0).clamp(F8E4M3::from_f32(-2.0), F8E4M3::from_f32(1.0)) == F8E4M3::from_f32(-2.0));
assert!(F8E4M3::from_f32(0.0).clamp(F8E4M3::from_f32(-2.0), F8E4M3::from_f32(1.0)) == F8E4M3::from_f32(0.0));
assert!(F8E4M3::from_f32(2.0).clamp(F8E4M3::from_f32(-2.0), F8E4M3::from_f32(1.0)) == F8E4M3::from_f32(1.0));
assert!(F8E4M3::NAN.clamp(F8E4M3::from_f32(-2.0), F8E4M3::from_f32(1.0)).is_nan());
Source

pub const fn copysign(self, sign: Self) -> Self

Returns a number composed of the magnitude of self and the sign of sign.

Equal to self if the sign of self and sign are the same, otherwise equal to -self. If self is NaN, then NaN with the sign of sign is returned.

§Examples
let f = F8E4M3::from_f32(3.5);

assert_eq!(f.copysign(F8E4M3::from_f32(0.42)), F8E4M3::from_f32(3.5));
assert_eq!(f.copysign(F8E4M3::from_f32(-0.42)), F8E4M3::from_f32(-3.5));
assert_eq!((-f).copysign(F8E4M3::from_f32(0.42)), F8E4M3::from_f32(3.5));
assert_eq!((-f).copysign(F8E4M3::from_f32(-0.42)), F8E4M3::from_f32(-3.5));

assert!(F8E4M3::NAN.copysign(F8E4M3::from_f32(1.0)).is_nan());
Source

pub const fn signum(self) -> Self

Returns a number that represents the sign of self.

  • 1.0 if the number is positive, +0.0 or INFINITY
  • -1.0 if the number is negative, -0.0 or NEG_INFINITY
  • NAN if the number is NaN
§Examples

let f = F8E4M3::from_f32(3.5_f32);

assert_eq!(f.signum(), F8E4M3::from_f32(1.0));
assert_eq!(F8E4M3::NEG_INFINITY.signum(), F8E4M3::from_f32(-1.0));

assert!(F8E4M3::NAN.signum().is_nan());
Source

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.

§Examples
use std::num::FpCategory;

let num = F8E4M3::from_f32(12.4_f32);
let inf = F8E4M3::INFINITY;

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

impl F8E4M3

Source

pub const PI: Self

π

Source

pub const TAU: Self

The full circle constant (τ)

Equal to 2π.

Source

pub const FRAC_PI_2: Self

π/2

Source

pub const FRAC_PI_3: Self

π/3

Source

pub const FRAC_PI_4: Self

π/4

Source

pub const FRAC_PI_6: Self

π/6

Source

pub const FRAC_PI_8: Self

π/8

Source

pub const FRAC_1_PI: Self

1/π

Source

pub const FRAC_2_PI: Self

2/π

Source

pub const FRAC_2_SQRT_PI: Self

2/sqrt(π)

Source

pub const SQRT_2: Self

sqrt(2)

Source

pub const FRAC_1_SQRT_2: Self

1/sqrt(2)

Source

pub const E: Self

Euler’s number (e)

Source

pub const LOG2_10: Self

log2(10)

Source

pub const LOG2_E: Self

log2(e)

Source

pub const LOG10_2: Self

log10(2)

Source

pub const LOG10_E: Self

log10(e)

Source

pub const LN_2: Self

ln(2)

Source

pub const LN_10: Self

ln(10)

Source§

impl F8E4M3

Source

pub const MANTISSA_DIGITS: u32 = 3u32

Number of mantissa digits

Source

pub const MAX: Self

Maximum possible value

Source

pub const MIN: Self

Minimum possible value

Source

pub const INFINITY: Self

Positive infinity ∞

Source

pub const NEG_INFINITY: Self

Negative infinity -∞

Source

pub const MIN_POSITIVE: Self

Smallest possible normal value

Source

pub const MIN_POSITIVE_SUBNORMAL: Self

Smallest possible subnormal value

Source

pub const MAX_SUBNORMAL: Self

Smallest possible subnormal value

Source

pub const EPSILON: Self

This is the difference between 1.0 and the next largest representable number.

Source

pub const NAN: Self

NaN value

Source

pub const ONE: Self

1

Source

pub const ZERO: Self

0

Source

pub const NEG_ONE: Self

-1

Source

pub const NEG_ZERO: Self

-0

Source

pub const MIN_EXP: i32 = -5i32

One greater than the minimum possible normal power of 2 exponent

Source

pub const MIN_10_EXP: i32 = -1i32

Minimum possible normal power of 10 exponent

Source

pub const MAX_EXP: i32 = 7i32

Maximum possible normal power of 2 exponent

Source

pub const MAX_10_EXP: i32 = 2i32

Maximum possible normal power of 10 exponent

Source

pub const DIGITS: u32 = 0u32

Approximate number of significant digits in base 10

Trait Implementations§

Source§

impl Add for F8E4M3

Source§

type Output = F8E4M3

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Self) -> Self::Output

Performs the + operation. Read more
Source§

impl AddAssign for F8E4M3

Source§

fn add_assign(&mut self, rhs: Self)

Performs the += operation. Read more
Source§

impl Clone for F8E4M3

Source§

fn clone(&self) -> F8E4M3

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

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

Performs copy-assignment from source. Read more
Source§

impl Debug for F8E4M3

Source§

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

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

impl Default for F8E4M3

Source§

fn default() -> F8E4M3

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

impl Display for F8E4M3

Source§

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

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

impl Div for F8E4M3

Source§

type Output = F8E4M3

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Self) -> Self::Output

Performs the / operation. Read more
Source§

impl DivAssign for F8E4M3

Source§

fn div_assign(&mut self, rhs: Self)

Performs the /= operation. Read more
Source§

impl From<F8E4M3> for f16

Source§

fn from(value: F8E4M3) -> Self

Converts to this type from the input type.
Source§

impl From<F8E4M3> for f32

Source§

fn from(value: F8E4M3) -> Self

Converts to this type from the input type.
Source§

impl From<F8E4M3> for f64

Source§

fn from(value: F8E4M3) -> Self

Converts to this type from the input type.
Source§

impl From<f16> for F8E4M3

Source§

fn from(x: f16) -> F8E4M3

Converts to this type from the input type.
Source§

impl From<f32> for F8E4M3

Source§

fn from(x: f32) -> F8E4M3

Converts to this type from the input type.
Source§

impl From<f64> for F8E4M3

Source§

fn from(x: f64) -> F8E4M3

Converts to this type from the input type.
Source§

impl FromStr for F8E4M3

Source§

type Err = ParseFloatError

The associated error which can be returned from parsing.
Source§

fn from_str(src: &str) -> Result<F8E4M3, ParseFloatError>

Parses a string s to return a value of this type. Read more
Source§

impl LowerExp for F8E4M3

Source§

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

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

impl LowerHex for F8E4M3

Source§

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

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

impl Mul for F8E4M3

Source§

type Output = F8E4M3

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Self) -> Self::Output

Performs the * operation. Read more
Source§

impl MulAssign for F8E4M3

Source§

fn mul_assign(&mut self, rhs: Self)

Performs the *= operation. Read more
Source§

impl Neg for F8E4M3

Source§

type Output = F8E4M3

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
Source§

impl PartialEq for F8E4M3

Source§

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

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

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for F8E4M3

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
Source§

impl Rem for F8E4M3

Source§

type Output = F8E4M3

The resulting type after applying the % operator.
Source§

fn rem(self, rhs: Self) -> Self::Output

Performs the % operation. Read more
Source§

impl RemAssign for F8E4M3

Source§

fn rem_assign(&mut self, rhs: Self)

Performs the %= operation. Read more
Source§

impl Sub for F8E4M3

Source§

type Output = F8E4M3

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Self) -> Self::Output

Performs the - operation. Read more
Source§

impl SubAssign for F8E4M3

Source§

fn sub_assign(&mut self, rhs: Self)

Performs the -= operation. Read more
Source§

impl UpperExp for F8E4M3

Source§

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

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

impl UpperHex for F8E4M3

Source§

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

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

impl Copy for F8E4M3

Auto Trait Implementations§

§

impl Freeze for F8E4M3

§

impl RefUnwindSafe for F8E4M3

§

impl Send for F8E4M3

§

impl Sync for F8E4M3

§

impl Unpin for F8E4M3

§

impl UnwindSafe for F8E4M3

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> 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 = 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.