-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Add the ability to parse/format a float/double from/to a hexadecimal literal #1630
Comments
cc: @tannergooding |
Tracked by #1387? |
Thanks for opening this, its good to see additional customer wants for these areas 😄 It is partially tracked by https://github.com/dotnet/corefx/issues/31901 (which is IEEE 754:2008 compliance) and by #1387 (which is IEEE 754:2019 compliance), but those are largely meta issues and the individual proposals will be easier to take through API review. The format specifier can be broken down into:
The terminal The computed value is So, for example if you have
|
Even though this isn't a new API, I believe we still want to take it through API review since it is modifying an existing API. We would want to check it against the compat bar and make the necessary decisions around what flags would be used to support this functionality and ensure that we wouldn't accidentally introduce any breaking changes, etc. |
I've updated the issue description to show what changes should be made. Free to discuss. |
(Sorry for the mess when I edit the title) |
Would this change also allow more combinations with |
I don't see why it wouldn't, provided it was still valid according to the IEEE requirements. |
Yeah, it makes sense, but that is out of scope - which should be parsed into signed integer, not floating point nunber - so I think it should be proposed in a separate issue, though. |
I don't believe its out of scope, as I said as long as its still valid according to the IEEE requirements, it should be fine. There are "paths" where the decimal point isn't required and so the user should fully be able to specify that
|
Oh, right. numbers like +0x23p1 can also be parsed as float... So this is OK to be valid (if users doesn't want to accept input that has But about exponent part... I saw an explanation of this being mandatory in the standard because it may create confusion like:
While it clearly only parses into 1 state: But in c(++)'s implementation this is optional:
I think when parsing, let the exponent optional is OK, because you can't parse an expression into a floating-point number.
|
Updated the description based on the discussion. |
Sorry, but anything going on? |
This hasn't made it to API review yet. Issues that are critical to .NET 5 are being reviewed first and the general backlog is then reviewed from oldest to newest. |
OK, thanks. :D |
namespace System.Globalization
{
public partial enum NumberStyles
{
HexFloat = AllowLeadingWhite | AllowTrailingWhite | AllowLeadingSign | AllowHexSpecifier | AllowDecimalPoint
}
} |
Assigning myself to this. |
@dovisutu are you interested in working on this? If not I will label up for grabs. |
Well, nope, I don't think I have spare time (or adequate abilities?) to work on this. Fine to label as up-for-grabs. |
It seems that that regex might be wrong, I found this one that seems to behave correctly:
From here: https://efxa.org/2014/05/10/regular-expressions-for-matching-data-values-in-compiler-lexers/ |
It's possible its incorrect, but the ultimate logic won't be using a RegEx anyways |
OK, is that because of performance considerations? |
Performance is certainly a factor, but the biggest reason is float/double live in System.Private.CoreLib, which can't depend on System.Text.RegularExpressions.dll |
That's quite interesting, thanks for explaining! |
Part of #27204
In IEEE 754:2008 part 5.12.3, transfering a float/double from/to an
external hexadecimal-significand character sequence representing finite number
is requested while we don't have it yet. The pattern is like this: (regex)which means:
benefits
0x0.ffp0
is the equivalent of0.99609375
while using less chars. Same for formatting as it reduces the size of string to transfer.proposal APIs
And let
Numbers.ParseDouble/Single(string s, NumberStyles style[, NumberFormatInfo info])
acceptNumberstyles.HexFloat
(or it withoutNumberStyles.AllowDecimalPoint
) and correctly parse string.Edit
Numbers.FormatDouble/Single(ref ValueStringBuilder sb, double/float value, ReadOnlySpan<char> format, NumberFormatInfo info)
so that they can correctly identifyX
specifier (which is also used for outputting integers in hex) which may have a trailing precision specifier, and correctly format it.The text was updated successfully, but these errors were encountered: