private const int Int64Precision = 19;
private const int UInt64Precision = 20;
+ private const int DoubleMaxExponent = 309;
+ private const int DoubleMinExponent = -324;
+
+ private const int FloatingPointMaxExponent = DoubleMaxExponent;
+ private const int FloatingPointMinExponent = DoubleMinExponent;
+
+ private const int SingleMaxExponent = 39;
+ private const int SingleMinExponent = -45;
+
/// <summary>Map from an ASCII char to its hex value, e.g. arr['b'] == 11. 0xFF means it's not a hex digit.</summary>
internal static ReadOnlySpan<byte> CharToHexLookup => new byte[]
{
internal static double NumberToDouble(ref NumberBuffer number)
{
number.CheckConsistency();
+ double result;
+
+ if (number.Scale > DoubleMaxExponent)
+ {
+ result = double.PositiveInfinity;
+ }
+ else if (number.Scale < DoubleMinExponent)
+ {
+ result = 0;
+ }
+ else
+ {
+ ulong bits = NumberToFloatingPointBits(ref number, in FloatingPointInfo.Double);
+ result = BitConverter.Int64BitsToDouble((long)(bits));
+ }
- ulong bits = NumberToFloatingPointBits(ref number, in FloatingPointInfo.Double);
- double result = BitConverter.Int64BitsToDouble((long)(bits));
return number.IsNegative ? -result : result;
}
internal static float NumberToSingle(ref NumberBuffer number)
{
number.CheckConsistency();
+ float result;
+
+ if (number.Scale > SingleMaxExponent)
+ {
+ result = float.PositiveInfinity;
+ }
+ else if (number.Scale < SingleMinExponent)
+ {
+ result = 0;
+ }
+ else
+ {
+ uint bits = (uint)(NumberToFloatingPointBits(ref number, in FloatingPointInfo.Single));
+ result = BitConverter.Int32BitsToSingle((int)(bits));
+ }
- uint bits = (uint)(NumberToFloatingPointBits(ref number, in FloatingPointInfo.Single));
- float result = BitConverter.Int32BitsToSingle((int)(bits));
return number.IsNegative ? -result : result;
}
}