// returns a * b; public static DiyFp Times(ref DiyFp a, ref DiyFp b) { DiyFp result = a; result.Multiply(ref b); return(result); }
// 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(ref GrisuDouble v, Span <char> buffer, out int length, out int decimal_exponent) { DiyFp w = v.AsNormalizedDiyFp(); // 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 boundary_minus, boundary_plus; v.NormalizedBoundaries(out boundary_minus, out boundary_plus); Debug.Assert(boundary_plus.E == w.E); DiyFp ten_mk; // Cached power of ten: 10^-k int mk; // -k int ten_mk_minimal_binary_exponent = kMinimalTargetExponent - (w.E + DiyFp.kSignificandSize); int ten_mk_maximal_binary_exponent = kMaximalTargetExponent - (w.E + DiyFp.kSignificandSize); PowersOfTenCache.GetCachedPowerForBinaryExponentRange( ten_mk_minimal_binary_exponent, ten_mk_maximal_binary_exponent, out ten_mk, out mk); Debug.Assert((kMinimalTargetExponent <= w.E + ten_mk.E + DiyFp.kSignificandSize) && (kMaximalTargetExponent >= w.E + ten_mk.E + DiyFp.kSignificandSize)); // 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 scaled_w = DiyFp.Times(ref w, ref ten_mk); w.Multiply(ref ten_mk); Debug.Assert(w.E == boundary_plus.E + ten_mk.E + DiyFp.kSignificandSize); // 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 scaled_boundary_minus = DiyFp.Times(ref boundary_minus, ref ten_mk); boundary_minus.Multiply(ref ten_mk); //DiyFp scaled_boundary_plus = DiyFp.Times(ref boundary_plus, ref ten_mk); boundary_plus.Multiply(ref ten_mk); // 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. int kappa; bool result = DigitGen(ref boundary_minus, ref w, ref boundary_plus, buffer, out length, out kappa); decimal_exponent = -mk + kappa; return(result); }