// Let v = significand * 2^exponent. // Computes v / 10^estimated_power exactly, as a ratio of two bignums, numerator // and denominator. The functions GenerateShortestDigits and // GenerateCountedDigits will then convert this ratio to its decimal // representation d, with the required accuracy. // Then d * 10^estimated_power is the representation of v. // (Note: the fraction and the estimated_power might get adjusted before // generating the decimal representation.) // // The initial start values consist of: // - a scaled numerator: s.t. numerator/denominator == v / 10^estimated_power. // - a scaled (common) denominator. // optionally (used by GenerateShortestDigits to decide if it has the shortest // decimal converting back to v): // - v - m-: the distance to the lower boundary. // - m+ - v: the distance to the upper boundary. // // v, m+, m-, and therefore v - m- and m+ - v all share the same denominator. // // Let ep == estimated_power, then the returned values will satisfy: // v / 10^ep = numerator / denominator. // v's boundarys m- and m+: // m- / 10^ep == v / 10^ep - delta_minus / denominator // m+ / 10^ep == v / 10^ep + delta_plus / denominator // Or in other words: // m- == v - delta_minus * 10^ep / denominator; // m+ == v + delta_plus * 10^ep / denominator; // // Since 10^(k-1) <= v < 10^k (with k == estimated_power) // or 10^k <= v < 10^(k+1) // we then have 0.1 <= numerator/denominator < 1 // or 1 <= numerator/denominator < 10 // // It is then easy to kickstart the digit-generation routine. // // The boundary-deltas are only filled if need_boundary_deltas is set. private static void InitialScaledStartValues( double v, int estimated_power, bool need_boundary_deltas, Bignum numerator, Bignum denominator, Bignum delta_minus, Bignum delta_plus) { var bits = (ulong)BitConverter.DoubleToInt64Bits(v); if (DoubleHelper.Exponent(bits) >= 0) { InitialScaledStartValuesPositiveExponent( v, estimated_power, need_boundary_deltas, numerator, denominator, delta_minus, delta_plus); } else if (estimated_power >= 0) { InitialScaledStartValuesNegativeExponentPositivePower( v, estimated_power, need_boundary_deltas, numerator, denominator, delta_minus, delta_plus); } else { InitialScaledStartValuesNegativeExponentNegativePower( v, estimated_power, need_boundary_deltas, numerator, denominator, delta_minus, delta_plus); } }
// Provides a decimal representation of v. // Returns true if it succeeds, otherwise the result cannot be trusted. // There will be *length digits inside the buffer (not null-terminated). // If the function returns true then // v == (double) (buffer * 10^decimal_exponent). // The digits in the buffer are the shortest representation possible: no // 0.09999999999999999 instead of 0.1. The shorter representation will even be // chosen even if the longer one would be closer to v. // The last digit will be closest to the actual v. That is, even if several // digits might correctly yield 'v' when read again, the closest will be // computed. private static bool Grisu3(double v, FastDtoaBuilder buffer) { long bits = BitConverter.DoubleToInt64Bits(v); DiyFp w = DoubleHelper.AsNormalizedDiyFp(bits); // boundary_minus and boundary_plus are the boundaries between v and its // closest floating-point neighbors. Any number strictly between // boundary_minus and boundary_plus will round to v when convert to a double. // Grisu3 will never output representations that lie exactly on a boundary. DiyFp boundaryMinus = new DiyFp(), boundaryPlus = new DiyFp(); DoubleHelper.NormalizedBoundaries(bits, boundaryMinus, boundaryPlus); var tenMk = new DiyFp(); // Cached power of ten: 10^-k int mk = CachedPowers.GetCachedPower(w.E + DiyFp.KSignificandSize, MinimalTargetExponent, MaximalTargetExponent, tenMk); // Note that ten_mk is only an approximation of 10^-k. A DiyFp only contains a // 64 bit significand and ten_mk is thus only precise up to 64 bits. // The DiyFp::Times procedure rounds its result, and ten_mk is approximated // too. The variable scaled_w (as well as scaled_boundary_minus/plus) are now // off by a small amount. // In fact: scaled_w - w*10^k < 1ulp (unit in the last place) of scaled_w. // In other words: let f = scaled_w.f() and e = scaled_w.e(), then // (f-1) * 2^e < w*10^k < (f+1) * 2^e DiyFp scaledW = DiyFp.Times(w, tenMk); // In theory it would be possible to avoid some recomputations by computing // the difference between w and boundary_minus/plus (a power of 2) and to // compute scaled_boundary_minus/plus by subtracting/adding from // scaled_w. However the code becomes much less readable and the speed // enhancements are not terriffic. DiyFp scaledBoundaryMinus = DiyFp.Times(boundaryMinus, tenMk); DiyFp scaledBoundaryPlus = DiyFp.Times(boundaryPlus, tenMk); // DigitGen will generate the digits of scaled_w. Therefore we have // v == (double) (scaled_w * 10^-mk). // Set decimal_exponent == -mk and pass it to DigitGen. If scaled_w is not an // integer than it will be updated. For instance if scaled_w == 1.23 then // the buffer will be filled with "123" und the decimal_exponent will be // decreased by 2. return(DigitGen(scaledBoundaryMinus, scaledW, scaledBoundaryPlus, buffer, mk)); }
public static void NumberToString( double v, DtoaMode mode, int requested_digits, DtoaBuilder builder, out int decimal_point) { var bits = (ulong)BitConverter.DoubleToInt64Bits(v); var significand = DoubleHelper.Significand(bits); var is_even = (significand & 1) == 0; var exponent = DoubleHelper.Exponent(bits); var normalized_exponent = DoubleHelper.NormalizedExponent(significand, exponent); // estimated_power might be too low by 1. var estimated_power = EstimatePower(normalized_exponent); // Shortcut for Fixed. // The requested digits correspond to the digits after the point. If the // number is much too small, then there is no need in trying to get any // digits. if (mode == DtoaMode.Fixed && -estimated_power - 1 > requested_digits) { // Set decimal-point to -requested_digits. This is what Gay does. // Note that it should not have any effect anyways since the string is // empty. decimal_point = -requested_digits; return; } Bignum numerator = new Bignum(); Bignum denominator = new Bignum(); Bignum delta_minus = new Bignum(); Bignum delta_plus = new Bignum(); // Make sure the bignum can grow large enough. The smallest double equals // 4e-324. In this case the denominator needs fewer than 324*4 binary digits. // The maximum double is 1.7976931348623157e308 which needs fewer than // 308*4 binary digits. var need_boundary_deltas = mode == DtoaMode.Shortest; InitialScaledStartValues( v, estimated_power, need_boundary_deltas, numerator, denominator, delta_minus, delta_plus); // We now have v = (numerator / denominator) * 10^estimated_power. FixupMultiply10( estimated_power, is_even, out decimal_point, numerator, denominator, delta_minus, delta_plus); // We now have v = (numerator / denominator) * 10^(decimal_point-1), and // 1 <= (numerator + delta_plus) / denominator < 10 switch (mode) { case DtoaMode.Shortest: GenerateShortestDigits( numerator, denominator, delta_minus, delta_plus, is_even, builder); break; case DtoaMode.Fixed: BignumToFixed( requested_digits, ref decimal_point, numerator, denominator, builder); break; case DtoaMode.Precision: GenerateCountedDigits( requested_digits, ref decimal_point, numerator, denominator, builder); break; default: ExceptionHelper.ThrowArgumentOutOfRangeException(); break; } }
// See comments for InitialScaledStartValues private static void InitialScaledStartValuesNegativeExponentNegativePower( double v, int estimated_power, bool need_boundary_deltas, Bignum numerator, Bignum denominator, Bignum delta_minus, Bignum delta_plus) { const ulong kMinimalNormalizedExponent = 0x0010000000000000; var bits = (ulong)BitConverter.DoubleToInt64Bits(v); ulong significand = DoubleHelper.Significand(bits); int exponent = DoubleHelper.Exponent(bits); // Instead of multiplying the denominator with 10^estimated_power we // multiply all values (numerator and deltas) by 10^-estimated_power. // Use numerator as temporary container for power_ten. Bignum power_ten = numerator; power_ten.AssignPowerUInt16(10, -estimated_power); if (need_boundary_deltas) { // Since power_ten == numerator we must make a copy of 10^estimated_power // before we complete the computation of the numerator. // delta_plus = delta_minus = 10^estimated_power delta_plus.AssignBignum(power_ten); delta_minus.AssignBignum(power_ten); } // numerator = significand * 2 * 10^-estimated_power // since v = significand * 2^exponent this is equivalent to // numerator = v * 10^-estimated_power * 2 * 2^-exponent. // Remember: numerator has been abused as power_ten. So no need to assign it // to itself. numerator.MultiplyByUInt64(significand); // denominator = 2 * 2^-exponent with exponent < 0. denominator.AssignUInt16(1); denominator.ShiftLeft(-exponent); if (need_boundary_deltas) { // Introduce a common denominator so that the deltas to the boundaries are // integers. numerator.ShiftLeft(1); denominator.ShiftLeft(1); // With this shift the boundaries have their correct value, since // delta_plus = 10^-estimated_power, and // delta_minus = 10^-estimated_power. // These assignments have been done earlier. // The special case where the lower boundary is twice as close. // This time we have to look out for the exception too. ulong v_bits = bits; if ((v_bits & DoubleHelper.KSignificandMask) == 0 && // The only exception where a significand == 0 has its boundaries at // "normal" distances: (v_bits & DoubleHelper.KExponentMask) != kMinimalNormalizedExponent) { numerator.ShiftLeft(1); // *2 denominator.ShiftLeft(1); // *2 delta_plus.ShiftLeft(1); // *2 } } }