3

According to the documentation :

When precision specifier controls the number of fractional digits in the result string, the result string reflects a number that is rounded to a representable result nearest to the infinitely precise result. If there are two equally near representable results:

On .NET Framework and .NET Core up to .NET Core 2.0, the runtime selects the result with the greater least significant digit (that is, using MidpointRounding.AwayFromZero).

On .NET Core 2.1 and later, the runtime selects the result with an even least significant digit (that is, using MidpointRounding.ToEven).

In this case, I would expect both to produce 1.584599426. Using .NET Framework 4.5.2 the resulting text is 1.584599426 but using .NET5, the resulting text is 1.584599425.

My only thought is that with the .NET5 runtime, 1.584599425 is a "closer" representation but I don't know how to verify that.

Set up :

  • Windows 10, x64
  • Visual Studio 2019 (Community)
  • .NET5 SDK Version : 5.0.300
  • .NET Framework Version : 528372

Thanks.

G. Ath
  • 31
  • 2
  • 2
    5845994255/10000000000 is a periodic number in binary just like 1/3 is a periodic number in decimal. That means it can't be accurately represented by a floating point number. You should actually get 1.584599425499999991728827808401547372341156005859375, which should indeed round to 1.584599425. Don't count on floating point numbers being exactly what you think they are. – ikegami Oct 15 '21 at 12:52
  • Getting 1.584599426 at all is even more surprising since the number *isn't* at a midpoint (the literal looks like it, but after parsing it it won't be anymore and the parsed value is what counts), it is strictly closer to 1.584599425 than it is to 1.584599426 – harold Oct 15 '21 at 12:53
  • 1
    Presumably connected to https://devblogs.microsoft.com/dotnet/floating-point-parsing-and-formatting-improvements-in-net-core-3-0/ In short: As of Net Core 3 the parsing stuff here is IEEE 754-2008 compliant. – Ralf Oct 15 '21 at 13:34
  • Relevant: [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Charlieface Oct 15 '21 at 15:29

0 Answers0