Skip to content

Commit b30280d

Browse files
Fixing up the Double/Single parsing code to be correct (dotnet#20707)
* Don't normalize -0.0 to 0.0 when parsing * Updating NumberBuffer to validate the constructor inputs * Updating NumberToDouble to just get the precision * Don't check for non-significant zero's in NumberToDouble * Updating Number.BigInteger to carry additional space for the worst-case scenario * Removing some dead code from double.TryParse * Updating NumberToDouble to use the RealParser implementation from Roslyn * Fixing TryNumberToDouble and TryNumberToSingle to apply the appropriate sign. * Adding a fast path for double/single parsing when we have <= 15 digits and an absolute scale <= 22 * Update NumberBuffer to also track whether any input digits past maxDigCount were non-zero * Renaming NumberToFloatingPointBitsRoslyn to NumberToFloatingPointBits * Updating TryNumberToDouble and TryNumberToSingle to support Overflow to Infinity * Fixing a Debug.Assert in TryParseNumber * Fixing `DecimalNumberBufferLength` to 30 * Renaming NumberToFloatingPointBitsRoslyn to NumberToFloatingPointBits * Clarifying the NumberBufferLength comments * Fixing TryNumberToDecimal to check the last digit in the buffer, if it exists * Disable some CoreFX tests due to the single/double/decimal parsing fixes * Fix TryNumberToDecimal to not modify the value of p in the assert. Co-Authored-By: tannergooding <[email protected]> * Updating NumberToFloatingPointBits to use single-precision arithmetic and extended-precision multiplication where possible * Splitting the NumberToFloatingPointBits code into a fast and slow-path method * Ensure Roslyn is properly attributed. * Removing the 80-bit extended precision fast path for NumberToFloatingPointBits, due to a bug * Fixing the double and single parser to ignore case for Infinity and NaN * Add a clarifying comment to Number.NumberToFloatingPointBits that the code has been modified from the original source. * Removing the remaining code that was used by the 80-bit extended precision fast-path in NumberToFloatingPointBits * Adding a missing comma to the CoreFX.issues.json * Remove licensing "glue" and re-release the Roslyn RealParser code under the MIT license. * Some minor cleanup to the NumberToFloatingPointBits code.
1 parent 002603e commit b30280d

12 files changed

+1407
-577
lines changed

src/System.Private.CoreLib/Resources/Strings.resx

-6
Original file line numberDiff line numberDiff line change
@@ -3100,9 +3100,6 @@
31003100
<data name="Overflow_Decimal" xml:space="preserve">
31013101
<value>Value was either too large or too small for a Decimal.</value>
31023102
</data>
3103-
<data name="Overflow_Double" xml:space="preserve">
3104-
<value>Value was either too large or too small for a Double.</value>
3105-
</data>
31063103
<data name="Overflow_Duration" xml:space="preserve">
31073104
<value>The duration cannot be returned for TimeSpan.MinValue because the absolute value of TimeSpan.MinValue exceeds the value of TimeSpan.MaxValue.</value>
31083105
</data>
@@ -3124,9 +3121,6 @@
31243121
<data name="Overflow_SByte" xml:space="preserve">
31253122
<value>Value was either too large or too small for a signed byte.</value>
31263123
</data>
3127-
<data name="Overflow_Single" xml:space="preserve">
3128-
<value>Value was either too large or too small for a Single.</value>
3129-
</data>
31303124
<data name="Overflow_TimeSpanElementTooLarge" xml:space="preserve">
31313125
<value>The TimeSpan string '{0}' could not be parsed because at least one of the numeric components is out of range or contains too many digits.</value>
31323126
</data>

src/System.Private.CoreLib/shared/System.Private.CoreLib.Shared.projitems

+1-1
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@
294294
<Compile Include="$(MSBuildThisFileDirectory)System\Number.Formatting.cs" />
295295
<Compile Include="$(MSBuildThisFileDirectory)System\Number.Grisu3.cs" />
296296
<Compile Include="$(MSBuildThisFileDirectory)System\Number.NumberBuffer.cs" />
297-
<Compile Include="$(MSBuildThisFileDirectory)System\Number.NumberToDouble.cs" />
297+
<Compile Include="$(MSBuildThisFileDirectory)System\Number.NumberToFloatingPointBits.cs" />
298298
<Compile Include="$(MSBuildThisFileDirectory)System\Number.Parsing.cs" />
299299
<Compile Include="$(MSBuildThisFileDirectory)System\NullReferenceException.cs" />
300300
<Compile Include="$(MSBuildThisFileDirectory)System\ObjectDisposedException.cs" />

src/System.Private.CoreLib/shared/System/Double.cs

+1-22
Original file line numberDiff line numberDiff line change
@@ -341,28 +341,7 @@ public static bool TryParse(ReadOnlySpan<char> s, NumberStyles style, IFormatPro
341341

342342
private static bool TryParse(ReadOnlySpan<char> s, NumberStyles style, NumberFormatInfo info, out double result)
343343
{
344-
bool success = Number.TryParseDouble(s, style, info, out result, out _);
345-
if (!success)
346-
{
347-
ReadOnlySpan<char> sTrim = s.Trim();
348-
if (sTrim.EqualsOrdinal(info.PositiveInfinitySymbol))
349-
{
350-
result = PositiveInfinity;
351-
}
352-
else if (sTrim.EqualsOrdinal(info.NegativeInfinitySymbol))
353-
{
354-
result = NegativeInfinity;
355-
}
356-
else if (sTrim.EqualsOrdinal(info.NaNSymbol))
357-
{
358-
result = NaN;
359-
}
360-
else
361-
{
362-
return false; // We really failed
363-
}
364-
}
365-
return true;
344+
return Number.TryParseDouble(s, style, info, out result);
366345
}
367346

368347
//

0 commit comments

Comments
 (0)