Skip to content

Commit

Permalink
Some cleanup of the System.Number class (dotnet/coreclr#20619)
Browse files Browse the repository at this point in the history
* Formatting Number.Formatting.cs and Number.Parsing.cs

* Removing some duplicated parsing code by having the Parse method call TryParse

* Moving two constants from NumberBuffer to Dragon4

* Rename FloatPrecision to SinglePrecision

* Updating the casing of the NumberBuffer fields

* Updating NumberBuffer to allow taking a custom-sized digit buffer.

* Updating the various NumberBufferLength constants to be the exact needed lengths

* Fixing DoubleNumberBufferLength and SingleNumberBufferLength to account for the rounding digit.

* Fixing TryParseNumber to use the correct maxDigCount

* Ensure the TryParseSingle out result is assigned on success

Signed-off-by: dotnet-bot <dotnet-bot@microsoft.com>
  • Loading branch information
tannergooding authored and dotnet-bot committed Oct 29, 2018
1 parent 8305e5f commit 7e67b7a
Show file tree
Hide file tree
Showing 17 changed files with 778 additions and 743 deletions.
2 changes: 1 addition & 1 deletion src/Common/src/CoreLib/System/Byte.cs
Expand Up @@ -160,7 +160,7 @@ private static bool TryParse(ReadOnlySpan<char> s, NumberStyles style, NumberFor
{
result = 0;
int i;
if (!Number.TryParseInt32(s, style, info, out i))
if (!Number.TryParseInt32(s, style, info, out i, out _))
{
return false;
}
Expand Down
8 changes: 4 additions & 4 deletions src/Common/src/CoreLib/System/Decimal.cs
Expand Up @@ -488,12 +488,12 @@ public static bool TryParse(string s, out decimal result)
return false;
}

return Number.TryParseDecimal(s, NumberStyles.Number, NumberFormatInfo.CurrentInfo, out result);
return Number.TryParseDecimal(s, NumberStyles.Number, NumberFormatInfo.CurrentInfo, out result, out _);
}

public static bool TryParse(ReadOnlySpan<char> s, out decimal result)
{
return Number.TryParseDecimal(s, NumberStyles.Number, NumberFormatInfo.CurrentInfo, out result);
return Number.TryParseDecimal(s, NumberStyles.Number, NumberFormatInfo.CurrentInfo, out result, out _);
}

public static bool TryParse(string s, NumberStyles style, IFormatProvider provider, out decimal result)
Expand All @@ -506,13 +506,13 @@ public static bool TryParse(string s, NumberStyles style, IFormatProvider provid
return false;
}

return Number.TryParseDecimal(s, style, NumberFormatInfo.GetInstance(provider), out result);
return Number.TryParseDecimal(s, style, NumberFormatInfo.GetInstance(provider), out result, out _);
}

public static bool TryParse(ReadOnlySpan<char> s, NumberStyles style, IFormatProvider provider, out decimal result)
{
NumberFormatInfo.ValidateParseStyleFloatingPoint(style);
return Number.TryParseDecimal(s, style, NumberFormatInfo.GetInstance(provider), out result);
return Number.TryParseDecimal(s, style, NumberFormatInfo.GetInstance(provider), out result, out _);
}

// Returns a binary representation of a Decimal. The return value is an
Expand Down
2 changes: 1 addition & 1 deletion src/Common/src/CoreLib/System/Double.cs
Expand Up @@ -341,7 +341,7 @@ public static bool TryParse(ReadOnlySpan<char> s, NumberStyles style, IFormatPro

private static bool TryParse(ReadOnlySpan<char> s, NumberStyles style, NumberFormatInfo info, out double result)
{
bool success = Number.TryParseDouble(s, style, info, out result);
bool success = Number.TryParseDouble(s, style, info, out result, out _);
if (!success)
{
ReadOnlySpan<char> sTrim = s.Trim();
Expand Down
2 changes: 1 addition & 1 deletion src/Common/src/CoreLib/System/Int16.cs
Expand Up @@ -201,7 +201,7 @@ private static bool TryParse(ReadOnlySpan<char> s, NumberStyles style, NumberFor
{
result = 0;
int i;
if (!Number.TryParseInt32(s, style, info, out i))
if (!Number.TryParseInt32(s, style, info, out i, out _))
{
return false;
}
Expand Down
8 changes: 4 additions & 4 deletions src/Common/src/CoreLib/System/Int32.cs
Expand Up @@ -152,12 +152,12 @@ public static bool TryParse(string s, out int result)
return false;
}

return Number.TryParseInt32(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result);
return Number.TryParseInt32(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result, out _);
}

public static bool TryParse(ReadOnlySpan<char> s, out int result)
{
return Number.TryParseInt32(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result);
return Number.TryParseInt32(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result, out _);
}

// Parses an integer from a String in the given style. Returns false rather
Expand All @@ -173,13 +173,13 @@ public static bool TryParse(string s, NumberStyles style, IFormatProvider provid
return false;
}

return Number.TryParseInt32(s, style, NumberFormatInfo.GetInstance(provider), out result);
return Number.TryParseInt32(s, style, NumberFormatInfo.GetInstance(provider), out result, out _);
}

public static bool TryParse(ReadOnlySpan<char> s, NumberStyles style, IFormatProvider provider, out int result)
{
NumberFormatInfo.ValidateParseStyleInteger(style);
return Number.TryParseInt32(s, style, NumberFormatInfo.GetInstance(provider), out result);
return Number.TryParseInt32(s, style, NumberFormatInfo.GetInstance(provider), out result, out _);
}

//
Expand Down
8 changes: 4 additions & 4 deletions src/Common/src/CoreLib/System/Int64.cs
Expand Up @@ -143,12 +143,12 @@ public static bool TryParse(string s, out long result)
return false;
}

return Number.TryParseInt64(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result);
return Number.TryParseInt64(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result, out _);
}

public static bool TryParse(ReadOnlySpan<char> s, out long result)
{
return Number.TryParseInt64(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result);
return Number.TryParseInt64(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result, out _);
}

public static bool TryParse(string s, NumberStyles style, IFormatProvider provider, out long result)
Expand All @@ -161,13 +161,13 @@ public static bool TryParse(string s, NumberStyles style, IFormatProvider provid
return false;
}

return Number.TryParseInt64(s, style, NumberFormatInfo.GetInstance(provider), out result);
return Number.TryParseInt64(s, style, NumberFormatInfo.GetInstance(provider), out result, out _);
}

public static bool TryParse(ReadOnlySpan<char> s, NumberStyles style, IFormatProvider provider, out long result)
{
NumberFormatInfo.ValidateParseStyleInteger(style);
return Number.TryParseInt64(s, style, NumberFormatInfo.GetInstance(provider), out result);
return Number.TryParseInt64(s, style, NumberFormatInfo.GetInstance(provider), out result, out _);
}

//
Expand Down
25 changes: 13 additions & 12 deletions src/Common/src/CoreLib/System/Number.Dragon4.cs
Expand Up @@ -2,16 +2,17 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
using Internal.Runtime.CompilerServices;

namespace System
{
internal static partial class Number
{
private static unsafe void Dragon4(double value, int precision, ref NumberBuffer number)
{
const double Log10V2 = 0.30102999566398119521373889472449;

// DriftFactor = 1 - Log10V2 - epsilon (a small number account for drift of floating point multiplication)
const double DriftFactor = 0.69;

// ========================================================================================================================================
// This implementation is based on the paper: https://www.cs.indiana.edu/~dyb/pubs/FP-Printing-PLDI96.pdf
// Besides the paper, some of the code and ideas are modified from http://www.ryanjuckett.com/programming/printing-floating-point-numbers/
Expand Down Expand Up @@ -149,7 +150,7 @@ private static unsafe void Dragon4(double value, int precision, ref NumberBuffer
r.Multiply10();
}

number.scale = (k - 1);
number.Scale = (k - 1);

// This the prerequisite of calling BigInteger.HeuristicDivide().
BigInteger.PrepareHeuristicDivide(ref r, ref s);
Expand All @@ -171,7 +172,7 @@ private static unsafe void Dragon4(double value, int precision, ref NumberBuffer
break;
}

number.digits[digitsNum] = (char)('0' + currentDigit);
number.Digits[digitsNum] = (char)('0' + currentDigit);
digitsNum++;

r.Multiply10();
Expand Down Expand Up @@ -199,7 +200,7 @@ private static unsafe void Dragon4(double value, int precision, ref NumberBuffer

if (isRoundDown)
{
number.digits[digitsNum] = (char)('0' + currentDigit);
number.Digits[digitsNum] = (char)('0' + currentDigit);
digitsNum++;
}
else
Expand All @@ -218,7 +219,7 @@ private static unsafe void Dragon4(double value, int precision, ref NumberBuffer
// Output 1 at the next highest exponent
*pCurrentDigit = '1';
digitsNum++;
number.scale += 1;
number.Scale += 1;
break;
}

Expand All @@ -244,14 +245,14 @@ private static unsafe void Dragon4(double value, int precision, ref NumberBuffer

while (digitsNum < precision)
{
number.digits[digitsNum] = '0';
number.Digits[digitsNum] = '0';
digitsNum++;
}

number.digits[precision] = '\0';
number.Digits[precision] = '\0';

number.scale++;
number.sign = double.IsNegative(value);
number.Scale++;
number.Sign = double.IsNegative(value);
}
}
}

0 comments on commit 7e67b7a

Please sign in to comment.