Skip to content
Permalink
Browse files

Some cleanup of the System.Number class (dotnet/coreclr#20619)

* 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 jkotas committed Oct 29, 2018
1 parent ddfe3e4 commit cfaf1c84c5e08536e6d510d247c5cfefbe5c0587
@@ -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;
}
@@ -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)
@@ -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
@@ -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();
@@ -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;
}
@@ -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
@@ -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 _);
}

//
@@ -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)
@@ -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 _);
}

//
@@ -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/
@@ -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);
@@ -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();
@@ -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
@@ -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;
}

@@ -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 cfaf1c8

Please sign in to comment.
You can’t perform that action at this time.