-
Notifications
You must be signed in to change notification settings - Fork 20
ACP: hex formatting and parsing for floats #536
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
in order to match |
Good point, I removed that bit. We should probably still parse it since 754 says On that note we only print |
How will subnormals be printed, if not via a leading |
+1 for adding this. This seems like a good solution for handling hex floats in general.
We could accept |
The input is always shifted to normalize and the exponent is just adjusted, so e.g. the minimum f64 subnormal will print as Both seem to be in use other places. I proposed the normalized form since it at a quick glance I can still read ~2^-1074 (vs. needing to count the zeros in the C version) and, if it matters, know it's subnormal based on the exponent. But I'm not tied to it, more literally matching the bitwise representation can be nice other times. 🤷♂ |
I didn't do this originally because of consistency with |
Argh, and |
@tgross35 Sigh. It seems like the right solution, though, so one question is whether we would have const traits before trying to stabilize this. (At least, enough of const traits that we can expose it, even if const traits themselves aren't stable yet.) |
Agreed, First thing the implementation does anyway is call |
Suggestion: Let's have two functions, one with a more ideal name (e.g. |
We discussed this in today's @rust-lang/libs-api meeting. We're going to go ahead and approve the We'll consider the |
Also, a point someone raised in the libs-api meeting: if T-lang decides to accept float literals, the syntax would need to match. (If T-lang decides to not accept float literals, perhaps because this method suffices, then that won't be a concern.) |
Formatting was accepted as well, right? I am assuming so but that will need to go a separate FCP path.
Hi lang team member :) What is the best way to get a vibe check from the rest of the team as to whether this is worth pursuing? I would very much appreciate having them and there are a lot of threads scattered around discussing it, but I don't think there was ever any more concrete feedback. Feasibility of syntax was one possible blocker, I'll try to figure that out on Zulip. (edit: https://rust-lang.zulipchat.com/#narrow/channel/131828-t-compiler/topic/Parsing.20hex.20float.20literals) |
If we had hex float literals, it might be reasonable to warn by default for excess precision in those, but ...
... it doesn't feel right for parsing. The value should be rounded to the target type as usual. For one use case where that matters, you could be formatting With decimal formatting that does not hold, since the printed value is rounded in a way that preserves the value only as long as it is parsed back to the same precision:
|
This is something I meant to bring up as a discussion point. But that reasoning makes sense, +1 from me. |
Typically the answer would be: open an issue describing the question, and nominate it for t-lang. |
I'll close this since the ACP was accepted. We'll probably smooth out our implementation in libm first since that's easy, then just copy the result over rust-lang/compiler-builtins#851. |
Proposal
Problem statement
Rust's floats do not have IEE hex float formatting (C's
%a
), e.g. "-0x1.921fb6p+1".Motivating examples or use cases
Rust's decimals do roundtrip exactly, unlike C. However, there are a few other reasons to support the hex format:
const
, which allowslet x = const { f64::from_hex_str("0x1.23p+42") }
in lieu of const literals.Solution sketch
Implement
LowerHex
andUpperHex
forf16
,f32
,f64
, andf128
to provide the output. Additionally, introduce a method to parse the above:The format is specified by IEEE:
Proposed Rust-specific rules:
1.
if nonzero. 754 allows any hex sequence before the.
(e.g. Julia uses a single hex digit of any value, some implementations use0.
to indicate subnormals); we should accept this but not produce it.abcdef
/ABCDEF
,p
/P
is determined by:x
or:X
0x
is always reproduced regardless of the:#
format parameter:+
format should workAlternatives
&[u8]
instead, to avoid needless conversion to&str
(this is an existing problem forint::from_str_radix
). Edit: changed toAsRef
based on Josh's comment ACP: hex formatting and parsing for floats #536 (comment).Links and related work
libm
https://github.com/rust-lang/libm/blob/670f8a87373aa00a5e341cc167d6ad181b15ba38/src/math/support/hex_float.rshexf
provides parsinghexfloat2
provides printingWhat happens now?
This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.
Possible responses
The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):
Second, if there's a concrete solution:
The text was updated successfully, but these errors were encountered: