1 // Copyright 2010 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "bignum-dtoa.h"
39 namespace double_conversion {
41 static int NormalizedExponent(uint64_t significand, int exponent) {
42 ASSERT(significand != 0);
43 while ((significand & Double::kHiddenBit) == 0) {
44 significand = significand << 1;
45 exponent = exponent - 1;
51 // Forward declarations:
52 // Returns an estimation of k such that 10^(k-1) <= v < 10^k.
53 static int EstimatePower(int exponent);
54 // Computes v / 10^estimated_power exactly, as a ratio of two bignums, numerator
56 static void InitialScaledStartValues(double v,
58 bool need_boundary_deltas,
63 // Multiplies numerator/denominator so that its values lies in the range 1-10.
64 // Returns decimal_point s.t.
65 // v = numerator'/denominator' * 10^(decimal_point-1)
66 // where numerator' and denominator' are the values of numerator and
67 // denominator after the call to this function.
68 static void FixupMultiply10(int estimated_power, bool is_even,
70 Bignum* numerator, Bignum* denominator,
71 Bignum* delta_minus, Bignum* delta_plus);
72 // Generates digits from the left to the right and stops when the generated
73 // digits yield the shortest decimal representation of v.
74 static void GenerateShortestDigits(Bignum* numerator, Bignum* denominator,
75 Bignum* delta_minus, Bignum* delta_plus,
77 Vector<char> buffer, int* length);
78 // Generates 'requested_digits' after the decimal point.
79 static void BignumToFixed(int requested_digits, int* decimal_point,
80 Bignum* numerator, Bignum* denominator,
81 Vector<char>(buffer), int* length);
82 // Generates 'count' digits of numerator/denominator.
83 // Once 'count' digits have been produced rounds the result depending on the
84 // remainder (remainders of exactly .5 round upwards). Might update the
85 // decimal_point when rounding up (for example for 0.9999).
86 static void GenerateCountedDigits(int count, int* decimal_point,
87 Bignum* numerator, Bignum* denominator,
88 Vector<char>(buffer), int* length);
91 void BignumDtoa(double v, BignumDtoaMode mode, int requested_digits,
92 Vector<char> buffer, int* length, int* decimal_point) {
94 ASSERT(!Double(v).IsSpecial());
95 uint64_t significand = Double(v).Significand();
96 bool is_even = (significand & 1) == 0;
97 int exponent = Double(v).Exponent();
98 int normalized_exponent = NormalizedExponent(significand, exponent);
99 // estimated_power might be too low by 1.
100 int estimated_power = EstimatePower(normalized_exponent);
102 // Shortcut for Fixed.
103 // The requested digits correspond to the digits after the point. If the
104 // number is much too small, then there is no need in trying to get any
106 if (mode == BIGNUM_DTOA_FIXED && -estimated_power - 1 > requested_digits) {
109 // Set decimal-point to -requested_digits. This is what Gay does.
110 // Note that it should not have any effect anyways since the string is
112 *decimal_point = -requested_digits;
120 // Make sure the bignum can grow large enough. The smallest double equals
121 // 4e-324. In this case the denominator needs fewer than 324*4 binary digits.
122 // The maximum double is 1.7976931348623157e308 which needs fewer than
123 // 308*4 binary digits.
124 ASSERT(Bignum::kMaxSignificantBits >= 324*4);
125 bool need_boundary_deltas = (mode == BIGNUM_DTOA_SHORTEST);
126 InitialScaledStartValues(v, estimated_power, need_boundary_deltas,
127 &numerator, &denominator,
128 &delta_minus, &delta_plus);
129 // We now have v = (numerator / denominator) * 10^estimated_power.
130 FixupMultiply10(estimated_power, is_even, decimal_point,
131 &numerator, &denominator,
132 &delta_minus, &delta_plus);
133 // We now have v = (numerator / denominator) * 10^(decimal_point-1), and
134 // 1 <= (numerator + delta_plus) / denominator < 10
136 case BIGNUM_DTOA_SHORTEST:
137 GenerateShortestDigits(&numerator, &denominator,
138 &delta_minus, &delta_plus,
139 is_even, buffer, length);
141 case BIGNUM_DTOA_FIXED:
142 BignumToFixed(requested_digits, decimal_point,
143 &numerator, &denominator,
146 case BIGNUM_DTOA_PRECISION:
147 GenerateCountedDigits(requested_digits, decimal_point,
148 &numerator, &denominator,
154 buffer[*length] = '\0';
158 // The procedure starts generating digits from the left to the right and stops
159 // when the generated digits yield the shortest decimal representation of v. A
160 // decimal representation of v is a number lying closer to v than to any other
161 // double, so it converts to v when read.
163 // This is true if d, the decimal representation, is between m- and m+, the
164 // upper and lower boundaries. d must be strictly between them if !is_even.
165 // m- := (numerator - delta_minus) / denominator
166 // m+ := (numerator + delta_plus) / denominator
168 // Precondition: 0 <= (numerator+delta_plus) / denominator < 10.
169 // If 1 <= (numerator+delta_plus) / denominator < 10 then no leading 0 digit
170 // will be produced. This should be the standard precondition.
171 static void GenerateShortestDigits(Bignum* numerator, Bignum* denominator,
172 Bignum* delta_minus, Bignum* delta_plus,
174 Vector<char> buffer, int* length) {
175 // Small optimization: if delta_minus and delta_plus are the same just reuse
176 // one of the two bignums.
177 if (Bignum::Equal(*delta_minus, *delta_plus)) {
178 delta_plus = delta_minus;
183 digit = numerator->DivideModuloIntBignum(*denominator);
184 ASSERT(digit <= 9); // digit is a uint16_t and therefore always positive.
185 // digit = numerator / denominator (integer division).
186 // numerator = numerator % denominator.
187 buffer[(*length)++] = digit + '0';
189 // Can we stop already?
190 // If the remainder of the division is less than the distance to the lower
191 // boundary we can stop. In this case we simply round down (discarding the
193 // Similarly we test if we can round up (using the upper boundary).
194 bool in_delta_room_minus;
195 bool in_delta_room_plus;
197 in_delta_room_minus = Bignum::LessEqual(*numerator, *delta_minus);
199 in_delta_room_minus = Bignum::Less(*numerator, *delta_minus);
203 Bignum::PlusCompare(*numerator, *delta_plus, *denominator) >= 0;
206 Bignum::PlusCompare(*numerator, *delta_plus, *denominator) > 0;
208 if (!in_delta_room_minus && !in_delta_room_plus) {
209 // Prepare for next iteration.
210 numerator->Times10();
211 delta_minus->Times10();
212 // We optimized delta_plus to be equal to delta_minus (if they share the
213 // same value). So don't multiply delta_plus if they point to the same
215 if (delta_minus != delta_plus) {
216 delta_plus->Times10();
218 } else if (in_delta_room_minus && in_delta_room_plus) {
219 // Let's see if 2*numerator < denominator.
220 // If yes, then the next digit would be < 5 and we can round down.
221 int compare = Bignum::PlusCompare(*numerator, *numerator, *denominator);
223 // Remaining digits are less than .5. -> Round down (== do nothing).
224 } else if (compare > 0) {
225 // Remaining digits are more than .5 of denominator. -> Round up.
226 // Note that the last digit could not be a '9' as otherwise the whole
227 // loop would have stopped earlier.
228 // We still have an assert here in case the preconditions were not
230 ASSERT(buffer[(*length) - 1] != '9');
231 buffer[(*length) - 1]++;
234 // TODO(floitsch): need a way to solve half-way cases.
235 // For now let's round towards even (since this is what Gay seems to
238 if ((buffer[(*length) - 1] - '0') % 2 == 0) {
239 // Round down => Do nothing.
241 ASSERT(buffer[(*length) - 1] != '9');
242 buffer[(*length) - 1]++;
246 } else if (in_delta_room_minus) {
247 // Round down (== do nothing).
249 } else { // in_delta_room_plus
251 // Note again that the last digit could not be '9' since this would have
252 // stopped the loop earlier.
253 // We still have an ASSERT here, in case the preconditions were not
255 ASSERT(buffer[(*length) -1] != '9');
256 buffer[(*length) - 1]++;
263 // Let v = numerator / denominator < 10.
264 // Then we generate 'count' digits of d = x.xxxxx... (without the decimal point)
265 // from left to right. Once 'count' digits have been produced we decide wether
266 // to round up or down. Remainders of exactly .5 round upwards. Numbers such
267 // as 9.999999 propagate a carry all the way, and change the
268 // exponent (decimal_point), when rounding upwards.
269 static void GenerateCountedDigits(int count, int* decimal_point,
270 Bignum* numerator, Bignum* denominator,
271 Vector<char>(buffer), int* length) {
273 for (int i = 0; i < count - 1; ++i) {
275 digit = numerator->DivideModuloIntBignum(*denominator);
276 ASSERT(digit <= 9); // digit is a uint16_t and therefore always positive.
277 // digit = numerator / denominator (integer division).
278 // numerator = numerator % denominator.
279 buffer[i] = digit + '0';
280 // Prepare for next iteration.
281 numerator->Times10();
283 // Generate the last digit.
285 digit = numerator->DivideModuloIntBignum(*denominator);
286 if (Bignum::PlusCompare(*numerator, *numerator, *denominator) >= 0) {
289 buffer[count - 1] = digit + '0';
290 // Correct bad digits (in case we had a sequence of '9's). Propagate the
291 // carry until we hat a non-'9' or til we reach the first digit.
292 for (int i = count - 1; i > 0; --i) {
293 if (buffer[i] != '0' + 10) break;
297 if (buffer[0] == '0' + 10) {
298 // Propagate a carry past the top place.
306 // Generates 'requested_digits' after the decimal point. It might omit
307 // trailing '0's. If the input number is too small then no digits at all are
308 // generated (ex.: 2 fixed digits for 0.00001).
310 // Input verifies: 1 <= (numerator + delta) / denominator < 10.
311 static void BignumToFixed(int requested_digits, int* decimal_point,
312 Bignum* numerator, Bignum* denominator,
313 Vector<char>(buffer), int* length) {
314 // Note that we have to look at more than just the requested_digits, since
315 // a number could be rounded up. Example: v=0.5 with requested_digits=0.
316 // Even though the power of v equals 0 we can't just stop here.
317 if (-(*decimal_point) > requested_digits) {
318 // The number is definitively too small.
319 // Ex: 0.001 with requested_digits == 1.
320 // Set decimal-point to -requested_digits. This is what Gay does.
321 // Note that it should not have any effect anyways since the string is
323 *decimal_point = -requested_digits;
326 } else if (-(*decimal_point) == requested_digits) {
327 // We only need to verify if the number rounds down or up.
328 // Ex: 0.04 and 0.06 with requested_digits == 1.
329 ASSERT(*decimal_point == -requested_digits);
330 // Initially the fraction lies in range (1, 10]. Multiply the denominator
331 // by 10 so that we can compare more easily.
332 denominator->Times10();
333 if (Bignum::PlusCompare(*numerator, *numerator, *denominator) >= 0) {
334 // If the fraction is >= 0.5 then we have to include the rounded
340 // Note that we caught most of similar cases earlier.
345 // The requested digits correspond to the digits after the point.
346 // The variable 'needed_digits' includes the digits before the point.
347 int needed_digits = (*decimal_point) + requested_digits;
348 GenerateCountedDigits(needed_digits, decimal_point,
349 numerator, denominator,
355 // Returns an estimation of k such that 10^(k-1) <= v < 10^k where
356 // v = f * 2^exponent and 2^52 <= f < 2^53.
357 // v is hence a normalized double with the given exponent. The output is an
358 // approximation for the exponent of the decimal approimation .digits * 10^k.
360 // The result might undershoot by 1 in which case 10^k <= v < 10^k+1.
361 // Note: this property holds for v's upper boundary m+ too.
362 // 10^k <= m+ < 10^k+1.
363 // (see explanation below).
366 // EstimatePower(0) => 16
367 // EstimatePower(-52) => 0
369 // Note: e >= 0 => EstimatedPower(e) > 0. No similar claim can be made for e<0.
370 static int EstimatePower(int exponent) {
371 // This function estimates log10 of v where v = f*2^e (with e == exponent).
372 // Note that 10^floor(log10(v)) <= v, but v <= 10^ceil(log10(v)).
373 // Note that f is bounded by its container size. Let p = 53 (the double's
374 // significand size). Then 2^(p-1) <= f < 2^p.
376 // Given that log10(v) == log2(v)/log2(10) and e+(len(f)-1) is quite close
377 // to log2(v) the function is simplified to (e+(len(f)-1)/log2(10)).
378 // The computed number undershoots by less than 0.631 (when we compute log3
381 // Optimization: since we only need an approximated result this computation
382 // can be performed on 64 bit integers. On x86/x64 architecture the speedup is
383 // not really measurable, though.
385 // Since we want to avoid overshooting we decrement by 1e10 so that
386 // floating-point imprecisions don't affect us.
388 // Explanation for v's boundary m+: the computation takes advantage of
389 // the fact that 2^(p-1) <= f < 2^p. Boundaries still satisfy this requirement
390 // (even for denormals where the delta can be much more important).
392 const double k1Log10 = 0.30102999566398114; // 1/lg(10)
394 // For doubles len(f) == 53 (don't forget the hidden bit).
395 const int kSignificandSize = 53;
396 double estimate = ceil((exponent + kSignificandSize - 1) * k1Log10 - 1e-10);
397 return static_cast<int>(estimate);
401 // See comments for InitialScaledStartValues.
402 static void InitialScaledStartValuesPositiveExponent(
403 double v, int estimated_power, bool need_boundary_deltas,
404 Bignum* numerator, Bignum* denominator,
405 Bignum* delta_minus, Bignum* delta_plus) {
406 // A positive exponent implies a positive power.
407 ASSERT(estimated_power >= 0);
408 // Since the estimated_power is positive we simply multiply the denominator
409 // by 10^estimated_power.
412 numerator->AssignUInt64(Double(v).Significand());
413 numerator->ShiftLeft(Double(v).Exponent());
414 // denominator = 10^estimated_power.
415 denominator->AssignPowerUInt16(10, estimated_power);
417 if (need_boundary_deltas) {
418 // Introduce a common denominator so that the deltas to the boundaries are
420 denominator->ShiftLeft(1);
421 numerator->ShiftLeft(1);
422 // Let v = f * 2^e, then m+ - v = 1/2 * 2^e; With the common
423 // denominator (of 2) delta_plus equals 2^e.
424 delta_plus->AssignUInt16(1);
425 delta_plus->ShiftLeft(Double(v).Exponent());
426 // Same for delta_minus (with adjustments below if f == 2^p-1).
427 delta_minus->AssignUInt16(1);
428 delta_minus->ShiftLeft(Double(v).Exponent());
430 // If the significand (without the hidden bit) is 0, then the lower
431 // boundary is closer than just half a ulp (unit in the last place).
432 // There is only one exception: if the next lower number is a denormal then
433 // the distance is 1 ulp. This cannot be the case for exponent >= 0 (but we
434 // have to test it in the other function where exponent < 0).
435 uint64_t v_bits = Double(v).AsUint64();
436 if ((v_bits & Double::kSignificandMask) == 0) {
437 // The lower boundary is closer at half the distance of "normal" numbers.
438 // Increase the common denominator and adapt all but the delta_minus.
439 denominator->ShiftLeft(1); // *2
440 numerator->ShiftLeft(1); // *2
441 delta_plus->ShiftLeft(1); // *2
447 // See comments for InitialScaledStartValues
448 static void InitialScaledStartValuesNegativeExponentPositivePower(
449 double v, int estimated_power, bool need_boundary_deltas,
450 Bignum* numerator, Bignum* denominator,
451 Bignum* delta_minus, Bignum* delta_plus) {
452 uint64_t significand = Double(v).Significand();
453 int exponent = Double(v).Exponent();
454 // v = f * 2^e with e < 0, and with estimated_power >= 0.
455 // This means that e is close to 0 (have a look at how estimated_power is
458 // numerator = significand
459 // since v = significand * 2^exponent this is equivalent to
460 // numerator = v * / 2^-exponent
461 numerator->AssignUInt64(significand);
462 // denominator = 10^estimated_power * 2^-exponent (with exponent < 0)
463 denominator->AssignPowerUInt16(10, estimated_power);
464 denominator->ShiftLeft(-exponent);
466 if (need_boundary_deltas) {
467 // Introduce a common denominator so that the deltas to the boundaries are
469 denominator->ShiftLeft(1);
470 numerator->ShiftLeft(1);
471 // Let v = f * 2^e, then m+ - v = 1/2 * 2^e; With the common
472 // denominator (of 2) delta_plus equals 2^e.
473 // Given that the denominator already includes v's exponent the distance
474 // to the boundaries is simply 1.
475 delta_plus->AssignUInt16(1);
476 // Same for delta_minus (with adjustments below if f == 2^p-1).
477 delta_minus->AssignUInt16(1);
479 // If the significand (without the hidden bit) is 0, then the lower
480 // boundary is closer than just one ulp (unit in the last place).
481 // There is only one exception: if the next lower number is a denormal
482 // then the distance is 1 ulp. Since the exponent is close to zero
483 // (otherwise estimated_power would have been negative) this cannot happen
485 uint64_t v_bits = Double(v).AsUint64();
486 if ((v_bits & Double::kSignificandMask) == 0) {
487 // The lower boundary is closer at half the distance of "normal" numbers.
488 // Increase the denominator and adapt all but the delta_minus.
489 denominator->ShiftLeft(1); // *2
490 numerator->ShiftLeft(1); // *2
491 delta_plus->ShiftLeft(1); // *2
497 // See comments for InitialScaledStartValues
498 static void InitialScaledStartValuesNegativeExponentNegativePower(
499 double v, int estimated_power, bool need_boundary_deltas,
500 Bignum* numerator, Bignum* denominator,
501 Bignum* delta_minus, Bignum* delta_plus) {
502 const uint64_t kMinimalNormalizedExponent =
503 UINT64_2PART_C(0x00100000, 00000000);
504 uint64_t significand = Double(v).Significand();
505 int exponent = Double(v).Exponent();
506 // Instead of multiplying the denominator with 10^estimated_power we
507 // multiply all values (numerator and deltas) by 10^-estimated_power.
509 // Use numerator as temporary container for power_ten.
510 Bignum* power_ten = numerator;
511 power_ten->AssignPowerUInt16(10, -estimated_power);
513 if (need_boundary_deltas) {
514 // Since power_ten == numerator we must make a copy of 10^estimated_power
515 // before we complete the computation of the numerator.
516 // delta_plus = delta_minus = 10^estimated_power
517 delta_plus->AssignBignum(*power_ten);
518 delta_minus->AssignBignum(*power_ten);
521 // numerator = significand * 2 * 10^-estimated_power
522 // since v = significand * 2^exponent this is equivalent to
523 // numerator = v * 10^-estimated_power * 2 * 2^-exponent.
524 // Remember: numerator has been abused as power_ten. So no need to assign it
526 ASSERT(numerator == power_ten);
527 numerator->MultiplyByUInt64(significand);
529 // denominator = 2 * 2^-exponent with exponent < 0.
530 denominator->AssignUInt16(1);
531 denominator->ShiftLeft(-exponent);
533 if (need_boundary_deltas) {
534 // Introduce a common denominator so that the deltas to the boundaries are
536 numerator->ShiftLeft(1);
537 denominator->ShiftLeft(1);
538 // With this shift the boundaries have their correct value, since
539 // delta_plus = 10^-estimated_power, and
540 // delta_minus = 10^-estimated_power.
541 // These assignments have been done earlier.
543 // The special case where the lower boundary is twice as close.
544 // This time we have to look out for the exception too.
545 uint64_t v_bits = Double(v).AsUint64();
546 if ((v_bits & Double::kSignificandMask) == 0 &&
547 // The only exception where a significand == 0 has its boundaries at
548 // "normal" distances:
549 (v_bits & Double::kExponentMask) != kMinimalNormalizedExponent) {
550 numerator->ShiftLeft(1); // *2
551 denominator->ShiftLeft(1); // *2
552 delta_plus->ShiftLeft(1); // *2
558 // Let v = significand * 2^exponent.
559 // Computes v / 10^estimated_power exactly, as a ratio of two bignums, numerator
560 // and denominator. The functions GenerateShortestDigits and
561 // GenerateCountedDigits will then convert this ratio to its decimal
562 // representation d, with the required accuracy.
563 // Then d * 10^estimated_power is the representation of v.
564 // (Note: the fraction and the estimated_power might get adjusted before
565 // generating the decimal representation.)
567 // The initial start values consist of:
568 // - a scaled numerator: s.t. numerator/denominator == v / 10^estimated_power.
569 // - a scaled (common) denominator.
570 // optionally (used by GenerateShortestDigits to decide if it has the shortest
571 // decimal converting back to v):
572 // - v - m-: the distance to the lower boundary.
573 // - m+ - v: the distance to the upper boundary.
575 // v, m+, m-, and therefore v - m- and m+ - v all share the same denominator.
577 // Let ep == estimated_power, then the returned values will satisfy:
578 // v / 10^ep = numerator / denominator.
579 // v's boundarys m- and m+:
580 // m- / 10^ep == v / 10^ep - delta_minus / denominator
581 // m+ / 10^ep == v / 10^ep + delta_plus / denominator
582 // Or in other words:
583 // m- == v - delta_minus * 10^ep / denominator;
584 // m+ == v + delta_plus * 10^ep / denominator;
586 // Since 10^(k-1) <= v < 10^k (with k == estimated_power)
587 // or 10^k <= v < 10^(k+1)
588 // we then have 0.1 <= numerator/denominator < 1
589 // or 1 <= numerator/denominator < 10
591 // It is then easy to kickstart the digit-generation routine.
593 // The boundary-deltas are only filled if need_boundary_deltas is set.
594 static void InitialScaledStartValues(double v,
596 bool need_boundary_deltas,
600 Bignum* delta_plus) {
601 if (Double(v).Exponent() >= 0) {
602 InitialScaledStartValuesPositiveExponent(
603 v, estimated_power, need_boundary_deltas,
604 numerator, denominator, delta_minus, delta_plus);
605 } else if (estimated_power >= 0) {
606 InitialScaledStartValuesNegativeExponentPositivePower(
607 v, estimated_power, need_boundary_deltas,
608 numerator, denominator, delta_minus, delta_plus);
610 InitialScaledStartValuesNegativeExponentNegativePower(
611 v, estimated_power, need_boundary_deltas,
612 numerator, denominator, delta_minus, delta_plus);
617 // This routine multiplies numerator/denominator so that its values lies in the
618 // range 1-10. That is after a call to this function we have:
619 // 1 <= (numerator + delta_plus) /denominator < 10.
620 // Let numerator the input before modification and numerator' the argument
621 // after modification, then the output-parameter decimal_point is such that
622 // numerator / denominator * 10^estimated_power ==
623 // numerator' / denominator' * 10^(decimal_point - 1)
624 // In some cases estimated_power was too low, and this is already the case. We
625 // then simply adjust the power so that 10^(k-1) <= v < 10^k (with k ==
626 // estimated_power) but do not touch the numerator or denominator.
627 // Otherwise the routine multiplies the numerator and the deltas by 10.
628 static void FixupMultiply10(int estimated_power, bool is_even,
630 Bignum* numerator, Bignum* denominator,
631 Bignum* delta_minus, Bignum* delta_plus) {
634 // For IEEE doubles half-way cases (in decimal system numbers ending with 5)
635 // are rounded to the closest floating-point number with even significand.
636 in_range = Bignum::PlusCompare(*numerator, *delta_plus, *denominator) >= 0;
638 in_range = Bignum::PlusCompare(*numerator, *delta_plus, *denominator) > 0;
641 // Since numerator + delta_plus >= denominator we already have
642 // 1 <= numerator/denominator < 10. Simply update the estimated_power.
643 *decimal_point = estimated_power + 1;
645 *decimal_point = estimated_power;
646 numerator->Times10();
647 if (Bignum::Equal(*delta_minus, *delta_plus)) {
648 delta_minus->Times10();
649 delta_plus->AssignBignum(*delta_minus);
651 delta_minus->Times10();
652 delta_plus->Times10();
657 } // namespace double_conversion