1 /****************************************************************
3 * The author of this software is David M. Gay.
5 * Copyright (c) 1991, 2000, 2001 by Lucent Technologies.
6 * Copyright (C) 2002, 2005, 2006, 2007, 2008, 2010, 2012 Apple Inc. All rights reserved.
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose without fee is hereby granted, provided that this entire notice
10 * is included in all copies of any software which is or includes a copy
11 * or modification of this software and in all copies of the supporting
12 * documentation for such software.
14 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
15 * WARRANTY. IN PARTICULAR, NEITHER THE AUTHOR NOR LUCENT MAKES ANY
16 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
17 * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
19 ***************************************************************/
21 /* Please send bug reports to David M. Gay (dmg at acm dot org,
22 * with " at " changed at "@" and " dot " changed to "."). */
24 /* On a machine with IEEE extended-precision registers, it is
25 * necessary to specify double-precision (53-bit) rounding precision
26 * before invoking strtod or dtoa. If the machine uses (the equivalent
27 * of) Intel 80x87 arithmetic, the call
28 * _control87(PC_53, MCW_PC);
29 * does this with many compilers. Whether this or another call is
30 * appropriate depends on the compiler; for this to work, it may be
31 * necessary to #include "float.h" or another system-dependent header
39 #include <wtf/AlwaysInline.h>
40 #include <wtf/MathExtras.h>
41 #include <wtf/Threading.h>
42 #include <wtf/Vector.h>
45 #pragma warning(disable: 4244)
46 #pragma warning(disable: 4245)
47 #pragma warning(disable: 4554)
59 #if CPU(BIG_ENDIAN) || CPU(MIDDLE_ENDIAN)
60 #define word0(x) (x)->L[0]
61 #define word1(x) (x)->L[1]
63 #define word0(x) (x)->L[1]
64 #define word1(x) (x)->L[0]
66 #define dval(x) (x)->d
68 /* The following definition of Storeinc is appropriate for MIPS processors.
69 * An alternative that might be better on some machines is
70 * *p++ = high << 16 | low & 0xffff;
72 static ALWAYS_INLINE uint32_t* storeInc(uint32_t* p, uint16_t high, uint16_t low)
74 uint16_t* p16 = reinterpret_cast<uint16_t*>(p);
87 #define Exp_msk1 0x100000
88 #define Exp_msk11 0x100000
89 #define Exp_mask 0x7ff00000
93 #define Exp_1 0x3ff00000
94 #define Exp_11 0x3ff00000
96 #define Frac_mask 0xfffff
97 #define Frac_mask1 0xfffff
100 #define Bndry_mask 0xfffff
101 #define Bndry_mask1 0xfffff
103 #define Sign_bit 0x80000000
110 #define rounded_product(a, b) a *= b
111 #define rounded_quotient(a, b) a /= b
113 #define Big0 (Frac_mask1 | Exp_msk1 * (DBL_MAX_EXP + Bias - 1))
114 #define Big1 0xffffffff
116 #if CPU(PPC64) || CPU(X86_64)
117 // FIXME: should we enable this on all 64-bit CPUs?
118 // 64-bit emulation provided by the compiler is likely to be slower than dtoa own code on 32-bit hardware.
119 #define USE_LONG_LONG
123 BigInt() : sign(0) { }
134 return m_words.size();
137 void resize(size_t s)
144 return m_words.data();
147 const uint32_t* words() const
149 return m_words.data();
152 void append(uint32_t w)
157 Vector<uint32_t, 16> m_words;
160 static void multadd(BigInt& b, int m, int a) /* multiply by m and add a */
163 unsigned long long carry;
169 uint32_t* x = b.words();
174 unsigned long long y = *x * (unsigned long long)m + carry;
176 *x++ = (uint32_t)y & 0xffffffffUL;
179 uint32_t y = (xi & 0xffff) * m + carry;
180 uint32_t z = (xi >> 16) * m + (y >> 16);
182 *x++ = (z << 16) + (y & 0xffff);
187 b.append((uint32_t)carry);
190 static int hi0bits(uint32_t x)
194 if (!(x & 0xffff0000)) {
198 if (!(x & 0xff000000)) {
202 if (!(x & 0xf0000000)) {
206 if (!(x & 0xc0000000)) {
210 if (!(x & 0x80000000)) {
212 if (!(x & 0x40000000))
218 static int lo0bits(uint32_t* y)
260 static void i2b(BigInt& b, int i)
267 static void mult(BigInt& aRef, const BigInt& bRef)
269 const BigInt* a = &aRef;
270 const BigInt* b = &bRef;
273 const uint32_t* x = 0;
282 unsigned long long carry, z;
287 if (a->size() < b->size()) {
288 const BigInt* tmp = a;
298 for (xc = c.words(), xa = xc + wc; xc < xa; xc++)
306 for (; xb < xbe; xc0++) {
312 z = *x++ * (unsigned long long)y + *xc + carry;
314 *xc++ = (uint32_t)z & 0xffffffffUL;
316 *xc = (uint32_t)carry;
320 for (; xb < xbe; xb++, xc0++) {
321 if ((y = *xb & 0xffff)) {
326 z = (*x & 0xffff) * y + (*xc & 0xffff) + carry;
328 uint32_t z2 = (*x++ >> 16) * y + (*xc >> 16) + carry;
330 xc = storeInc(xc, z2, z);
334 if ((y = *xb >> 16)) {
340 z = (*x & 0xffff) * y + (*xc >> 16) + carry;
342 xc = storeInc(xc, z, z2);
343 z2 = (*x++ >> 16) * y + (*xc & 0xffff) + carry;
350 for (xc0 = c.words(), xc = xc0 + wc; wc > 0 && !*--xc; --wc) { }
356 WTF_MAKE_NONCOPYABLE(P5Node); WTF_MAKE_FAST_ALLOCATED;
366 static ALWAYS_INLINE void pow5mult(BigInt& b, int k)
368 static int p05[3] = { 5, 25, 125 };
371 multadd(b, p05[i - 1], 0);
376 s_dtoaP5Mutex->lock();
388 int p5sCountLocal = p5sCount;
389 s_dtoaP5Mutex->unlock();
399 if (++p5sUsed == p5sCountLocal) {
400 s_dtoaP5Mutex->lock();
401 if (p5sUsed == p5sCount) {
403 p5->next = new P5Node;
405 p5->next->val = p5->val;
406 mult(p5->next->val, p5->next->val);
410 p5sCountLocal = p5sCount;
411 s_dtoaP5Mutex->unlock();
417 static ALWAYS_INLINE void lshift(BigInt& b, int k)
421 int origSize = b.size();
422 int n1 = n + origSize + 1;
425 b.resize(b.size() + n + 1);
427 b.resize(b.size() + n);
429 const uint32_t* srcStart = b.words();
430 uint32_t* dstStart = b.words();
431 const uint32_t* src = srcStart + origSize - 1;
432 uint32_t* dst = dstStart + n1 - 1;
434 uint32_t hiSubword = 0;
436 for (; src >= srcStart; --src) {
437 *dst-- = hiSubword | *src >> s;
438 hiSubword = *src << k;
441 ASSERT(dst == dstStart + n);
443 b.resize(origSize + n + !!b.words()[n1 - 1]);
448 } while (src >= srcStart);
450 for (dst = dstStart + n; dst != dstStart; )
453 ASSERT(b.size() <= 1 || b.words()[b.size() - 1]);
456 static int cmp(const BigInt& a, const BigInt& b)
458 const uint32_t *xa, *xa0, *xb, *xb0;
463 ASSERT(i <= 1 || a.words()[i - 1]);
464 ASSERT(j <= 1 || b.words()[j - 1]);
473 return *xa < *xb ? -1 : 1;
480 static ALWAYS_INLINE void diff(BigInt& c, const BigInt& aRef, const BigInt& bRef)
482 const BigInt* a = &aRef;
483 const BigInt* b = &bRef;
495 const BigInt* tmp = a;
503 const uint32_t* xa = a->words();
504 const uint32_t* xae = xa + wa;
506 const uint32_t* xb = b->words();
507 const uint32_t* xbe = xb + wb;
513 unsigned long long borrow = 0;
515 unsigned long long y = (unsigned long long)*xa++ - *xb++ - borrow;
516 borrow = y >> 32 & (uint32_t)1;
517 *xc++ = (uint32_t)y & 0xffffffffUL;
520 unsigned long long y = *xa++ - borrow;
521 borrow = y >> 32 & (uint32_t)1;
522 *xc++ = (uint32_t)y & 0xffffffffUL;
527 uint32_t y = (*xa & 0xffff) - (*xb & 0xffff) - borrow;
528 borrow = (y & 0x10000) >> 16;
529 uint32_t z = (*xa++ >> 16) - (*xb++ >> 16) - borrow;
530 borrow = (z & 0x10000) >> 16;
531 xc = storeInc(xc, z, y);
534 uint32_t y = (*xa & 0xffff) - borrow;
535 borrow = (y & 0x10000) >> 16;
536 uint32_t z = (*xa++ >> 16) - borrow;
537 borrow = (z & 0x10000) >> 16;
538 xc = storeInc(xc, z, y);
546 static ALWAYS_INLINE void d2b(BigInt& b, U* d, int* e, int* bits)
560 d0 &= 0x7fffffff; /* clear sign bit, which we ignore */
561 if ((de = (int)(d0 >> Exp_shift)))
564 if ((k = lo0bits(&y))) {
565 x[0] = y | (z << (32 - k));
583 *e = de - Bias - (P - 1) + k;
586 *e = 0 - Bias - (P - 1) + 1 + k;
587 *bits = (32 * i) - hi0bits(x[i - 1]);
593 static const double tens[] = {
594 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
595 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
599 static const double bigtens[] = { 1e16, 1e32, 1e64, 1e128, 1e256 };
600 static const double tinytens[] = { 1e-16, 1e-32, 1e-64, 1e-128,
601 9007199254740992. * 9007199254740992.e-256
602 /* = 2^106 * 1e-256 */
605 /* The factor of 2^53 in tinytens[4] helps us avoid setting the underflow */
606 /* flag unnecessarily. It leads to a song and dance at the end of strtod. */
607 #define Scale_Bit 0x10
610 static ALWAYS_INLINE int quorem(BigInt& b, BigInt& S)
619 unsigned long long borrow, carry, y, ys;
621 uint32_t borrow, carry, y, ys;
624 ASSERT(b.size() <= 1 || b.words()[b.size() - 1]);
625 ASSERT(S.size() <= 1 || S.words()[S.size() - 1]);
628 ASSERT_WITH_MESSAGE(b.size() <= n, "oversize b in quorem");
635 q = *bxe / (*sxe + 1); /* ensure q <= true quotient */
636 ASSERT_WITH_MESSAGE(q <= 9, "oversized quotient in quorem");
642 ys = *sx++ * (unsigned long long)q + carry;
644 y = *bx - (ys & 0xffffffffUL) - borrow;
645 borrow = y >> 32 & (uint32_t)1;
646 *bx++ = (uint32_t)y & 0xffffffffUL;
649 ys = (si & 0xffff) * q + carry;
650 zs = (si >> 16) * q + (ys >> 16);
652 y = (*bx & 0xffff) - (ys & 0xffff) - borrow;
653 borrow = (y & 0x10000) >> 16;
654 z = (*bx >> 16) - (zs & 0xffff) - borrow;
655 borrow = (z & 0x10000) >> 16;
656 bx = storeInc(bx, z, y);
661 while (--bxe > bx && !*bxe)
666 if (cmp(b, S) >= 0) {
676 y = *bx - (ys & 0xffffffffUL) - borrow;
677 borrow = y >> 32 & (uint32_t)1;
678 *bx++ = (uint32_t)y & 0xffffffffUL;
681 ys = (si & 0xffff) + carry;
682 zs = (si >> 16) + (ys >> 16);
684 y = (*bx & 0xffff) - (ys & 0xffff) - borrow;
685 borrow = (y & 0x10000) >> 16;
686 z = (*bx >> 16) - (zs & 0xffff) - borrow;
687 borrow = (z & 0x10000) >> 16;
688 bx = storeInc(bx, z, y);
694 while (--bxe > bx && !*bxe)
702 /* dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
704 * Inspired by "How to Print Floating-Point Numbers Accurately" by
705 * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 112-126].
708 * 1. Rather than iterating, we use a simple numeric overestimate
709 * to determine k = floor(log10(d)). We scale relevant
710 * quantities using O(log2(k)) rather than O(k) multiplications.
711 * 2. For some modes > 2 (corresponding to ecvt and fcvt), we don't
712 * try to generate digits strictly left to right. Instead, we
713 * compute with fewer bits and propagate the carry if necessary
714 * when rounding the final digit up. This is often faster.
715 * 3. Under the assumption that input will be rounded nearest,
716 * mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22.
717 * That is, we allow equality in stopping tests when the
718 * round-nearest rule will give the same floating-point value
719 * as would satisfaction of the stopping test with strict
721 * 4. We remove common factors of powers of 2 from relevant
723 * 5. When converting floating-point integers less than 1e16,
724 * we use floating-point arithmetic rather than resorting
725 * to multiple-precision integers.
726 * 6. When asked to produce fewer than 15 digits, we first try
727 * to get by with floating-point arithmetic; we resort to
728 * multiple-precision integer arithmetic only if we cannot
729 * guarantee that the floating-point calculation has given
730 * the correctly rounded result. For k requested digits and
731 * "uniformly" distributed input, the probability is
732 * something like 10^(k-15) that we must resort to the int32_t
735 * Note: 'leftright' translates to 'generate shortest possible string'.
737 template<bool roundingNone, bool roundingSignificantFigures, bool roundingDecimalPlaces, bool leftright>
738 void dtoa(DtoaBuffer result, double dd, int ndigits, bool& signOut, int& exponentOut, unsigned& precisionOut)
740 // Exactly one rounding mode must be specified.
741 ASSERT(roundingNone + roundingSignificantFigures + roundingDecimalPlaces == 1);
742 // roundingNone only allowed (only sensible?) with leftright set.
743 ASSERT(!roundingNone || leftright);
745 ASSERT(isfinite(dd));
747 int bbits, b2, b5, be, dig, i, ieps, ilim = 0, ilim0, ilim1 = 0,
748 j, j1, k, k0, k_check, m2, m5, s2, s5,
753 BigInt b, delta, mlo, mhi, S;
761 /* Infinity or NaN */
762 ASSERT((word0(&u) & Exp_mask) != Exp_mask);
764 // JavaScript toString conversion treats -0 as 0.
774 if (word0(&u) & Sign_bit) {
776 word0(&u) &= ~Sign_bit; // clear sign bit
780 d2b(b, &u, &be, &bbits);
781 if ((i = (int)(word0(&u) >> Exp_shift1 & (Exp_mask >> Exp_shift1)))) {
782 dval(&d2) = dval(&u);
783 word0(&d2) &= Frac_mask1;
784 word0(&d2) |= Exp_11;
786 /* log(x) ~=~ log(1.5) + (x-1.5)/1.5
787 * log10(x) = log(x) / log(10)
788 * ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10))
789 * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2)
791 * This suggests computing an approximation k to log10(d) by
793 * k = (i - Bias)*0.301029995663981
794 * + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 );
796 * We want k to be too large rather than too small.
797 * The error in the first-order Taylor series approximation
798 * is in our favor, so we just round up the constant enough
799 * to compensate for any error in the multiplication of
800 * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077,
801 * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14,
802 * adding 1e-13 to the constant term more than suffices.
803 * Hence we adjust the constant term to 0.1760912590558.
804 * (We could get a more accurate k by invoking log10,
805 * but this is probably not worthwhile.)
811 /* d is denormalized */
813 i = bbits + be + (Bias + (P - 1) - 1);
814 x = (i > 32) ? (word0(&u) << (64 - i)) | (word1(&u) >> (i - 32))
815 : word1(&u) << (32 - i);
817 word0(&d2) -= 31 * Exp_msk1; /* adjust exponent */
818 i -= (Bias + (P - 1) - 1) + 1;
821 ds = (dval(&d2) - 1.5) * 0.289529654602168 + 0.1760912590558 + (i * 0.301029995663981);
823 if (ds < 0. && ds != k)
824 k--; /* want k = floor(ds) */
826 if (k >= 0 && k <= Ten_pmax) {
827 if (dval(&u) < tens[k])
854 if (roundingSignificantFigures) {
857 ilim = ilim1 = i = ndigits;
859 if (roundingDecimalPlaces) {
869 if (ilim >= 0 && ilim <= Quick_max) {
870 /* Try to get by with floating-point arithmetic. */
873 dval(&d2) = dval(&u);
876 ieps = 2; /* conservative */
881 /* prevent overflows */
883 dval(&u) /= bigtens[n_bigtens - 1];
886 for (; j; j >>= 1, i++) {
893 } else if ((j1 = -k)) {
894 dval(&u) *= tens[j1 & 0xf];
895 for (j = j1 >> 4; j; j >>= 1, i++) {
898 dval(&u) *= bigtens[i];
902 if (k_check && dval(&u) < 1. && ilim > 0) {
910 dval(&eps) = (ieps * dval(&u)) + 7.;
911 word0(&eps) -= (P - 1) * Exp_msk1;
916 if (dval(&u) > dval(&eps))
918 if (dval(&u) < -dval(&eps))
923 /* Use Steele & White method of only
924 * generating digits needed.
926 dval(&eps) = (0.5 / tens[ilim - 1]) - dval(&eps);
928 L = (long int)dval(&u);
931 if (dval(&u) < dval(&eps))
933 if (1. - dval(&u) < dval(&eps))
941 /* Generate ilim digits, then fix them up. */
942 dval(&eps) *= tens[ilim - 1];
943 for (i = 1;; i++, dval(&u) *= 10.) {
944 L = (int32_t)(dval(&u));
945 if (!(dval(&u) -= L))
949 if (dval(&u) > 0.5 + dval(&eps))
951 if (dval(&u) < 0.5 - dval(&eps)) {
952 while (*--s == '0') { }
962 dval(&u) = dval(&d2);
967 /* Do we have a "small" integer? */
969 if (be >= 0 && k <= Int_max) {
972 if (ndigits < 0 && ilim <= 0) {
975 if (ilim < 0 || dval(&u) <= 5 * ds)
979 for (i = 1;; i++, dval(&u) *= 10.) {
980 L = (int32_t)(dval(&u) / ds);
987 dval(&u) += dval(&u);
988 if (dval(&u) > ds || (dval(&u) == ds && (L & 1))) {
1009 i = denorm ? be + (Bias + (P - 1) - 1 + 1) : 1 + P - bbits;
1014 if (m2 > 0 && s2 > 0) {
1015 i = m2 < s2 ? m2 : s2;
1035 /* Check for special case that d is a normalized power of 2. */
1038 if ((roundingNone || leftright) && (!word1(&u) && !(word0(&u) & Bndry_mask) && word0(&u) & (Exp_mask & ~Exp_msk1))) {
1039 /* The special case */
1045 /* Arrange for convenient computation of quotients:
1046 * shift left if necessary so divisor has 4 leading 0 bits.
1048 * Perhaps we should just compute leading 28 bits of S once
1049 * and for all and pass them and a shift to quorem, so it
1050 * can do shifts and ors to compute the numerator for q.
1052 if ((i = ((s5 ? 32 - hi0bits(S.words()[S.size() - 1]) : 1) + s2) & 0x1f))
1070 if (cmp(b, S) < 0) {
1072 multadd(b, 10, 0); /* we botched the k estimate */
1074 multadd(mhi, 10, 0);
1078 if (ilim <= 0 && roundingDecimalPlaces) {
1082 // For IEEE-754 unbiased rounding this check should be <=, such that 0.5 would flush to zero.
1091 /* Compute mlo -- check for special case
1092 * that d is a normalized power of 2.
1100 dig = quorem(b, S) + '0';
1101 /* Do we yet have the shortest decimal string
1102 * that will round to d?
1105 diff(delta, S, mhi);
1106 j1 = delta.sign ? 1 : cmp(b, delta);
1107 #ifdef DTOA_ROUND_BIASED
1110 // FIXME: ECMA-262 specifies that equidistant results round away from
1111 // zero, which probably means we shouldn't be on the unbiased code path
1112 // (the (word1(&u) & 1) clause is looking highly suspicious). I haven't
1113 // yet understood this code well enough to make the call, but we should
1114 // probably be enabling DTOA_ROUND_BIASED. I think the interesting corner
1115 // case to understand is probably "Math.pow(0.5, 24).toString()".
1116 // I believe this value is interesting because I think it is precisely
1117 // representable in binary floating point, and its decimal representation
1118 // has a single digit that Steele & White reduction can remove, with the
1119 // value 5 (thus equidistant from the next numbers above and below).
1120 // We produce the correct answer using either codepath, and I don't as
1121 // yet understand why. :-)
1122 if (!j1 && !(word1(&u) & 1)) {
1130 if (j < 0 || (!j && !(word1(&u) & 1))) {
1132 if ((b.words()[0] || b.size() > 1) && (j1 > 0)) {
1135 // For IEEE-754 round-to-even, this check should be (j1 > 0 || (!j1 && (dig & 1))),
1136 // but ECMA-262 specifies that equidistant values (e.g. (.5).toFixed()) should
1137 // be rounded away from zero.
1148 if (dig == '9') { /* possible if i == 1 */
1160 multadd(mlo, 10, 0);
1161 multadd(mhi, 10, 0);
1165 *s++ = dig = quorem(b, S) + '0';
1166 if (!b.words()[0] && b.size() <= 1)
1174 /* Round off last digit */
1178 // For IEEE-754 round-to-even, this check should be (j > 0 || (!j && (dig & 1))),
1179 // but ECMA-262 specifies that equidistant values (e.g. (.5).toFixed()) should
1180 // be rounded away from zero.
1191 while (*--s == '0') { }
1209 precisionOut = s - result;
1212 void dtoa(DtoaBuffer result, double dd, bool& sign, int& exponent, unsigned& precision)
1214 // flags are roundingNone, leftright.
1215 dtoa<true, false, false, true>(result, dd, 0, sign, exponent, precision);
1218 void dtoaRoundSF(DtoaBuffer result, double dd, int ndigits, bool& sign, int& exponent, unsigned& precision)
1220 // flag is roundingSignificantFigures.
1221 dtoa<false, true, false, false>(result, dd, ndigits, sign, exponent, precision);
1224 void dtoaRoundDP(DtoaBuffer result, double dd, int ndigits, bool& sign, int& exponent, unsigned& precision)
1226 // flag is roundingDecimalPlaces.
1227 dtoa<false, false, true, false>(result, dd, ndigits, sign, exponent, precision);
1230 const char* numberToString(double d, NumberToStringBuffer buffer)
1232 double_conversion::StringBuilder builder(buffer, NumberToStringBufferLength);
1233 const double_conversion::DoubleToStringConverter& converter = double_conversion::DoubleToStringConverter::EcmaScriptConverter();
1234 converter.ToShortest(d, &builder);
1235 return builder.Finalize();
1238 static inline const char* formatStringTruncatingTrailingZerosIfNeeded(NumberToStringBuffer buffer, double_conversion::StringBuilder& builder)
1240 size_t length = builder.position();
1241 size_t decimalPointPosition = 0;
1242 for (; decimalPointPosition < length; ++decimalPointPosition) {
1243 if (buffer[decimalPointPosition] == '.')
1247 // No decimal seperator found, early exit.
1248 if (decimalPointPosition == length)
1249 return builder.Finalize();
1251 size_t truncatedLength = length - 1;
1252 for (; truncatedLength > decimalPointPosition; --truncatedLength) {
1253 if (buffer[truncatedLength] != '0')
1257 // No trailing zeros found to strip.
1258 if (truncatedLength == length - 1)
1259 return builder.Finalize();
1261 // If we removed all trailing zeros, remove the decimal point as well.
1262 if (truncatedLength == decimalPointPosition) {
1263 ASSERT(truncatedLength > 0);
1267 // Truncate the StringBuilder, and return the final result.
1268 builder.SetPosition(truncatedLength + 1);
1269 return builder.Finalize();
1272 const char* numberToFixedPrecisionString(double d, unsigned significantFigures, NumberToStringBuffer buffer, bool truncateTrailingZeros)
1274 // Mimic String::format("%.[precision]g", ...), but use dtoas rounding facilities.
1275 // "g": Signed value printed in f or e format, whichever is more compact for the given value and precision.
1276 // The e format is used only when the exponent of the value is less than –4 or greater than or equal to the
1277 // precision argument. Trailing zeros are truncated, and the decimal point appears only if one or more digits follow it.
1278 // "precision": The precision specifies the maximum number of significant digits printed.
1279 double_conversion::StringBuilder builder(buffer, NumberToStringBufferLength);
1280 const double_conversion::DoubleToStringConverter& converter = double_conversion::DoubleToStringConverter::EcmaScriptConverter();
1281 converter.ToPrecision(d, significantFigures, &builder);
1282 if (!truncateTrailingZeros)
1283 return builder.Finalize();
1284 return formatStringTruncatingTrailingZerosIfNeeded(buffer, builder);
1287 const char* numberToFixedWidthString(double d, unsigned decimalPlaces, NumberToStringBuffer buffer)
1289 // Mimic String::format("%.[precision]f", ...), but use dtoas rounding facilities.
1290 // "f": Signed value having the form [ – ]dddd.dddd, where dddd is one or more decimal digits.
1291 // The number of digits before the decimal point depends on the magnitude of the number, and
1292 // the number of digits after the decimal point depends on the requested precision.
1293 // "precision": The precision value specifies the number of digits after the decimal point.
1294 // If a decimal point appears, at least one digit appears before it.
1295 // The value is rounded to the appropriate number of digits.
1296 double_conversion::StringBuilder builder(buffer, NumberToStringBufferLength);
1297 const double_conversion::DoubleToStringConverter& converter = double_conversion::DoubleToStringConverter::EcmaScriptConverter();
1298 converter.ToFixed(d, decimalPlaces, &builder);
1299 return builder.Finalize();
1302 namespace Internal {
1304 double parseDoubleFromLongString(const UChar* string, size_t length, size_t& parsedLength)
1306 Vector<LChar> conversionBuffer(length);
1307 for (size_t i = 0; i < length; ++i)
1308 conversionBuffer[i] = isASCII(string[i]) ? string[i] : 0;
1309 return parseDouble(conversionBuffer.data(), length, parsedLength);
1312 } // namespace Internal