[][src]Struct ibmfloat::F32

#[repr(transparent)]
pub struct F32(_);

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);

Methods

impl F32[src]

pub fn from_bits(value: u32) -> Self[src]

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);

pub fn to_bits(self) -> u32[src]

Transmute this F32 to a native-endian u32.

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

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

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

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);

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

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

impl Clone for F32[src]

impl Copy for F32[src]

impl Debug for F32[src]

impl From<F32> for f32[src]

impl From<F32> for f64[src]

impl PartialEq<F32> for F32[src]

impl PartialOrd<F32> for F32[src]

Auto Trait Implementations

impl RefUnwindSafe for F32

impl Send for F32

impl Sync for F32

impl Unpin for F32

impl UnwindSafe for F32

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.