Skip to content

double.ToString("R") is not round-trippable for 2^-25 and 2^-958 on .NET 10 (regression from .NET 8) #135582

Closed

Description

@BVV027

Description

On .NET 10, double.ToString("R") (and the default shortest round-trip ToString()) returns a string that does not parse back to the original value for some powers of two. The same values format correctly on .NET 8.

Found while validating an RFC 8785 (JSON canonicalization) number serializer against 100,000 JSON.stringify reference pairs.

Reproduction Steps

using System.Globalization;

foreach (int e in new[] { -25, -958 })
{
    double d = Math.ScaleB(1.0, e);
    string r = d.ToString("R", CultureInfo.InvariantCulture);
    bool roundTrips = double.Parse(r, CultureInfo.InvariantCulture) == d;
    Console.WriteLine($"2^{e}: R={r} roundTrips={roundTrips} G17={d.ToString("G17", CultureInfo.InvariantCulture)}");
}

Expected behavior

Shortest round-trippable output (as on .NET 8 and as produced by JavaScript Number.prototype.toString):

2^-25: R=2.9802322387695312E-08 roundTrips=True
2^-958: R=4.1045368012983762E-289 roundTrips=True

Actual behavior

2^-25: R=2.980232238769531E-08 roundTrips=False G17=2.9802322387695312E-08
2^-958: R=4.104536801298376E-289 roundTrips=False G17=4.1045368012983762E-289

The 16-digit output is one digit too short and parses to a neighbouring value. Both inputs are exact powers of two (zero mantissa bits), where the rounding interval is asymmetric (the lower neighbour is half as far away), so this looks like the lower boundary being treated as symmetric in the shortest-digits path.

Regression?

Yes. .NET 8.0.28 prints the expected values. .NET 9 was not tested.

Scanning all 2,098 positive powers of two (Math.ScaleB(1.0, e), e = -1074 … 1023), exactly these two fail on .NET 10.0.12; on .NET 8.0.28 none fail. 1,000,000 random finite doubles round-trip correctly on both.

Known Workarounds

Check the round trip and fall back to ToString("E16") (17 significant digits) or search precisions 1–17 for the shortest one that parses back.

Configuration

  • .NET 10.0.12 (SDK 10.0.401), Windows 10 22H2 (10.0.19045), x64
  • Comparison: .NET 8.0.28, same machine

Other information

Possibly related to the floating-point formatting work in #131068 / #134621 (not verified).

Activity

  1. added
    untriaged
    on Oct 11, 2026
  2. dotnet-policy-service commented on Oct 11, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/area-system-numerics
    See info in area-owners.md if you want to be subscribed.

  3. added this to the Future milestone on Oct 12, 2026
  4. added and removed
    untriaged
    on Oct 12, 2026
  5. locked and limited conversation to collaborators on Nov 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions