michael@0: // Copyright 2010 the V8 project authors. All rights reserved. michael@0: // Redistribution and use in source and binary forms, with or without michael@0: // modification, are permitted provided that the following conditions are michael@0: // met: michael@0: // michael@0: // * Redistributions of source code must retain the above copyright michael@0: // notice, this list of conditions and the following disclaimer. michael@0: // * Redistributions in binary form must reproduce the above michael@0: // copyright notice, this list of conditions and the following michael@0: // disclaimer in the documentation and/or other materials provided michael@0: // with the distribution. michael@0: // * Neither the name of Google Inc. nor the names of its michael@0: // contributors may be used to endorse or promote products derived michael@0: // from this software without specific prior written permission. michael@0: // michael@0: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT michael@0: // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, michael@0: // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT michael@0: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE michael@0: // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: michael@0: #include michael@0: michael@0: #include "bignum-dtoa.h" michael@0: michael@0: #include "bignum.h" michael@0: #include "ieee.h" michael@0: michael@0: namespace double_conversion { michael@0: michael@0: static int NormalizedExponent(uint64_t significand, int exponent) { michael@0: ASSERT(significand != 0); michael@0: while ((significand & Double::kHiddenBit) == 0) { michael@0: significand = significand << 1; michael@0: exponent = exponent - 1; michael@0: } michael@0: return exponent; michael@0: } michael@0: michael@0: michael@0: // Forward declarations: michael@0: // Returns an estimation of k such that 10^(k-1) <= v < 10^k. michael@0: static int EstimatePower(int exponent); michael@0: // Computes v / 10^estimated_power exactly, as a ratio of two bignums, numerator michael@0: // and denominator. michael@0: static void InitialScaledStartValues(uint64_t significand, michael@0: int exponent, michael@0: bool lower_boundary_is_closer, michael@0: int estimated_power, michael@0: bool need_boundary_deltas, michael@0: Bignum* numerator, michael@0: Bignum* denominator, michael@0: Bignum* delta_minus, michael@0: Bignum* delta_plus); michael@0: // Multiplies numerator/denominator so that its values lies in the range 1-10. michael@0: // Returns decimal_point s.t. michael@0: // v = numerator'/denominator' * 10^(decimal_point-1) michael@0: // where numerator' and denominator' are the values of numerator and michael@0: // denominator after the call to this function. michael@0: static void FixupMultiply10(int estimated_power, bool is_even, michael@0: int* decimal_point, michael@0: Bignum* numerator, Bignum* denominator, michael@0: Bignum* delta_minus, Bignum* delta_plus); michael@0: // Generates digits from the left to the right and stops when the generated michael@0: // digits yield the shortest decimal representation of v. michael@0: static void GenerateShortestDigits(Bignum* numerator, Bignum* denominator, michael@0: Bignum* delta_minus, Bignum* delta_plus, michael@0: bool is_even, michael@0: Vector buffer, int* length); michael@0: // Generates 'requested_digits' after the decimal point. michael@0: static void BignumToFixed(int requested_digits, int* decimal_point, michael@0: Bignum* numerator, Bignum* denominator, michael@0: Vector(buffer), int* length); michael@0: // Generates 'count' digits of numerator/denominator. michael@0: // Once 'count' digits have been produced rounds the result depending on the michael@0: // remainder (remainders of exactly .5 round upwards). Might update the michael@0: // decimal_point when rounding up (for example for 0.9999). michael@0: static void GenerateCountedDigits(int count, int* decimal_point, michael@0: Bignum* numerator, Bignum* denominator, michael@0: Vector(buffer), int* length); michael@0: michael@0: michael@0: void BignumDtoa(double v, BignumDtoaMode mode, int requested_digits, michael@0: Vector buffer, int* length, int* decimal_point) { michael@0: ASSERT(v > 0); michael@0: ASSERT(!Double(v).IsSpecial()); michael@0: uint64_t significand; michael@0: int exponent; michael@0: bool lower_boundary_is_closer; michael@0: if (mode == BIGNUM_DTOA_SHORTEST_SINGLE) { michael@0: float f = static_cast(v); michael@0: ASSERT(f == v); michael@0: significand = Single(f).Significand(); michael@0: exponent = Single(f).Exponent(); michael@0: lower_boundary_is_closer = Single(f).LowerBoundaryIsCloser(); michael@0: } else { michael@0: significand = Double(v).Significand(); michael@0: exponent = Double(v).Exponent(); michael@0: lower_boundary_is_closer = Double(v).LowerBoundaryIsCloser(); michael@0: } michael@0: bool need_boundary_deltas = michael@0: (mode == BIGNUM_DTOA_SHORTEST || mode == BIGNUM_DTOA_SHORTEST_SINGLE); michael@0: michael@0: bool is_even = (significand & 1) == 0; michael@0: int normalized_exponent = NormalizedExponent(significand, exponent); michael@0: // estimated_power might be too low by 1. michael@0: int estimated_power = EstimatePower(normalized_exponent); michael@0: michael@0: // Shortcut for Fixed. michael@0: // The requested digits correspond to the digits after the point. If the michael@0: // number is much too small, then there is no need in trying to get any michael@0: // digits. michael@0: if (mode == BIGNUM_DTOA_FIXED && -estimated_power - 1 > requested_digits) { michael@0: buffer[0] = '\0'; michael@0: *length = 0; michael@0: // Set decimal-point to -requested_digits. This is what Gay does. michael@0: // Note that it should not have any effect anyways since the string is michael@0: // empty. michael@0: *decimal_point = -requested_digits; michael@0: return; michael@0: } michael@0: michael@0: Bignum numerator; michael@0: Bignum denominator; michael@0: Bignum delta_minus; michael@0: Bignum delta_plus; michael@0: // Make sure the bignum can grow large enough. The smallest double equals michael@0: // 4e-324. In this case the denominator needs fewer than 324*4 binary digits. michael@0: // The maximum double is 1.7976931348623157e308 which needs fewer than michael@0: // 308*4 binary digits. michael@0: ASSERT(Bignum::kMaxSignificantBits >= 324*4); michael@0: InitialScaledStartValues(significand, exponent, lower_boundary_is_closer, michael@0: estimated_power, need_boundary_deltas, michael@0: &numerator, &denominator, michael@0: &delta_minus, &delta_plus); michael@0: // We now have v = (numerator / denominator) * 10^estimated_power. michael@0: FixupMultiply10(estimated_power, is_even, decimal_point, michael@0: &numerator, &denominator, michael@0: &delta_minus, &delta_plus); michael@0: // We now have v = (numerator / denominator) * 10^(decimal_point-1), and michael@0: // 1 <= (numerator + delta_plus) / denominator < 10 michael@0: switch (mode) { michael@0: case BIGNUM_DTOA_SHORTEST: michael@0: case BIGNUM_DTOA_SHORTEST_SINGLE: michael@0: GenerateShortestDigits(&numerator, &denominator, michael@0: &delta_minus, &delta_plus, michael@0: is_even, buffer, length); michael@0: break; michael@0: case BIGNUM_DTOA_FIXED: michael@0: BignumToFixed(requested_digits, decimal_point, michael@0: &numerator, &denominator, michael@0: buffer, length); michael@0: break; michael@0: case BIGNUM_DTOA_PRECISION: michael@0: GenerateCountedDigits(requested_digits, decimal_point, michael@0: &numerator, &denominator, michael@0: buffer, length); michael@0: break; michael@0: default: michael@0: UNREACHABLE(); michael@0: } michael@0: buffer[*length] = '\0'; michael@0: } michael@0: michael@0: michael@0: // The procedure starts generating digits from the left to the right and stops michael@0: // when the generated digits yield the shortest decimal representation of v. A michael@0: // decimal representation of v is a number lying closer to v than to any other michael@0: // double, so it converts to v when read. michael@0: // michael@0: // This is true if d, the decimal representation, is between m- and m+, the michael@0: // upper and lower boundaries. d must be strictly between them if !is_even. michael@0: // m- := (numerator - delta_minus) / denominator michael@0: // m+ := (numerator + delta_plus) / denominator michael@0: // michael@0: // Precondition: 0 <= (numerator+delta_plus) / denominator < 10. michael@0: // If 1 <= (numerator+delta_plus) / denominator < 10 then no leading 0 digit michael@0: // will be produced. This should be the standard precondition. michael@0: static void GenerateShortestDigits(Bignum* numerator, Bignum* denominator, michael@0: Bignum* delta_minus, Bignum* delta_plus, michael@0: bool is_even, michael@0: Vector buffer, int* length) { michael@0: // Small optimization: if delta_minus and delta_plus are the same just reuse michael@0: // one of the two bignums. michael@0: if (Bignum::Equal(*delta_minus, *delta_plus)) { michael@0: delta_plus = delta_minus; michael@0: } michael@0: *length = 0; michael@0: while (true) { michael@0: uint16_t digit; michael@0: digit = numerator->DivideModuloIntBignum(*denominator); michael@0: ASSERT(digit <= 9); // digit is a uint16_t and therefore always positive. michael@0: // digit = numerator / denominator (integer division). michael@0: // numerator = numerator % denominator. michael@0: buffer[(*length)++] = digit + '0'; michael@0: michael@0: // Can we stop already? michael@0: // If the remainder of the division is less than the distance to the lower michael@0: // boundary we can stop. In this case we simply round down (discarding the michael@0: // remainder). michael@0: // Similarly we test if we can round up (using the upper boundary). michael@0: bool in_delta_room_minus; michael@0: bool in_delta_room_plus; michael@0: if (is_even) { michael@0: in_delta_room_minus = Bignum::LessEqual(*numerator, *delta_minus); michael@0: } else { michael@0: in_delta_room_minus = Bignum::Less(*numerator, *delta_minus); michael@0: } michael@0: if (is_even) { michael@0: in_delta_room_plus = michael@0: Bignum::PlusCompare(*numerator, *delta_plus, *denominator) >= 0; michael@0: } else { michael@0: in_delta_room_plus = michael@0: Bignum::PlusCompare(*numerator, *delta_plus, *denominator) > 0; michael@0: } michael@0: if (!in_delta_room_minus && !in_delta_room_plus) { michael@0: // Prepare for next iteration. michael@0: numerator->Times10(); michael@0: delta_minus->Times10(); michael@0: // We optimized delta_plus to be equal to delta_minus (if they share the michael@0: // same value). So don't multiply delta_plus if they point to the same michael@0: // object. michael@0: if (delta_minus != delta_plus) { michael@0: delta_plus->Times10(); michael@0: } michael@0: } else if (in_delta_room_minus && in_delta_room_plus) { michael@0: // Let's see if 2*numerator < denominator. michael@0: // If yes, then the next digit would be < 5 and we can round down. michael@0: int compare = Bignum::PlusCompare(*numerator, *numerator, *denominator); michael@0: if (compare < 0) { michael@0: // Remaining digits are less than .5. -> Round down (== do nothing). michael@0: } else if (compare > 0) { michael@0: // Remaining digits are more than .5 of denominator. -> Round up. michael@0: // Note that the last digit could not be a '9' as otherwise the whole michael@0: // loop would have stopped earlier. michael@0: // We still have an assert here in case the preconditions were not michael@0: // satisfied. michael@0: ASSERT(buffer[(*length) - 1] != '9'); michael@0: buffer[(*length) - 1]++; michael@0: } else { michael@0: // Halfway case. michael@0: // TODO(floitsch): need a way to solve half-way cases. michael@0: // For now let's round towards even (since this is what Gay seems to michael@0: // do). michael@0: michael@0: if ((buffer[(*length) - 1] - '0') % 2 == 0) { michael@0: // Round down => Do nothing. michael@0: } else { michael@0: ASSERT(buffer[(*length) - 1] != '9'); michael@0: buffer[(*length) - 1]++; michael@0: } michael@0: } michael@0: return; michael@0: } else if (in_delta_room_minus) { michael@0: // Round down (== do nothing). michael@0: return; michael@0: } else { // in_delta_room_plus michael@0: // Round up. michael@0: // Note again that the last digit could not be '9' since this would have michael@0: // stopped the loop earlier. michael@0: // We still have an ASSERT here, in case the preconditions were not michael@0: // satisfied. michael@0: ASSERT(buffer[(*length) -1] != '9'); michael@0: buffer[(*length) - 1]++; michael@0: return; michael@0: } michael@0: } michael@0: } michael@0: michael@0: michael@0: // Let v = numerator / denominator < 10. michael@0: // Then we generate 'count' digits of d = x.xxxxx... (without the decimal point) michael@0: // from left to right. Once 'count' digits have been produced we decide wether michael@0: // to round up or down. Remainders of exactly .5 round upwards. Numbers such michael@0: // as 9.999999 propagate a carry all the way, and change the michael@0: // exponent (decimal_point), when rounding upwards. michael@0: static void GenerateCountedDigits(int count, int* decimal_point, michael@0: Bignum* numerator, Bignum* denominator, michael@0: Vector(buffer), int* length) { michael@0: ASSERT(count >= 0); michael@0: for (int i = 0; i < count - 1; ++i) { michael@0: uint16_t digit; michael@0: digit = numerator->DivideModuloIntBignum(*denominator); michael@0: ASSERT(digit <= 9); // digit is a uint16_t and therefore always positive. michael@0: // digit = numerator / denominator (integer division). michael@0: // numerator = numerator % denominator. michael@0: buffer[i] = digit + '0'; michael@0: // Prepare for next iteration. michael@0: numerator->Times10(); michael@0: } michael@0: // Generate the last digit. michael@0: uint16_t digit; michael@0: digit = numerator->DivideModuloIntBignum(*denominator); michael@0: if (Bignum::PlusCompare(*numerator, *numerator, *denominator) >= 0) { michael@0: digit++; michael@0: } michael@0: buffer[count - 1] = digit + '0'; michael@0: // Correct bad digits (in case we had a sequence of '9's). Propagate the michael@0: // carry until we hat a non-'9' or til we reach the first digit. michael@0: for (int i = count - 1; i > 0; --i) { michael@0: if (buffer[i] != '0' + 10) break; michael@0: buffer[i] = '0'; michael@0: buffer[i - 1]++; michael@0: } michael@0: if (buffer[0] == '0' + 10) { michael@0: // Propagate a carry past the top place. michael@0: buffer[0] = '1'; michael@0: (*decimal_point)++; michael@0: } michael@0: *length = count; michael@0: } michael@0: michael@0: michael@0: // Generates 'requested_digits' after the decimal point. It might omit michael@0: // trailing '0's. If the input number is too small then no digits at all are michael@0: // generated (ex.: 2 fixed digits for 0.00001). michael@0: // michael@0: // Input verifies: 1 <= (numerator + delta) / denominator < 10. michael@0: static void BignumToFixed(int requested_digits, int* decimal_point, michael@0: Bignum* numerator, Bignum* denominator, michael@0: Vector(buffer), int* length) { michael@0: // Note that we have to look at more than just the requested_digits, since michael@0: // a number could be rounded up. Example: v=0.5 with requested_digits=0. michael@0: // Even though the power of v equals 0 we can't just stop here. michael@0: if (-(*decimal_point) > requested_digits) { michael@0: // The number is definitively too small. michael@0: // Ex: 0.001 with requested_digits == 1. michael@0: // Set decimal-point to -requested_digits. This is what Gay does. michael@0: // Note that it should not have any effect anyways since the string is michael@0: // empty. michael@0: *decimal_point = -requested_digits; michael@0: *length = 0; michael@0: return; michael@0: } else if (-(*decimal_point) == requested_digits) { michael@0: // We only need to verify if the number rounds down or up. michael@0: // Ex: 0.04 and 0.06 with requested_digits == 1. michael@0: ASSERT(*decimal_point == -requested_digits); michael@0: // Initially the fraction lies in range (1, 10]. Multiply the denominator michael@0: // by 10 so that we can compare more easily. michael@0: denominator->Times10(); michael@0: if (Bignum::PlusCompare(*numerator, *numerator, *denominator) >= 0) { michael@0: // If the fraction is >= 0.5 then we have to include the rounded michael@0: // digit. michael@0: buffer[0] = '1'; michael@0: *length = 1; michael@0: (*decimal_point)++; michael@0: } else { michael@0: // Note that we caught most of similar cases earlier. michael@0: *length = 0; michael@0: } michael@0: return; michael@0: } else { michael@0: // The requested digits correspond to the digits after the point. michael@0: // The variable 'needed_digits' includes the digits before the point. michael@0: int needed_digits = (*decimal_point) + requested_digits; michael@0: GenerateCountedDigits(needed_digits, decimal_point, michael@0: numerator, denominator, michael@0: buffer, length); michael@0: } michael@0: } michael@0: michael@0: michael@0: // Returns an estimation of k such that 10^(k-1) <= v < 10^k where michael@0: // v = f * 2^exponent and 2^52 <= f < 2^53. michael@0: // v is hence a normalized double with the given exponent. The output is an michael@0: // approximation for the exponent of the decimal approimation .digits * 10^k. michael@0: // michael@0: // The result might undershoot by 1 in which case 10^k <= v < 10^k+1. michael@0: // Note: this property holds for v's upper boundary m+ too. michael@0: // 10^k <= m+ < 10^k+1. michael@0: // (see explanation below). michael@0: // michael@0: // Examples: michael@0: // EstimatePower(0) => 16 michael@0: // EstimatePower(-52) => 0 michael@0: // michael@0: // Note: e >= 0 => EstimatedPower(e) > 0. No similar claim can be made for e<0. michael@0: static int EstimatePower(int exponent) { michael@0: // This function estimates log10 of v where v = f*2^e (with e == exponent). michael@0: // Note that 10^floor(log10(v)) <= v, but v <= 10^ceil(log10(v)). michael@0: // Note that f is bounded by its container size. Let p = 53 (the double's michael@0: // significand size). Then 2^(p-1) <= f < 2^p. michael@0: // michael@0: // Given that log10(v) == log2(v)/log2(10) and e+(len(f)-1) is quite close michael@0: // to log2(v) the function is simplified to (e+(len(f)-1)/log2(10)). michael@0: // The computed number undershoots by less than 0.631 (when we compute log3 michael@0: // and not log10). michael@0: // michael@0: // Optimization: since we only need an approximated result this computation michael@0: // can be performed on 64 bit integers. On x86/x64 architecture the speedup is michael@0: // not really measurable, though. michael@0: // michael@0: // Since we want to avoid overshooting we decrement by 1e10 so that michael@0: // floating-point imprecisions don't affect us. michael@0: // michael@0: // Explanation for v's boundary m+: the computation takes advantage of michael@0: // the fact that 2^(p-1) <= f < 2^p. Boundaries still satisfy this requirement michael@0: // (even for denormals where the delta can be much more important). michael@0: michael@0: const double k1Log10 = 0.30102999566398114; // 1/lg(10) michael@0: michael@0: // For doubles len(f) == 53 (don't forget the hidden bit). michael@0: const int kSignificandSize = Double::kSignificandSize; michael@0: double estimate = ceil((exponent + kSignificandSize - 1) * k1Log10 - 1e-10); michael@0: return static_cast(estimate); michael@0: } michael@0: michael@0: michael@0: // See comments for InitialScaledStartValues. michael@0: static void InitialScaledStartValuesPositiveExponent( michael@0: uint64_t significand, int exponent, michael@0: int estimated_power, bool need_boundary_deltas, michael@0: Bignum* numerator, Bignum* denominator, michael@0: Bignum* delta_minus, Bignum* delta_plus) { michael@0: // A positive exponent implies a positive power. michael@0: ASSERT(estimated_power >= 0); michael@0: // Since the estimated_power is positive we simply multiply the denominator michael@0: // by 10^estimated_power. michael@0: michael@0: // numerator = v. michael@0: numerator->AssignUInt64(significand); michael@0: numerator->ShiftLeft(exponent); michael@0: // denominator = 10^estimated_power. michael@0: denominator->AssignPowerUInt16(10, estimated_power); michael@0: michael@0: if (need_boundary_deltas) { michael@0: // Introduce a common denominator so that the deltas to the boundaries are michael@0: // integers. michael@0: denominator->ShiftLeft(1); michael@0: numerator->ShiftLeft(1); michael@0: // Let v = f * 2^e, then m+ - v = 1/2 * 2^e; With the common michael@0: // denominator (of 2) delta_plus equals 2^e. michael@0: delta_plus->AssignUInt16(1); michael@0: delta_plus->ShiftLeft(exponent); michael@0: // Same for delta_minus. The adjustments if f == 2^p-1 are done later. michael@0: delta_minus->AssignUInt16(1); michael@0: delta_minus->ShiftLeft(exponent); michael@0: } michael@0: } michael@0: michael@0: michael@0: // See comments for InitialScaledStartValues michael@0: static void InitialScaledStartValuesNegativeExponentPositivePower( michael@0: uint64_t significand, int exponent, michael@0: int estimated_power, bool need_boundary_deltas, michael@0: Bignum* numerator, Bignum* denominator, michael@0: Bignum* delta_minus, Bignum* delta_plus) { michael@0: // v = f * 2^e with e < 0, and with estimated_power >= 0. michael@0: // This means that e is close to 0 (have a look at how estimated_power is michael@0: // computed). michael@0: michael@0: // numerator = significand michael@0: // since v = significand * 2^exponent this is equivalent to michael@0: // numerator = v * / 2^-exponent michael@0: numerator->AssignUInt64(significand); michael@0: // denominator = 10^estimated_power * 2^-exponent (with exponent < 0) michael@0: denominator->AssignPowerUInt16(10, estimated_power); michael@0: denominator->ShiftLeft(-exponent); michael@0: michael@0: if (need_boundary_deltas) { michael@0: // Introduce a common denominator so that the deltas to the boundaries are michael@0: // integers. michael@0: denominator->ShiftLeft(1); michael@0: numerator->ShiftLeft(1); michael@0: // Let v = f * 2^e, then m+ - v = 1/2 * 2^e; With the common michael@0: // denominator (of 2) delta_plus equals 2^e. michael@0: // Given that the denominator already includes v's exponent the distance michael@0: // to the boundaries is simply 1. michael@0: delta_plus->AssignUInt16(1); michael@0: // Same for delta_minus. The adjustments if f == 2^p-1 are done later. michael@0: delta_minus->AssignUInt16(1); michael@0: } michael@0: } michael@0: michael@0: michael@0: // See comments for InitialScaledStartValues michael@0: static void InitialScaledStartValuesNegativeExponentNegativePower( michael@0: uint64_t significand, int exponent, michael@0: int estimated_power, bool need_boundary_deltas, michael@0: Bignum* numerator, Bignum* denominator, michael@0: Bignum* delta_minus, Bignum* delta_plus) { michael@0: // Instead of multiplying the denominator with 10^estimated_power we michael@0: // multiply all values (numerator and deltas) by 10^-estimated_power. michael@0: michael@0: // Use numerator as temporary container for power_ten. michael@0: Bignum* power_ten = numerator; michael@0: power_ten->AssignPowerUInt16(10, -estimated_power); michael@0: michael@0: if (need_boundary_deltas) { michael@0: // Since power_ten == numerator we must make a copy of 10^estimated_power michael@0: // before we complete the computation of the numerator. michael@0: // delta_plus = delta_minus = 10^estimated_power michael@0: delta_plus->AssignBignum(*power_ten); michael@0: delta_minus->AssignBignum(*power_ten); michael@0: } michael@0: michael@0: // numerator = significand * 2 * 10^-estimated_power michael@0: // since v = significand * 2^exponent this is equivalent to michael@0: // numerator = v * 10^-estimated_power * 2 * 2^-exponent. michael@0: // Remember: numerator has been abused as power_ten. So no need to assign it michael@0: // to itself. michael@0: ASSERT(numerator == power_ten); michael@0: numerator->MultiplyByUInt64(significand); michael@0: michael@0: // denominator = 2 * 2^-exponent with exponent < 0. michael@0: denominator->AssignUInt16(1); michael@0: denominator->ShiftLeft(-exponent); michael@0: michael@0: if (need_boundary_deltas) { michael@0: // Introduce a common denominator so that the deltas to the boundaries are michael@0: // integers. michael@0: numerator->ShiftLeft(1); michael@0: denominator->ShiftLeft(1); michael@0: // With this shift the boundaries have their correct value, since michael@0: // delta_plus = 10^-estimated_power, and michael@0: // delta_minus = 10^-estimated_power. michael@0: // These assignments have been done earlier. michael@0: // The adjustments if f == 2^p-1 (lower boundary is closer) are done later. michael@0: } michael@0: } michael@0: michael@0: michael@0: // Let v = significand * 2^exponent. michael@0: // Computes v / 10^estimated_power exactly, as a ratio of two bignums, numerator michael@0: // and denominator. The functions GenerateShortestDigits and michael@0: // GenerateCountedDigits will then convert this ratio to its decimal michael@0: // representation d, with the required accuracy. michael@0: // Then d * 10^estimated_power is the representation of v. michael@0: // (Note: the fraction and the estimated_power might get adjusted before michael@0: // generating the decimal representation.) michael@0: // michael@0: // The initial start values consist of: michael@0: // - a scaled numerator: s.t. numerator/denominator == v / 10^estimated_power. michael@0: // - a scaled (common) denominator. michael@0: // optionally (used by GenerateShortestDigits to decide if it has the shortest michael@0: // decimal converting back to v): michael@0: // - v - m-: the distance to the lower boundary. michael@0: // - m+ - v: the distance to the upper boundary. michael@0: // michael@0: // v, m+, m-, and therefore v - m- and m+ - v all share the same denominator. michael@0: // michael@0: // Let ep == estimated_power, then the returned values will satisfy: michael@0: // v / 10^ep = numerator / denominator. michael@0: // v's boundarys m- and m+: michael@0: // m- / 10^ep == v / 10^ep - delta_minus / denominator michael@0: // m+ / 10^ep == v / 10^ep + delta_plus / denominator michael@0: // Or in other words: michael@0: // m- == v - delta_minus * 10^ep / denominator; michael@0: // m+ == v + delta_plus * 10^ep / denominator; michael@0: // michael@0: // Since 10^(k-1) <= v < 10^k (with k == estimated_power) michael@0: // or 10^k <= v < 10^(k+1) michael@0: // we then have 0.1 <= numerator/denominator < 1 michael@0: // or 1 <= numerator/denominator < 10 michael@0: // michael@0: // It is then easy to kickstart the digit-generation routine. michael@0: // michael@0: // The boundary-deltas are only filled if the mode equals BIGNUM_DTOA_SHORTEST michael@0: // or BIGNUM_DTOA_SHORTEST_SINGLE. michael@0: michael@0: static void InitialScaledStartValues(uint64_t significand, michael@0: int exponent, michael@0: bool lower_boundary_is_closer, michael@0: int estimated_power, michael@0: bool need_boundary_deltas, michael@0: Bignum* numerator, michael@0: Bignum* denominator, michael@0: Bignum* delta_minus, michael@0: Bignum* delta_plus) { michael@0: if (exponent >= 0) { michael@0: InitialScaledStartValuesPositiveExponent( michael@0: significand, exponent, estimated_power, need_boundary_deltas, michael@0: numerator, denominator, delta_minus, delta_plus); michael@0: } else if (estimated_power >= 0) { michael@0: InitialScaledStartValuesNegativeExponentPositivePower( michael@0: significand, exponent, estimated_power, need_boundary_deltas, michael@0: numerator, denominator, delta_minus, delta_plus); michael@0: } else { michael@0: InitialScaledStartValuesNegativeExponentNegativePower( michael@0: significand, exponent, estimated_power, need_boundary_deltas, michael@0: numerator, denominator, delta_minus, delta_plus); michael@0: } michael@0: michael@0: if (need_boundary_deltas && lower_boundary_is_closer) { michael@0: // The lower boundary is closer at half the distance of "normal" numbers. michael@0: // Increase the common denominator and adapt all but the delta_minus. michael@0: denominator->ShiftLeft(1); // *2 michael@0: numerator->ShiftLeft(1); // *2 michael@0: delta_plus->ShiftLeft(1); // *2 michael@0: } michael@0: } michael@0: michael@0: michael@0: // This routine multiplies numerator/denominator so that its values lies in the michael@0: // range 1-10. That is after a call to this function we have: michael@0: // 1 <= (numerator + delta_plus) /denominator < 10. michael@0: // Let numerator the input before modification and numerator' the argument michael@0: // after modification, then the output-parameter decimal_point is such that michael@0: // numerator / denominator * 10^estimated_power == michael@0: // numerator' / denominator' * 10^(decimal_point - 1) michael@0: // In some cases estimated_power was too low, and this is already the case. We michael@0: // then simply adjust the power so that 10^(k-1) <= v < 10^k (with k == michael@0: // estimated_power) but do not touch the numerator or denominator. michael@0: // Otherwise the routine multiplies the numerator and the deltas by 10. michael@0: static void FixupMultiply10(int estimated_power, bool is_even, michael@0: int* decimal_point, michael@0: Bignum* numerator, Bignum* denominator, michael@0: Bignum* delta_minus, Bignum* delta_plus) { michael@0: bool in_range; michael@0: if (is_even) { michael@0: // For IEEE doubles half-way cases (in decimal system numbers ending with 5) michael@0: // are rounded to the closest floating-point number with even significand. michael@0: in_range = Bignum::PlusCompare(*numerator, *delta_plus, *denominator) >= 0; michael@0: } else { michael@0: in_range = Bignum::PlusCompare(*numerator, *delta_plus, *denominator) > 0; michael@0: } michael@0: if (in_range) { michael@0: // Since numerator + delta_plus >= denominator we already have michael@0: // 1 <= numerator/denominator < 10. Simply update the estimated_power. michael@0: *decimal_point = estimated_power + 1; michael@0: } else { michael@0: *decimal_point = estimated_power; michael@0: numerator->Times10(); michael@0: if (Bignum::Equal(*delta_minus, *delta_plus)) { michael@0: delta_minus->Times10(); michael@0: delta_plus->AssignBignum(*delta_minus); michael@0: } else { michael@0: delta_minus->Times10(); michael@0: delta_plus->Times10(); michael@0: } michael@0: } michael@0: } michael@0: michael@0: } // namespace double_conversion