pub struct F32(/* private fields */);
Expand description
A 32-bit IBM floating point number.
This type supports the conversions:
- Transmuting to/from a
u32
viafrom_bits()
,to_bits()
- Transmuting to/from a big-endian
[u8; 4]
viafrom_be_bytes()
/to_be_bytes()
- Lossily converting to an
f32
viaFrom
/Into
- Losslessly converting to an
f64
viaFrom
/Into
IBM F32
floats have slightly less precision than IEEE-754 f32
floats, but it covers a
slightly larger domain. F32
s of typical magnitude can be converted to f32
without rounding
or other loss of precision. Converting F32
s of large magnitude to f32
will cause rounding;
F32
s 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
impl F32
Sourcepub fn from_bits(value: u32) -> Self
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);
Sourcepub fn to_bits(self) -> u32
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);
Sourcepub fn from_be_bytes(bytes: [u8; 4]) -> Self
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);
Sourcepub fn to_be_bytes(self) -> [u8; 4]
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]);