ibmfloat
A Rust library for IBM floating point numbers, specifically focused on converting them to IEEE-754 floating point values.
This library has no C dependencies or unsafe
code.
The conversion processes and much of the test suite are derived from the
Python ibm2ieee
library.
ibmfloat::F32
represents a 32-bit IBM floating point number. It supports the conversions:
u32
via from_bits()
, to_bits()
[u8; 4]
via from_be_bytes()
/to_be_bytes()
f32
via From
/Into
f64
via From
/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
.
```rust // Use the example -118.625: // https://en.wikipedia.org/wiki/IBMhexadecimalfloatingpoint#Example let foreignfloat = ibmfloat::F32::frombits(0b11000010011101101010000000000000);
let nativefloat = f32::from(foreignfloat); asserteq!(nativefloat, -118.625f32);
let nativefloat: f32 = foreignfloat.into(); asserteq!(nativefloat, -118.625f32); ```
ibmfloat::64
represents a 64-bit IBM floating point number. It supports the conversions:
u64
via from_bits()
, to_bits()
[u8; 8]
via from_be_bytes()
/to_be_bytes()
f32
via From
/Into
f64
via From
/Into
IBM F64
floats have slightly more precision than IEEE-754 f64
floats, but they cover a slightly smaller domain. Most
conversions will require rounding, but there is no risk of overflow or underflow.
```rust let foreignfloat = ibmfloat::F64::frombits(0x4110000000000000);
let nativefloat = f64::from(foreignfloat); asserteq!(nativefloat, 1.0f64);
let nativefloat: f64 = foreignfloat.into(); asserteq!(nativefloat, 1.0f64); ```
Please use cargo test
, cargo clippy
, and rustfmt
as you go.
cargo fuzz
covers each of the four IBM to IEEE conversion paths, comparing
them to ibm2ieee.c
's output. Please run them as needed if you tinker with that logic.
console
$ cargo +nightly fuzz run ibm32ieee32
$ cargo +nightly fuzz run ibm32ieee64
$ cargo +nightly fuzz run ibm64ieee32
$ cargo +nightly fuzz run ibm64ieee64