Crates.io | fhex |
lib.rs | fhex |
version | 1.0.0 |
source | src |
created_at | 2023-11-30 11:23:50.217118 |
updated_at | 2023-11-30 11:23:50.217118 |
description | A crate providing the ToHex trait for converting floating-point numbers to hexadecimal. |
homepage | |
repository | https://github.com/rvagg/fhex |
max_upload_size | |
id | 1054082 |
size | 31,784 |
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.
Add fhex
to your Cargo.toml
dependencies:
[dependencies]
fhex = "1.0.0"
Then, import the ToHex
trait in your code:
use fhex::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!("{}", hex);
This will print the hexadecimal representation of 1.23.
0x1.3ae148p+0
Floating point numbers are represented in the format: ±0xh.hhhp±d
, where:
±
is the sign of the number, although +
is omitted for positive numbers.0x
is the prefix for hexadecimal numbers.h.hhh
is the significand (also known as the mantissa), represented as a hexadecimal number.p
indicates that the following number is the exponent.±d
is the exponent, represented as a decimal number.Special cases:
±0x0p+0
is used to represent zero.±inf
is used to represent infinity.nan
is used to represent NaN (a "quiet NaN").nan:0x
followed by a hexadecimal number is used to represent a NaN with a payload (a "signalling NaN").%x
format 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 ±Inf
and NaN as NaN
. There is no special treatment of signalling NaNs.std::hexfloat
format specifier for floating point numbers uses the format ±0xh.hhhp±d
. There is no special treatment of signalling NaNs, they are just nan
.This project is licensed under the Apache License 2.0 - see the LICENSE file for details.