Repository navigation
Description
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)
[+-]?0[xX](?:[\da-fA-F]*\.[\da-fA-F]+|[\da-fA-F]\.?)(?:[pP][+-]?\d+)?notice that this is slightly different to the standard based on the discussion below, which talked about the exponent part
which means:
| valid | invalid |
|---|---|
| +0x7ff.3edp+1 | +7ff.3edp-1 |
| 0x7ff.3edp+1 | 0x7ff.3ede+1 |
| 0x7ff.3edp1 | 0x7ff.3uup1 |
| +0x7ff.3edp1 | 0x7ff.3ed |
| +0X7FF.3EDP1 | 0X7FF.3ED |
| -0x7ff.3edp1 | 0x7ff_3edp-1 |
| 0x7ff. | +-0x7ff.3edp-1 |
| 0x7ff | 0x7fu.3edp-1 |
| 0x.3edp-1 | 0x.p-1 |
benefits
- Easier parsing for native hexadecimal float/double. for example,
0x0.ffp0is the equivalent of0.99609375while using less chars. Same for formatting as it reduces the size of string to transfer. - Parsed number is percise in the limit of float/double's limitations. Since both float and double are based on raw bit, transistion from hexadecimal literal to them would be easier and without rounding if they are in the limitation. parsing decimals, on the other hand, would often has to round.
- It's in IEEE 754:2008/2019 standard, so it's necessary to add it.
proposal APIs
namespace System.Globalization {
[Flags]
enum NumberStyles {
// ...,
HexFloat = AllowLeadingWhite | AllowTrailingWhite | AllowLeadingSign | AllowHexSpecifier | AllowDecimalPoint
// ,...
}
}And let Numbers.ParseDouble/Single(string s, NumberStyles style[, NumberFormatInfo info]) accept Numberstyles.HexFloat (or it without NumberStyles.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 identify X specifier (which is also used for outputting integers in hex) which may have a trailing precision specifier, and correctly format it.
Activity
stephentoub commentedon Jan 11, 2020
cc: @tannergooding
Gnbrkm41 commentedon Jan 11, 2020
Tracked by #1387?
tannergooding commentedon Jan 11, 2020
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
[fFdD]?listed is not actually part of the IEEE specification and should be excluded.I also updated
hexSignificandto clarify that just.is not valid.The computed value is
hexSignificand * 2^decExponent.So, for example if you have
0x1.234p0this is:0x1==10x234 * 16^-3==564 * 16^-3==0.13769531251.1376953125 * 2^0==1.1376953125tannergooding commentedon Jan 11, 2020
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.
dovisutu commentedon Mar 12, 2020
I've updated the issue description to show what changes should be made. Free to discuss.
[-]Add the ability to parse a hexadecimal number into a float/double[/-][+]Add the ability to parse/format a hexadecimal number into a float/double[/+][-]Add the ability to parse/format a hexadecimal number into a float/double[/-][+]Add the ability to parse/format a float/double from/to a hexadecimal literal[/+]21 remaining items
The-Futurist commentedon Dec 3, 2022
It seems that that regex might be wrong, I found this one that seems to behave correctly:
0[Xx]([0-9a-fA-F]*\.[0-9a-fA-F]+|[0-9a-fA-F]+\.?)[Pp][-+]?[0-9]+[flFL]?From here: https://efxa.org/2014/05/10/regular-expressions-for-matching-data-values-in-compiler-lexers/
tannergooding commentedon Dec 6, 2022
It's possible its incorrect, but the ultimate logic won't be using a RegEx anyways
The-Futurist commentedon Dec 6, 2022
OK, is that because of performance considerations?
stephentoub commentedon Dec 6, 2022
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
The-Futurist commentedon Dec 6, 2022
That's quite interesting, thanks for explaining!