Struct F32

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

A 32-bit IBM floating point number.

This type supports the conversions:

  • Transmuting to/from a u32 via from_bits(), to_bits()
  • Transmuting to/from a big-endian [u8; 4] via from_be_bytes()/to_be_bytes()
  • Lossily converting to an f32 via From/Into
  • Losslessly converting to an f64 via From/Into

IBM F32 floats have slightly less precision than IEEE-754 f32 floats, but it covers a slightly larger domain. F32s of typical magnitude can be converted to f32 without rounding or other loss of precision. Converting F32s of large magnitude to f32 will cause rounding; F32s of extreme magnitude can also cause overflow and underflow to occur.

Every F32 can be precisely represented as an f64, without rounding, overflow, or underflow. Those seeking a lossless path to IEEE-754 should convert F32 to f64.

// Use the example -118.625:
//   https://en.wikipedia.org/wiki/IBM_hexadecimal_floating_point#Example
let foreign_float = ibmfloat::F32::from_bits(0b1_1000010_0111_0110_1010_0000_0000_0000);

let native_float = f32::from(foreign_float);
assert_eq!(native_float, -118.625f32);

let native_float: f32 = foreign_float.into();
assert_eq!(native_float, -118.625f32);

Implementations§

Source§

impl F32

Source

pub fn from_bits(value: u32) -> Self

Transmute a native-endian u64 into an F64.

let foreign_float = ibmfloat::F32::from_bits(0x46000001);

let native_float = f32::from(foreign_float); // potential loss of precision
assert_eq!(native_float, 1.0f32);

let native_float = f64::from(foreign_float); // always exact
assert_eq!(native_float, 1.0f64);
Source

pub fn to_bits(self) -> u32

Transmute this F32 to a native-endian u32.

let foreign_float = ibmfloat::F32::from_bits(0x46000001);

assert_eq!(foreign_float.to_bits(), 0x46000001);
Source

pub fn from_be_bytes(bytes: [u8; 4]) -> Self

Create a floating point value from its representation as a byte array in big endian.

let foreign_float = ibmfloat::F32::from_be_bytes([0x46, 0, 0, 1]);

assert_eq!(foreign_float.to_bits(), 0x46000001);

let native_float = f32::from(foreign_float);
assert_eq!(native_float, 1.0f32);
Source

pub fn to_be_bytes(self) -> [u8; 4]

Return the memory representation of this floating point number as a byte array in big-endian (network) byte order.

let foreign_float = ibmfloat::F32::from_bits(0x46000001);

assert_eq!(foreign_float.to_be_bytes(), [0x46, 0, 0, 1]);

Trait Implementations§

Source§

impl Clone for F32

Source§

fn clone(&self) -> F32

Returns a copy 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 F32

Source§

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

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

impl From<F32> for f32

Source§

fn from(v: F32) -> Self

Converts to this type from the input type.
Source§

impl From<F32> for f64

Source§

fn from(v: F32) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for F32

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 F32

Source§

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

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · 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 · 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 · 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 · 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 Copy for F32

Auto Trait Implementations§

§

impl Freeze for F32

§

impl RefUnwindSafe for F32

§

impl Send for F32

§

impl Sync for F32

§

impl Unpin for F32

§

impl UnwindSafe for F32

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