fhex
fhex is a Rust crate that provides the ToHex trait for converting floating-point numbers to their hexadecimal representation. The trait is implemented for f32 and f64 types.
The hexadecimal representation follows the format specified for the IEEE 754 standard, ±0xh.hhhp±d, described below.
To parse hexadecimal floating point numbers, see the hexf crate.
Usage
Add fhex to your Cargo.toml dependencies:
[]
= "1.0.0"
Then, import the ToHex trait in your code:
use ToHex;
You can now call the to_hex method on any f32 or f64 value:
let num: f32 = 1.23;
let hex = num.to_hex;
println!;
This will print the hexadecimal representation of 1.23.
0x1.3ae148p+0
Format
Floating point numbers are represented in the format: ±0xh.hhhp±d, where:
±is the sign of the number, although+is omitted for positive numbers.0xis the prefix for hexadecimal numbers.h.hhhis the significand (also known as the mantissa), represented as a hexadecimal number.pindicates that the following number is the exponent.±dis the exponent, represented as a decimal number.
Special cases:
±0x0p+0is used to represent zero.±infis used to represent infinity.nanis used to represent NaN (a "quiet NaN").nan:0xfollowed by a hexadecimal number is used to represent a NaN with a payload (a "signalling NaN").
Known differences with other implementations
- Go's
%xformat specifier for floating point numbers uses the format±0xh.hhhp±d, but it insists on printing the exponent with at least two digits, zero padded. Infinity is represented as±Infand NaN asNaN. There is no special treatment of signalling NaNs. - C++'s
std::hexfloatformat specifier for floating point numbers uses the format±0xh.hhhp±d. There is no special treatment of signalling NaNs, they are justnan.
License
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.