-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Add hexadecimal float/double parsing and formatting support (IEEE 754:2008) #120637
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
base: main
Are you sure you want to change the base?
Conversation
Co-authored-by: stephentoub <[email protected]>
Co-authored-by: stephentoub <[email protected]>
Co-authored-by: stephentoub <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot, there are also some analyzer failures:
D:\a\_work\1\s\src\libraries\System.Private.CoreLib\src\System\Number.Formatting.cs(639,96): error IDE0060: Remove unused parameter 'precision' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0060) [D:\a\_work\1\s\src\coreclr\System.Private.CoreLib\System.Private.CoreLib.csproj]
D:\a\_work\1\s\src\libraries\System.Private.CoreLib\src\System\Number.Formatting.cs(639,115): error IDE0060: Remove unused parameter 'format' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0060) [D:\a\_work\1\s\src\coreclr\System.Private.CoreLib\System.Private.CoreLib.csproj]
D:\a\_work\1\s\src\libraries\System.Private.CoreLib\src\System\Number.Formatting.cs(639,140): error IDE0060: Remove unused parameter 'info' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0060) [D:\a\_work\1\s\src\coreclr\System.Private.CoreLib\System.Private.CoreLib.csproj]
src/libraries/System.Private.CoreLib/src/System/Globalization/NumberFormatInfo.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/Number.Formatting.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/Number.Parsing.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Runtime/tests/System.Runtime.Tests/System/DoubleTests.cs
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR implements IEEE 754:2008 section 5.12.3 support for parsing and formatting floating-point numbers using hexadecimal significand representation. The implementation allows parsing values like 0x1.8p0
(= 1.5) and formatting floats/doubles to hex representation using the 'X'/'x' format specifiers.
Key Changes:
- Adds
NumberStyles.HexFloat
enum value for hexadecimal float parsing - Implements comprehensive hex float parsing logic with proper IEEE 754 conversion
- Adds hex float formatting support using 'X'/'x' format specifiers
Reviewed Changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.
Show a summary per file
File | Description |
---|---|
NumberStyles.cs |
Adds HexFloat composite style flag |
System.Runtime.cs |
Updates reference assembly with new HexFloat enum value |
NumberFormatInfo.cs |
Updates validation to allow hex specifier for floating-point with specific constraints |
Number.Parsing.cs |
Implements complete hex float parsing with IEEE 754 conversion |
Number.Formatting.cs |
Adds hex float formatting functionality |
DoubleTests.cs |
Adds basic test cases for hex float parsing |
src/libraries/System.Private.CoreLib/src/System/Number.Formatting.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/Number.Formatting.cs
Outdated
Show resolved
Hide resolved
{ | ||
if (value < 0) | ||
{ | ||
vlb.Append(TChar.CastFrom('-')); |
Copilot
AI
Oct 12, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Potential integer overflow when negating int.MinValue
. The negation of int.MinValue
overflows and remains negative, which could cause incorrect behavior.
vlb.Append(TChar.CastFrom('-')); | |
vlb.Append(TChar.CastFrom('-')); | |
if (value == int.MinValue) | |
{ | |
// int.MinValue cannot be negated; use its magnitude as uint | |
string numStr = ((uint)int.MinValue).ToString(); | |
foreach (char c in numStr) | |
{ | |
vlb.Append(TChar.CastFrom(c)); | |
} | |
return; | |
} |
Copilot uses AI. Check for mistakes.
… comprehensive tests Co-authored-by: stephentoub <[email protected]>
vlb.Append(TChar.CastFrom((char)('0' + (uvalue % 10)))); | ||
uvalue /= 10; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could use Math.DivRem
.
} | ||
|
||
// Parse "0x" or "0X" prefix | ||
if (index + 1 >= value.Length || |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (index + 1 >= value.Length || | |
if (index >= value.Length - 1 || |
To avoid potential overflow (just to be super pedantic about this).
Implementation Plan for Hexadecimal Float/Double Parsing and Formatting
HexFloat
composite style toNumberStyles
enumValidateParseStyleFloatingPoint
to allowAllowHexSpecifier
with appropriate flags for float/double parsingNumber.Parsing.cs
TryParseFloat
methodNumber.Formatting.cs
Code Review Feedback Addressed
Hot Path Performance (ValidateParseStyleFloatingPoint)
[DoesNotReturn]
attributeusing System.Diagnostics.CodeAnalysis;
FormatFloatAsHex Bugs Fixed
FormatInt32 Improvements
Parser Optimization
| 0x20
trick for case-insensitive 'x'/'X' comparisonComprehensive Testing
Original prompt
This section details on the original issue you should resolve
<issue_title>Add the ability to parse/format a float/double from/to a hexadecimal literal</issue_title>
<issue_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)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. </issue_description>Comments on the Issue (you are @copilot in this section)
@stephentoub cc: @tannergooding @tannergooding 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
hexSignificand
to clarify that just.
is not valid.The computed value is
hexSignificand * 2^decExponent
.So, for example if you have
0x1.234p0
this is:0x1
==1
0x234 * 16^-3
==564 * 16^-3
==0.1376953125
1.1376953125 * 2^0
==1.1376953125
</comment_new><comment_new>@tannergooding
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.</comment_new>
<comment_new>@tannergooding
I don't see why it wouldn't, provided it was still valid according to the IEEE requirements.</comment_new>
<comment_new>@tannergooding
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
hex
is allowed butdecimal-point
is not. It's an advanced scenario, but the number parser already supports it and will continue doing so even if we add support forhex
, so there is no reason to block it. The interesting scenario would be whether or not to allow the exponent to be optional and to default to0
(which would still be valid based on the normal conversion rules, etc).Fixes #1630
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.