00001 // Copyright 2010 the V8 project authors. All rights reserved. 00002 // Redistribution and use in source and binary forms, with or without 00003 // modification, are permitted provided that the following conditions are 00004 // met: 00005 // 00006 // * Redistributions of source code must retain the above copyright 00007 // notice, this list of conditions and the following disclaimer. 00008 // * Redistributions in binary form must reproduce the above 00009 // copyright notice, this list of conditions and the following 00010 // disclaimer in the documentation and/or other materials provided 00011 // with the distribution. 00012 // * Neither the name of Google Inc. nor the names of its 00013 // contributors may be used to endorse or promote products derived 00014 // from this software without specific prior written permission. 00015 // 00016 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00017 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00018 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00019 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 00020 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00021 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00022 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00023 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00024 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00025 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00026 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00027 00028 #include <cmath> 00029 00030 #include "bignum-dtoa.h" 00031 00032 #include "bignum.h" 00033 #include "ieee.h" 00034 00035 namespace double_conversion { 00036 00037 static int NormalizedExponent(uint64_t significand, int exponent) { 00038 ASSERT(significand != 0); 00039 while ((significand & Double::kHiddenBit) == 0) { 00040 significand = significand << 1; 00041 exponent = exponent - 1; 00042 } 00043 return exponent; 00044 } 00045 00046 00047 // Forward declarations: 00048 // Returns an estimation of k such that 10^(k-1) <= v < 10^k. 00049 static int EstimatePower(int exponent); 00050 // Computes v / 10^estimated_power exactly, as a ratio of two bignums, numerator 00051 // and denominator. 00052 static void InitialScaledStartValues(uint64_t significand, 00053 int exponent, 00054 bool lower_boundary_is_closer, 00055 int estimated_power, 00056 bool need_boundary_deltas, 00057 Bignum* numerator, 00058 Bignum* denominator, 00059 Bignum* delta_minus, 00060 Bignum* delta_plus); 00061 // Multiplies numerator/denominator so that its values lies in the range 1-10. 00062 // Returns decimal_point s.t. 00063 // v = numerator'/denominator' * 10^(decimal_point-1) 00064 // where numerator' and denominator' are the values of numerator and 00065 // denominator after the call to this function. 00066 static void FixupMultiply10(int estimated_power, bool is_even, 00067 int* decimal_point, 00068 Bignum* numerator, Bignum* denominator, 00069 Bignum* delta_minus, Bignum* delta_plus); 00070 // Generates digits from the left to the right and stops when the generated 00071 // digits yield the shortest decimal representation of v. 00072 static void GenerateShortestDigits(Bignum* numerator, Bignum* denominator, 00073 Bignum* delta_minus, Bignum* delta_plus, 00074 bool is_even, 00075 Vector<char> buffer, int* length); 00076 // Generates 'requested_digits' after the decimal point. 00077 static void BignumToFixed(int requested_digits, int* decimal_point, 00078 Bignum* numerator, Bignum* denominator, 00079 Vector<char>(buffer), int* length); 00080 // Generates 'count' digits of numerator/denominator. 00081 // Once 'count' digits have been produced rounds the result depending on the 00082 // remainder (remainders of exactly .5 round upwards). Might update the 00083 // decimal_point when rounding up (for example for 0.9999). 00084 static void GenerateCountedDigits(int count, int* decimal_point, 00085 Bignum* numerator, Bignum* denominator, 00086 Vector<char>(buffer), int* length); 00087 00088 00089 void BignumDtoa(double v, BignumDtoaMode mode, int requested_digits, 00090 Vector<char> buffer, int* length, int* decimal_point) { 00091 ASSERT(v > 0); 00092 ASSERT(!Double(v).IsSpecial()); 00093 uint64_t significand; 00094 int exponent; 00095 bool lower_boundary_is_closer; 00096 if (mode == BIGNUM_DTOA_SHORTEST_SINGLE) { 00097 float f = static_cast<float>(v); 00098 ASSERT(f == v); 00099 significand = Single(f).Significand(); 00100 exponent = Single(f).Exponent(); 00101 lower_boundary_is_closer = Single(f).LowerBoundaryIsCloser(); 00102 } else { 00103 significand = Double(v).Significand(); 00104 exponent = Double(v).Exponent(); 00105 lower_boundary_is_closer = Double(v).LowerBoundaryIsCloser(); 00106 } 00107 bool need_boundary_deltas = 00108 (mode == BIGNUM_DTOA_SHORTEST || mode == BIGNUM_DTOA_SHORTEST_SINGLE); 00109 00110 bool is_even = (significand & 1) == 0; 00111 int normalized_exponent = NormalizedExponent(significand, exponent); 00112 // estimated_power might be too low by 1. 00113 int estimated_power = EstimatePower(normalized_exponent); 00114 00115 // Shortcut for Fixed. 00116 // The requested digits correspond to the digits after the point. If the 00117 // number is much too small, then there is no need in trying to get any 00118 // digits. 00119 if (mode == BIGNUM_DTOA_FIXED && -estimated_power - 1 > requested_digits) { 00120 buffer[0] = '\0'; 00121 *length = 0; 00122 // Set decimal-point to -requested_digits. This is what Gay does. 00123 // Note that it should not have any effect anyways since the string is 00124 // empty. 00125 *decimal_point = -requested_digits; 00126 return; 00127 } 00128 00129 Bignum numerator; 00130 Bignum denominator; 00131 Bignum delta_minus; 00132 Bignum delta_plus; 00133 // Make sure the bignum can grow large enough. The smallest double equals 00134 // 4e-324. In this case the denominator needs fewer than 324*4 binary digits. 00135 // The maximum double is 1.7976931348623157e308 which needs fewer than 00136 // 308*4 binary digits. 00137 ASSERT(Bignum::kMaxSignificantBits >= 324*4); 00138 InitialScaledStartValues(significand, exponent, lower_boundary_is_closer, 00139 estimated_power, need_boundary_deltas, 00140 &numerator, &denominator, 00141 &delta_minus, &delta_plus); 00142 // We now have v = (numerator / denominator) * 10^estimated_power. 00143 FixupMultiply10(estimated_power, is_even, decimal_point, 00144 &numerator, &denominator, 00145 &delta_minus, &delta_plus); 00146 // We now have v = (numerator / denominator) * 10^(decimal_point-1), and 00147 // 1 <= (numerator + delta_plus) / denominator < 10 00148 switch (mode) { 00149 case BIGNUM_DTOA_SHORTEST: 00150 case BIGNUM_DTOA_SHORTEST_SINGLE: 00151 GenerateShortestDigits(&numerator, &denominator, 00152 &delta_minus, &delta_plus, 00153 is_even, buffer, length); 00154 break; 00155 case BIGNUM_DTOA_FIXED: 00156 BignumToFixed(requested_digits, decimal_point, 00157 &numerator, &denominator, 00158 buffer, length); 00159 break; 00160 case BIGNUM_DTOA_PRECISION: 00161 GenerateCountedDigits(requested_digits, decimal_point, 00162 &numerator, &denominator, 00163 buffer, length); 00164 break; 00165 default: 00166 UNREACHABLE(); 00167 } 00168 buffer[*length] = '\0'; 00169 } 00170 00171 00172 // The procedure starts generating digits from the left to the right and stops 00173 // when the generated digits yield the shortest decimal representation of v. A 00174 // decimal representation of v is a number lying closer to v than to any other 00175 // double, so it converts to v when read. 00176 // 00177 // This is true if d, the decimal representation, is between m- and m+, the 00178 // upper and lower boundaries. d must be strictly between them if !is_even. 00179 // m- := (numerator - delta_minus) / denominator 00180 // m+ := (numerator + delta_plus) / denominator 00181 // 00182 // Precondition: 0 <= (numerator+delta_plus) / denominator < 10. 00183 // If 1 <= (numerator+delta_plus) / denominator < 10 then no leading 0 digit 00184 // will be produced. This should be the standard precondition. 00185 static void GenerateShortestDigits(Bignum* numerator, Bignum* denominator, 00186 Bignum* delta_minus, Bignum* delta_plus, 00187 bool is_even, 00188 Vector<char> buffer, int* length) { 00189 // Small optimization: if delta_minus and delta_plus are the same just reuse 00190 // one of the two bignums. 00191 if (Bignum::Equal(*delta_minus, *delta_plus)) { 00192 delta_plus = delta_minus; 00193 } 00194 *length = 0; 00195 while (true) { 00196 uint16_t digit; 00197 digit = numerator->DivideModuloIntBignum(*denominator); 00198 ASSERT(digit <= 9); // digit is a uint16_t and therefore always positive. 00199 // digit = numerator / denominator (integer division). 00200 // numerator = numerator % denominator. 00201 buffer[(*length)++] = digit + '0'; 00202 00203 // Can we stop already? 00204 // If the remainder of the division is less than the distance to the lower 00205 // boundary we can stop. In this case we simply round down (discarding the 00206 // remainder). 00207 // Similarly we test if we can round up (using the upper boundary). 00208 bool in_delta_room_minus; 00209 bool in_delta_room_plus; 00210 if (is_even) { 00211 in_delta_room_minus = Bignum::LessEqual(*numerator, *delta_minus); 00212 } else { 00213 in_delta_room_minus = Bignum::Less(*numerator, *delta_minus); 00214 } 00215 if (is_even) { 00216 in_delta_room_plus = 00217 Bignum::PlusCompare(*numerator, *delta_plus, *denominator) >= 0; 00218 } else { 00219 in_delta_room_plus = 00220 Bignum::PlusCompare(*numerator, *delta_plus, *denominator) > 0; 00221 } 00222 if (!in_delta_room_minus && !in_delta_room_plus) { 00223 // Prepare for next iteration. 00224 numerator->Times10(); 00225 delta_minus->Times10(); 00226 // We optimized delta_plus to be equal to delta_minus (if they share the 00227 // same value). So don't multiply delta_plus if they point to the same 00228 // object. 00229 if (delta_minus != delta_plus) { 00230 delta_plus->Times10(); 00231 } 00232 } else if (in_delta_room_minus && in_delta_room_plus) { 00233 // Let's see if 2*numerator < denominator. 00234 // If yes, then the next digit would be < 5 and we can round down. 00235 int compare = Bignum::PlusCompare(*numerator, *numerator, *denominator); 00236 if (compare < 0) { 00237 // Remaining digits are less than .5. -> Round down (== do nothing). 00238 } else if (compare > 0) { 00239 // Remaining digits are more than .5 of denominator. -> Round up. 00240 // Note that the last digit could not be a '9' as otherwise the whole 00241 // loop would have stopped earlier. 00242 // We still have an assert here in case the preconditions were not 00243 // satisfied. 00244 ASSERT(buffer[(*length) - 1] != '9'); 00245 buffer[(*length) - 1]++; 00246 } else { 00247 // Halfway case. 00248 // TODO(floitsch): need a way to solve half-way cases. 00249 // For now let's round towards even (since this is what Gay seems to 00250 // do). 00251 00252 if ((buffer[(*length) - 1] - '0') % 2 == 0) { 00253 // Round down => Do nothing. 00254 } else { 00255 ASSERT(buffer[(*length) - 1] != '9'); 00256 buffer[(*length) - 1]++; 00257 } 00258 } 00259 return; 00260 } else if (in_delta_room_minus) { 00261 // Round down (== do nothing). 00262 return; 00263 } else { // in_delta_room_plus 00264 // Round up. 00265 // Note again that the last digit could not be '9' since this would have 00266 // stopped the loop earlier. 00267 // We still have an ASSERT here, in case the preconditions were not 00268 // satisfied. 00269 ASSERT(buffer[(*length) -1] != '9'); 00270 buffer[(*length) - 1]++; 00271 return; 00272 } 00273 } 00274 } 00275 00276 00277 // Let v = numerator / denominator < 10. 00278 // Then we generate 'count' digits of d = x.xxxxx... (without the decimal point) 00279 // from left to right. Once 'count' digits have been produced we decide wether 00280 // to round up or down. Remainders of exactly .5 round upwards. Numbers such 00281 // as 9.999999 propagate a carry all the way, and change the 00282 // exponent (decimal_point), when rounding upwards. 00283 static void GenerateCountedDigits(int count, int* decimal_point, 00284 Bignum* numerator, Bignum* denominator, 00285 Vector<char>(buffer), int* length) { 00286 ASSERT(count >= 0); 00287 for (int i = 0; i < count - 1; ++i) { 00288 uint16_t digit; 00289 digit = numerator->DivideModuloIntBignum(*denominator); 00290 ASSERT(digit <= 9); // digit is a uint16_t and therefore always positive. 00291 // digit = numerator / denominator (integer division). 00292 // numerator = numerator % denominator. 00293 buffer[i] = digit + '0'; 00294 // Prepare for next iteration. 00295 numerator->Times10(); 00296 } 00297 // Generate the last digit. 00298 uint16_t digit; 00299 digit = numerator->DivideModuloIntBignum(*denominator); 00300 if (Bignum::PlusCompare(*numerator, *numerator, *denominator) >= 0) { 00301 digit++; 00302 } 00303 buffer[count - 1] = digit + '0'; 00304 // Correct bad digits (in case we had a sequence of '9's). Propagate the 00305 // carry until we hat a non-'9' or til we reach the first digit. 00306 for (int i = count - 1; i > 0; --i) { 00307 if (buffer[i] != '0' + 10) break; 00308 buffer[i] = '0'; 00309 buffer[i - 1]++; 00310 } 00311 if (buffer[0] == '0' + 10) { 00312 // Propagate a carry past the top place. 00313 buffer[0] = '1'; 00314 (*decimal_point)++; 00315 } 00316 *length = count; 00317 } 00318 00319 00320 // Generates 'requested_digits' after the decimal point. It might omit 00321 // trailing '0's. If the input number is too small then no digits at all are 00322 // generated (ex.: 2 fixed digits for 0.00001). 00323 // 00324 // Input verifies: 1 <= (numerator + delta) / denominator < 10. 00325 static void BignumToFixed(int requested_digits, int* decimal_point, 00326 Bignum* numerator, Bignum* denominator, 00327 Vector<char>(buffer), int* length) { 00328 // Note that we have to look at more than just the requested_digits, since 00329 // a number could be rounded up. Example: v=0.5 with requested_digits=0. 00330 // Even though the power of v equals 0 we can't just stop here. 00331 if (-(*decimal_point) > requested_digits) { 00332 // The number is definitively too small. 00333 // Ex: 0.001 with requested_digits == 1. 00334 // Set decimal-point to -requested_digits. This is what Gay does. 00335 // Note that it should not have any effect anyways since the string is 00336 // empty. 00337 *decimal_point = -requested_digits; 00338 *length = 0; 00339 return; 00340 } else if (-(*decimal_point) == requested_digits) { 00341 // We only need to verify if the number rounds down or up. 00342 // Ex: 0.04 and 0.06 with requested_digits == 1. 00343 ASSERT(*decimal_point == -requested_digits); 00344 // Initially the fraction lies in range (1, 10]. Multiply the denominator 00345 // by 10 so that we can compare more easily. 00346 denominator->Times10(); 00347 if (Bignum::PlusCompare(*numerator, *numerator, *denominator) >= 0) { 00348 // If the fraction is >= 0.5 then we have to include the rounded 00349 // digit. 00350 buffer[0] = '1'; 00351 *length = 1; 00352 (*decimal_point)++; 00353 } else { 00354 // Note that we caught most of similar cases earlier. 00355 *length = 0; 00356 } 00357 return; 00358 } else { 00359 // The requested digits correspond to the digits after the point. 00360 // The variable 'needed_digits' includes the digits before the point. 00361 int needed_digits = (*decimal_point) + requested_digits; 00362 GenerateCountedDigits(needed_digits, decimal_point, 00363 numerator, denominator, 00364 buffer, length); 00365 } 00366 } 00367 00368 00369 // Returns an estimation of k such that 10^(k-1) <= v < 10^k where 00370 // v = f * 2^exponent and 2^52 <= f < 2^53. 00371 // v is hence a normalized double with the given exponent. The output is an 00372 // approximation for the exponent of the decimal approimation .digits * 10^k. 00373 // 00374 // The result might undershoot by 1 in which case 10^k <= v < 10^k+1. 00375 // Note: this property holds for v's upper boundary m+ too. 00376 // 10^k <= m+ < 10^k+1. 00377 // (see explanation below). 00378 // 00379 // Examples: 00380 // EstimatePower(0) => 16 00381 // EstimatePower(-52) => 0 00382 // 00383 // Note: e >= 0 => EstimatedPower(e) > 0. No similar claim can be made for e<0. 00384 static int EstimatePower(int exponent) { 00385 // This function estimates log10 of v where v = f*2^e (with e == exponent). 00386 // Note that 10^floor(log10(v)) <= v, but v <= 10^ceil(log10(v)). 00387 // Note that f is bounded by its container size. Let p = 53 (the double's 00388 // significand size). Then 2^(p-1) <= f < 2^p. 00389 // 00390 // Given that log10(v) == log2(v)/log2(10) and e+(len(f)-1) is quite close 00391 // to log2(v) the function is simplified to (e+(len(f)-1)/log2(10)). 00392 // The computed number undershoots by less than 0.631 (when we compute log3 00393 // and not log10). 00394 // 00395 // Optimization: since we only need an approximated result this computation 00396 // can be performed on 64 bit integers. On x86/x64 architecture the speedup is 00397 // not really measurable, though. 00398 // 00399 // Since we want to avoid overshooting we decrement by 1e10 so that 00400 // floating-point imprecisions don't affect us. 00401 // 00402 // Explanation for v's boundary m+: the computation takes advantage of 00403 // the fact that 2^(p-1) <= f < 2^p. Boundaries still satisfy this requirement 00404 // (even for denormals where the delta can be much more important). 00405 00406 const double k1Log10 = 0.30102999566398114; // 1/lg(10) 00407 00408 // For doubles len(f) == 53 (don't forget the hidden bit). 00409 const int kSignificandSize = Double::kSignificandSize; 00410 double estimate = ceil((exponent + kSignificandSize - 1) * k1Log10 - 1e-10); 00411 return static_cast<int>(estimate); 00412 } 00413 00414 00415 // See comments for InitialScaledStartValues. 00416 static void InitialScaledStartValuesPositiveExponent( 00417 uint64_t significand, int exponent, 00418 int estimated_power, bool need_boundary_deltas, 00419 Bignum* numerator, Bignum* denominator, 00420 Bignum* delta_minus, Bignum* delta_plus) { 00421 // A positive exponent implies a positive power. 00422 ASSERT(estimated_power >= 0); 00423 // Since the estimated_power is positive we simply multiply the denominator 00424 // by 10^estimated_power. 00425 00426 // numerator = v. 00427 numerator->AssignUInt64(significand); 00428 numerator->ShiftLeft(exponent); 00429 // denominator = 10^estimated_power. 00430 denominator->AssignPowerUInt16(10, estimated_power); 00431 00432 if (need_boundary_deltas) { 00433 // Introduce a common denominator so that the deltas to the boundaries are 00434 // integers. 00435 denominator->ShiftLeft(1); 00436 numerator->ShiftLeft(1); 00437 // Let v = f * 2^e, then m+ - v = 1/2 * 2^e; With the common 00438 // denominator (of 2) delta_plus equals 2^e. 00439 delta_plus->AssignUInt16(1); 00440 delta_plus->ShiftLeft(exponent); 00441 // Same for delta_minus. The adjustments if f == 2^p-1 are done later. 00442 delta_minus->AssignUInt16(1); 00443 delta_minus->ShiftLeft(exponent); 00444 } 00445 } 00446 00447 00448 // See comments for InitialScaledStartValues 00449 static void InitialScaledStartValuesNegativeExponentPositivePower( 00450 uint64_t significand, int exponent, 00451 int estimated_power, bool need_boundary_deltas, 00452 Bignum* numerator, Bignum* denominator, 00453 Bignum* delta_minus, Bignum* delta_plus) { 00454 // v = f * 2^e with e < 0, and with estimated_power >= 0. 00455 // This means that e is close to 0 (have a look at how estimated_power is 00456 // computed). 00457 00458 // numerator = significand 00459 // since v = significand * 2^exponent this is equivalent to 00460 // numerator = v * / 2^-exponent 00461 numerator->AssignUInt64(significand); 00462 // denominator = 10^estimated_power * 2^-exponent (with exponent < 0) 00463 denominator->AssignPowerUInt16(10, estimated_power); 00464 denominator->ShiftLeft(-exponent); 00465 00466 if (need_boundary_deltas) { 00467 // Introduce a common denominator so that the deltas to the boundaries are 00468 // integers. 00469 denominator->ShiftLeft(1); 00470 numerator->ShiftLeft(1); 00471 // Let v = f * 2^e, then m+ - v = 1/2 * 2^e; With the common 00472 // denominator (of 2) delta_plus equals 2^e. 00473 // Given that the denominator already includes v's exponent the distance 00474 // to the boundaries is simply 1. 00475 delta_plus->AssignUInt16(1); 00476 // Same for delta_minus. The adjustments if f == 2^p-1 are done later. 00477 delta_minus->AssignUInt16(1); 00478 } 00479 } 00480 00481 00482 // See comments for InitialScaledStartValues 00483 static void InitialScaledStartValuesNegativeExponentNegativePower( 00484 uint64_t significand, int exponent, 00485 int estimated_power, bool need_boundary_deltas, 00486 Bignum* numerator, Bignum* denominator, 00487 Bignum* delta_minus, Bignum* delta_plus) { 00488 // Instead of multiplying the denominator with 10^estimated_power we 00489 // multiply all values (numerator and deltas) by 10^-estimated_power. 00490 00491 // Use numerator as temporary container for power_ten. 00492 Bignum* power_ten = numerator; 00493 power_ten->AssignPowerUInt16(10, -estimated_power); 00494 00495 if (need_boundary_deltas) { 00496 // Since power_ten == numerator we must make a copy of 10^estimated_power 00497 // before we complete the computation of the numerator. 00498 // delta_plus = delta_minus = 10^estimated_power 00499 delta_plus->AssignBignum(*power_ten); 00500 delta_minus->AssignBignum(*power_ten); 00501 } 00502 00503 // numerator = significand * 2 * 10^-estimated_power 00504 // since v = significand * 2^exponent this is equivalent to 00505 // numerator = v * 10^-estimated_power * 2 * 2^-exponent. 00506 // Remember: numerator has been abused as power_ten. So no need to assign it 00507 // to itself. 00508 ASSERT(numerator == power_ten); 00509 numerator->MultiplyByUInt64(significand); 00510 00511 // denominator = 2 * 2^-exponent with exponent < 0. 00512 denominator->AssignUInt16(1); 00513 denominator->ShiftLeft(-exponent); 00514 00515 if (need_boundary_deltas) { 00516 // Introduce a common denominator so that the deltas to the boundaries are 00517 // integers. 00518 numerator->ShiftLeft(1); 00519 denominator->ShiftLeft(1); 00520 // With this shift the boundaries have their correct value, since 00521 // delta_plus = 10^-estimated_power, and 00522 // delta_minus = 10^-estimated_power. 00523 // These assignments have been done earlier. 00524 // The adjustments if f == 2^p-1 (lower boundary is closer) are done later. 00525 } 00526 } 00527 00528 00529 // Let v = significand * 2^exponent. 00530 // Computes v / 10^estimated_power exactly, as a ratio of two bignums, numerator 00531 // and denominator. The functions GenerateShortestDigits and 00532 // GenerateCountedDigits will then convert this ratio to its decimal 00533 // representation d, with the required accuracy. 00534 // Then d * 10^estimated_power is the representation of v. 00535 // (Note: the fraction and the estimated_power might get adjusted before 00536 // generating the decimal representation.) 00537 // 00538 // The initial start values consist of: 00539 // - a scaled numerator: s.t. numerator/denominator == v / 10^estimated_power. 00540 // - a scaled (common) denominator. 00541 // optionally (used by GenerateShortestDigits to decide if it has the shortest 00542 // decimal converting back to v): 00543 // - v - m-: the distance to the lower boundary. 00544 // - m+ - v: the distance to the upper boundary. 00545 // 00546 // v, m+, m-, and therefore v - m- and m+ - v all share the same denominator. 00547 // 00548 // Let ep == estimated_power, then the returned values will satisfy: 00549 // v / 10^ep = numerator / denominator. 00550 // v's boundarys m- and m+: 00551 // m- / 10^ep == v / 10^ep - delta_minus / denominator 00552 // m+ / 10^ep == v / 10^ep + delta_plus / denominator 00553 // Or in other words: 00554 // m- == v - delta_minus * 10^ep / denominator; 00555 // m+ == v + delta_plus * 10^ep / denominator; 00556 // 00557 // Since 10^(k-1) <= v < 10^k (with k == estimated_power) 00558 // or 10^k <= v < 10^(k+1) 00559 // we then have 0.1 <= numerator/denominator < 1 00560 // or 1 <= numerator/denominator < 10 00561 // 00562 // It is then easy to kickstart the digit-generation routine. 00563 // 00564 // The boundary-deltas are only filled if the mode equals BIGNUM_DTOA_SHORTEST 00565 // or BIGNUM_DTOA_SHORTEST_SINGLE. 00566 00567 static void InitialScaledStartValues(uint64_t significand, 00568 int exponent, 00569 bool lower_boundary_is_closer, 00570 int estimated_power, 00571 bool need_boundary_deltas, 00572 Bignum* numerator, 00573 Bignum* denominator, 00574 Bignum* delta_minus, 00575 Bignum* delta_plus) { 00576 if (exponent >= 0) { 00577 InitialScaledStartValuesPositiveExponent( 00578 significand, exponent, estimated_power, need_boundary_deltas, 00579 numerator, denominator, delta_minus, delta_plus); 00580 } else if (estimated_power >= 0) { 00581 InitialScaledStartValuesNegativeExponentPositivePower( 00582 significand, exponent, estimated_power, need_boundary_deltas, 00583 numerator, denominator, delta_minus, delta_plus); 00584 } else { 00585 InitialScaledStartValuesNegativeExponentNegativePower( 00586 significand, exponent, estimated_power, need_boundary_deltas, 00587 numerator, denominator, delta_minus, delta_plus); 00588 } 00589 00590 if (need_boundary_deltas && lower_boundary_is_closer) { 00591 // The lower boundary is closer at half the distance of "normal" numbers. 00592 // Increase the common denominator and adapt all but the delta_minus. 00593 denominator->ShiftLeft(1); // *2 00594 numerator->ShiftLeft(1); // *2 00595 delta_plus->ShiftLeft(1); // *2 00596 } 00597 } 00598 00599 00600 // This routine multiplies numerator/denominator so that its values lies in the 00601 // range 1-10. That is after a call to this function we have: 00602 // 1 <= (numerator + delta_plus) /denominator < 10. 00603 // Let numerator the input before modification and numerator' the argument 00604 // after modification, then the output-parameter decimal_point is such that 00605 // numerator / denominator * 10^estimated_power == 00606 // numerator' / denominator' * 10^(decimal_point - 1) 00607 // In some cases estimated_power was too low, and this is already the case. We 00608 // then simply adjust the power so that 10^(k-1) <= v < 10^k (with k == 00609 // estimated_power) but do not touch the numerator or denominator. 00610 // Otherwise the routine multiplies the numerator and the deltas by 10. 00611 static void FixupMultiply10(int estimated_power, bool is_even, 00612 int* decimal_point, 00613 Bignum* numerator, Bignum* denominator, 00614 Bignum* delta_minus, Bignum* delta_plus) { 00615 bool in_range; 00616 if (is_even) { 00617 // For IEEE doubles half-way cases (in decimal system numbers ending with 5) 00618 // are rounded to the closest floating-point number with even significand. 00619 in_range = Bignum::PlusCompare(*numerator, *delta_plus, *denominator) >= 0; 00620 } else { 00621 in_range = Bignum::PlusCompare(*numerator, *delta_plus, *denominator) > 0; 00622 } 00623 if (in_range) { 00624 // Since numerator + delta_plus >= denominator we already have 00625 // 1 <= numerator/denominator < 10. Simply update the estimated_power. 00626 *decimal_point = estimated_power + 1; 00627 } else { 00628 *decimal_point = estimated_power; 00629 numerator->Times10(); 00630 if (Bignum::Equal(*delta_minus, *delta_plus)) { 00631 delta_minus->Times10(); 00632 delta_plus->AssignBignum(*delta_minus); 00633 } else { 00634 delta_minus->Times10(); 00635 delta_plus->Times10(); 00636 } 00637 } 00638 } 00639 00640 } // namespace double_conversion