Skip to content

Add the ability to parse/format a float/double from/to a hexadecimal literal #1630

Closed

Description

@dovisutu

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.ffp0 is the equivalent of 0.99609375 while 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

  1. added
    untriaged
    on Jan 11, 2020
  2. stephentoub commented on Jan 11, 2020

    @stephentoub
    Member
  3. Gnbrkm41 commented on Jan 11, 2020

    @Gnbrkm41
    Contributor

    Tracked by #1387?

  4. tannergooding commented on Jan 11, 2020

    @tannergooding
    Member

    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:

    sign:           [+-]?
    hexIndicator:   0[xX]
    hexSignificand: (?:[\da-fA-F]*\.[\da-fA-F]+|[\da-fA-F]+\.?)
    decExponent:    [pP][+-]?\d+
    

    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
  5. tannergooding commented on Jan 11, 2020

    @tannergooding
    Member

    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.

  6. added
    api-suggestion
    and removed
    untriaged
    on Jan 11, 2020
  7. dovisutu commented on Mar 12, 2020

    @dovisutu
    Author

    I've updated the issue description to show what changes should be made. Free to discuss.

  8. added and removed
    api-suggestion
    on Mar 12, 2020
  9. changed the title [-]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[/+] on Mar 13, 2020
  10. changed the title [-]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[/+] on Mar 13, 2020
  11. 21 remaining items

  12. added
    help wanted
    on Mar 14, 2022
  13. The-Futurist commented on Dec 3, 2022

    @The-Futurist

    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/

  14. tannergooding commented on Dec 6, 2022

    @tannergooding
    Member

    It's possible its incorrect, but the ultimate logic won't be using a RegEx anyways

  15. The-Futurist commented on Dec 6, 2022

    @The-Futurist

    It's possible its incorrect, but the ultimate logic won't be using a RegEx anyways

    OK, is that because of performance considerations?

  16. stephentoub commented on Dec 6, 2022

    @stephentoub
    Member

    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

  17. The-Futurist commented on Dec 6, 2022

    @The-Futurist

    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!

  18. added
    in-pr
    on Oct 11, 2025
  19. removed
    in-pr
    on Feb 5, 2026
  20. removed
    help wanted
    on Feb 7, 2026
  21. added
    in-pr
    on Feb 8, 2026
  22. locked and limited conversation to collaborators on Apr 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

api-approvedarea-System.Numericsin-pr

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions