Skip to content
This repository was archived by the owner on Apr 28, 2025. It is now read-only.

Add support for printing hex float syntax #483

Merged
merged 3 commits into from
Jan 30, 2025

Conversation

tgross35
Copy link
Contributor

Update the hf* functions to be able to parse infinities and NANs, then add a wrapper type that formats with the hex float syntax.

For now this is only being used in util, but in a followup I will somehow merge this into the existing Hex trait so this is displayed from test output.

@tgross35
Copy link
Contributor Author

Cc @quaternic, more parsing and printing

@tgross35
Copy link
Contributor Author

Looks like indicatif needs a temporary pin #484

@tgross35 tgross35 force-pushed the hex-print branch 3 times, most recently from 216dbf7 to a543e15 Compare January 28, 2025 11:53
const fn parse_any(s: &str, bits: u32, sig_bits: u32) -> u128 {
/// Parse any float from hex to its bitwise representation.
///
/// `nan_repr` is passed rather than constructed so the platform-specific NaN is returned.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it necessary to return specifically fN::NAN? Rather than, say, the quiet NaN with 0 payload.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had this for bitwise equality at some point, but I since removed that path. I'll clean this up.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed. I also moved the new struct to hex_float so it is usable for debug printing within libm.

} else {
"1."
};
let exp_sign = if exponent >= 0 { "+" } else { "" };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To always print the exponent sign, you could use the + format flag (https://doc.rust-lang.org/std/fmt/index.html#sign0)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤯 I don't think I ever knew about that option, thanks

This isn't very useful for constants since the trait constants are
available, but does enable roundtripping via hex float syntax.
@tgross35 tgross35 force-pushed the hex-print branch 3 times, most recently from ae636dd to f6b1122 Compare January 29, 2025 00:14
@tgross35 tgross35 merged commit f258331 into rust-lang:master Jan 30, 2025
35 checks passed
@tgross35 tgross35 deleted the hex-print branch January 30, 2025 05:59
tgross35 added a commit that referenced this pull request Apr 18, 2025
Add support for printing hex float syntax
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants