/// <summary> /// Convert a 32-bit Ryu floating point number to a roundtrip notation string. /// </summary> public void ToRoundtripString(StringBuilder result, RyuFormatOptions options, NumberFormatInfo info = null) { // Step 5: Print the decimal representation if (info == null) { info = CultureInfo.CurrentCulture.NumberFormat; } if (sign) { result.Append(info.NegativeSign); } #if DEBUG if (info.NumberDecimalSeparator.Length > 1) { throw new ArgumentException("Requires a single character decimal point"); } #endif // Print the decimal digits uint mantissa = this.mantissa; int olength = RyuUtils.DecimalLength9(mantissa), start = result.Length, index = olength + start; result.Length = index + 1; index = RyuUtils.PrintGroups42(result, ref mantissa, index); // Group of 1 if (mantissa >= 10U) { string digits = RyuTables.DIGIT_TABLE[mantissa]; // We can't use memcpy here: the decimal dot goes between these two digits result[index--] = digits[1]; result[start] = digits[0]; } else { result[start] = mantissa.DigitToChar(); } // Print decimal point if needed if (olength > 1) { result[start + 1] = info.NumberDecimalSeparator[0]; index += olength; } result.Length = index; RyuUtils.AppendExponent(result, exponent + olength - 1, options, info); }
/// <summary> /// Convert a 64-bit Ryu floating point number to a roundtrip notation string. /// </summary> public void ToRoundtripString(StringBuilder result, RyuFormatOptions options, NumberFormatInfo info = null) { // Step 5: Print the decimal representation if (info == null) { info = CultureInfo.CurrentCulture.NumberFormat; } if (sign) { result.Append(info.NegativeSign); } #if DEBUG if (info.NumberDecimalSeparator.Length > 1) { throw new ArgumentException("Requires a single character decimal point"); } #endif ulong mantissa = this.mantissa; uint mantissaShort; int olength = RyuUtils.DecimalLength17(mantissa), start = result.Length, index = olength + start; result.Length = index + 1; // Print the decimal digits: group of 8 if ((mantissa >> 32) != 0U) { // We prefer 32-bit operations, even on 64-bit platforms. // We have at most 17 digits, and uint can store 9 digits. // If output doesn't fit into uint, we cut off 8 digits, // so the rest will fit into uint ulong q = mantissa / 100000000UL; mantissaShort = (uint)mantissa - 100000000U * (uint)q; uint o10000 = mantissaShort / 10000U; uint c0 = mantissaShort - 10000U * o10000, d0 = o10000 % 10000U; uint c1 = c0 / 100U, d1 = d0 / 100U; mantissa = q; index = result.WriteDigits(index, c0 - 100U * c1); index = result.WriteDigits(index, c1); index = result.WriteDigits(index, d0 - 100U * d1); index = result.WriteDigits(index, d1); } mantissaShort = (uint)mantissa; index = RyuUtils.PrintGroups42(result, ref mantissaShort, index); // Group of 1 if (mantissaShort >= 10U) { string digits = RyuTables.DIGIT_TABLE[mantissaShort]; // We can't use memcpy here: the decimal dot goes between these two digits result[index--] = digits[1]; result[start] = digits[0]; } else { result[start] = mantissaShort.DigitToChar(); } // Print decimal point if needed if (olength > 1) { result[start + 1] = info.NumberDecimalSeparator[0]; index += olength; } result.Length = index; RyuUtils.AppendExponent(result, exponent + olength - 1, options, info); }
/// <summary> /// Convert a 64-bit floating point number to an exponential notation string. /// </summary> internal static void ToExponentialString(StringBuilder result, ulong ieeeMantissa, uint ieeeExponent, int precision, RyuFormatOptions options, NumberFormatInfo info) { bool printDP = precision > 0, soft = (options & RyuFormatOptions.SoftPrecision) != 0; uint digits = 0U; int printedDigits = 0, availDigits = 0, exp = 0, exponent = Decode(ieeeMantissa, ieeeExponent, out ulong mantissa), start = result.Length; ulong mantShift = mantissa << MANTISSA_SHIFT; ++precision; if (exponent >= -RyuFloat64.DOUBLE_MANTISSA_BITS) { int idx = (exponent < 0) ? 0 : RyuUtils.IndexForExponent(exponent), i = RyuUtils.LengthForIndex(idx) - 1, j = Pow10BitsForIndex(idx) - exponent + MANTISSA_SHIFT, p = RyuTables.POW10_OFFSET_D[idx] + i; for (; i >= 0; i--) { // Temporary: j is usually around 128, and by shifting a bit, we push it // to 128 or above, which is a slightly faster code path in // MulShiftMod1E9. Instead, we can just increase the multipliers digits = RyuUtils.MulShiftMod1E9(mantShift, RyuTables.POW10_SPLIT_D[p, 0], RyuTables.POW10_SPLIT_D[p, 1], RyuTables.POW10_SPLIT_D[p, 2], j); if (printedDigits > 0) { if (printedDigits + 9 > precision) { availDigits = 9; break; } RyuUtils.Append9Digits(result, digits); printedDigits += 9; } else if (digits != 0U) { availDigits = RyuUtils.DecimalLength9(digits); exp = i * 9 + availDigits - 1; if (availDigits > precision) { break; } RyuUtils.AppendDDigits(result, digits, availDigits + 1, printDP, info); printedDigits = availDigits; availDigits = 0; } p--; } } if (exponent < 0 && availDigits == 0) { int idx = (-exponent) >> 4, pMax = RyuTables.POW10_OFFSET_2_D[idx + 1], p = RyuTables.POW10_OFFSET_2_D[idx], j = MANTISSA_SHIFT + POW10_ADDITIONAL_BITS - exponent - (idx << 4); for (int i = RyuTables.MIN_BLOCK_2_D[idx]; i < 200; i++) { digits = (p >= pMax) ? 0U : RyuUtils.MulShiftMod1E9(mantShift, RyuTables.POW10_SPLIT_2_D[p, 0], RyuTables.POW10_SPLIT_2_D[p, 1], RyuTables.POW10_SPLIT_2_D[p, 2], j); if (printedDigits > 0) { if (printedDigits + 9 > precision) { availDigits = 9; break; } RyuUtils.Append9Digits(result, digits); printedDigits += 9; } else if (digits != 0) { availDigits = RyuUtils.DecimalLength9(digits); exp = (i + 1) * -9 + availDigits - 1; if (availDigits > precision) { break; } RyuUtils.AppendDDigits(result, digits, availDigits + 1, printDP, info); printedDigits = availDigits; availDigits = 0; } p++; } } // 0 = don't round up; 1 = round up unconditionally; 2 = round up if odd int maxDigits = precision - printedDigits, roundFlag; uint lastDigit = 0U; if (availDigits == 0) { digits = 0U; } if (availDigits > maxDigits) { lastDigit = RyuUtils.LastDigit(ref digits, availDigits - maxDigits); } if (lastDigit != 5U) { roundFlag = (lastDigit > 5U) ? 1 : 0; } else { // Is m * 2^e2 * 10^(precision + 1 - exp) integer? // precision was already increased by 1, so we don't need to write + 1 here. int rexp = precision - exp; bool trailingZeroes = HasTrailingZeroes(exponent, rexp, mantissa); if (rexp < 0 && trailingZeroes) { trailingZeroes = RyuUtils.IsMultipleOf5Power(mantissa, -rexp); } roundFlag = trailingZeroes ? 2 : 1; } if (printedDigits > 0) { if (digits == 0U) { if (!soft) { RyuUtils.Append0(result, maxDigits); } } else { RyuUtils.AppendCDigits(result, digits, maxDigits); } } else { RyuUtils.AppendDDigits(result, digits, maxDigits + 1, printDP, info); } if (roundFlag != 0 && RyuUtils.RoundResult(result, start, roundFlag, out _, info)) { exp++; } if (soft) { RyuUtils.SoftenResult(result, info); } RyuUtils.AppendExponent(result, exp, options, info); }