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/MathExtras.h"
40 #include "wtf/ThreadingPrimitives.h"
41 #include "wtf/Vector.h"
44 #pragma warning(disable: 4244)
45 #pragma warning(disable: 4245)
46 #pragma warning(disable: 4554)
58 #if CPU(BIG_ENDIAN) || CPU(MIDDLE_ENDIAN)
59 #define word0(x) (x)->L[0]
60 #define word1(x) (x)->L[1]
62 #define word0(x) (x)->L[1]
63 #define word1(x) (x)->L[0]
65 #define dval(x) (x)->d
69 #define Exp_msk1 0x100000
70 #define Exp_msk11 0x100000
71 #define Exp_mask 0x7ff00000
75 #define Exp_1 0x3ff00000
76 #define Exp_11 0x3ff00000
78 #define Frac_mask 0xfffff
79 #define Frac_mask1 0xfffff
82 #define Bndry_mask 0xfffff
83 #define Bndry_mask1 0xfffff
85 #define Sign_bit 0x80000000
92 #define rounded_product(a, b) a *= b
93 #define rounded_quotient(a, b) a /= b
95 #define Big0 (Frac_mask1 | Exp_msk1 * (DBL_MAX_EXP + Bias - 1))
96 #define Big1 0xffffffff
99 // FIXME: should we enable this on all 64-bit CPUs?
100 // 64-bit emulation provided by the compiler is likely to be slower than dtoa own code on 32-bit hardware.
101 #define USE_LONG_LONG
104 #ifndef USE_LONG_LONG
105 /* The following definition of Storeinc is appropriate for MIPS processors.
106 * An alternative that might be better on some machines is
107 * *p++ = high << 16 | low & 0xffff;
109 static ALWAYS_INLINE
uint32_t* storeInc(uint32_t* p
, uint16_t high
, uint16_t low
)
111 uint16_t* p16
= reinterpret_cast<uint16_t*>(p
);
124 BigInt() : sign(0) { }
135 return m_words
.size();
138 void resize(size_t s
)
145 return m_words
.data();
148 const uint32_t* words() const
150 return m_words
.data();
153 void append(uint32_t w
)
158 Vector
<uint32_t, 16> m_words
;
161 static void multadd(BigInt
& b
, int m
, int a
) /* multiply by m and add a */
164 unsigned long long carry
;
170 uint32_t* x
= b
.words();
175 unsigned long long y
= *x
* (unsigned long long)m
+ carry
;
177 *x
++ = (uint32_t)y
& 0xffffffffUL
;
180 uint32_t y
= (xi
& 0xffff) * m
+ carry
;
181 uint32_t z
= (xi
>> 16) * m
+ (y
>> 16);
183 *x
++ = (z
<< 16) + (y
& 0xffff);
188 b
.append((uint32_t)carry
);
191 static int hi0bits(uint32_t x
)
195 if (!(x
& 0xffff0000)) {
199 if (!(x
& 0xff000000)) {
203 if (!(x
& 0xf0000000)) {
207 if (!(x
& 0xc0000000)) {
211 if (!(x
& 0x80000000)) {
213 if (!(x
& 0x40000000))
219 static int lo0bits(uint32_t* y
)
261 static void i2b(BigInt
& b
, int i
)
268 static void mult(BigInt
& aRef
, const BigInt
& bRef
)
270 const BigInt
* a
= &aRef
;
271 const BigInt
* b
= &bRef
;
274 const uint32_t* x
= 0;
283 unsigned long long carry
, z
;
288 if (a
->size() < b
->size()) {
289 const BigInt
* tmp
= a
;
299 for (xc
= c
.words(), xa
= xc
+ wc
; xc
< xa
; xc
++)
307 for (; xb
< xbe
; xc0
++) {
313 z
= *x
++ * (unsigned long long)y
+ *xc
+ carry
;
315 *xc
++ = (uint32_t)z
& 0xffffffffUL
;
317 *xc
= (uint32_t)carry
;
321 for (; xb
< xbe
; xb
++, xc0
++) {
322 if ((y
= *xb
& 0xffff)) {
327 z
= (*x
& 0xffff) * y
+ (*xc
& 0xffff) + carry
;
329 uint32_t z2
= (*x
++ >> 16) * y
+ (*xc
>> 16) + carry
;
331 xc
= storeInc(xc
, z2
, z
);
335 if ((y
= *xb
>> 16)) {
341 z
= (*x
& 0xffff) * y
+ (*xc
>> 16) + carry
;
343 xc
= storeInc(xc
, z
, z2
);
344 z2
= (*x
++ >> 16) * y
+ (*xc
& 0xffff) + carry
;
351 for (xc0
= c
.words(), xc
= xc0
+ wc
; wc
> 0 && !*--xc
; --wc
) { }
357 WTF_MAKE_NONCOPYABLE(P5Node
); WTF_MAKE_FAST_ALLOCATED(P5Node
);
367 static ALWAYS_INLINE
void pow5mult(BigInt
& b
, int k
)
369 static int p05
[3] = { 5, 25, 125 };
372 multadd(b
, p05
[i
- 1], 0);
377 s_dtoaP5Mutex
->lock();
389 int p5sCountLocal
= p5sCount
;
390 s_dtoaP5Mutex
->unlock();
400 if (++p5sUsed
== p5sCountLocal
) {
401 s_dtoaP5Mutex
->lock();
402 if (p5sUsed
== p5sCount
) {
404 p5
->next
= new P5Node
;
406 p5
->next
->val
= p5
->val
;
407 mult(p5
->next
->val
, p5
->next
->val
);
411 p5sCountLocal
= p5sCount
;
412 s_dtoaP5Mutex
->unlock();
418 static ALWAYS_INLINE
void lshift(BigInt
& b
, int k
)
422 int origSize
= b
.size();
423 int n1
= n
+ origSize
+ 1;
426 b
.resize(b
.size() + n
+ 1);
428 b
.resize(b
.size() + n
);
430 const uint32_t* srcStart
= b
.words();
431 uint32_t* dstStart
= b
.words();
432 const uint32_t* src
= srcStart
+ origSize
- 1;
433 uint32_t* dst
= dstStart
+ n1
- 1;
435 uint32_t hiSubword
= 0;
437 for (; src
>= srcStart
; --src
) {
438 *dst
-- = hiSubword
| *src
>> s
;
439 hiSubword
= *src
<< k
;
442 ASSERT(dst
== dstStart
+ n
);
444 b
.resize(origSize
+ n
+ !!b
.words()[n1
- 1]);
449 } while (src
>= srcStart
);
451 for (dst
= dstStart
+ n
; dst
!= dstStart
; )
454 ASSERT(b
.size() <= 1 || b
.words()[b
.size() - 1]);
457 static int cmp(const BigInt
& a
, const BigInt
& b
)
459 const uint32_t *xa
, *xa0
, *xb
, *xb0
;
464 ASSERT(i
<= 1 || a
.words()[i
- 1]);
465 ASSERT(j
<= 1 || b
.words()[j
- 1]);
474 return *xa
< *xb
? -1 : 1;
481 static ALWAYS_INLINE
void diff(BigInt
& c
, const BigInt
& aRef
, const BigInt
& bRef
)
483 const BigInt
* a
= &aRef
;
484 const BigInt
* b
= &bRef
;
496 const BigInt
* tmp
= a
;
504 const uint32_t* xa
= a
->words();
505 const uint32_t* xae
= xa
+ wa
;
507 const uint32_t* xb
= b
->words();
508 const uint32_t* xbe
= xb
+ wb
;
514 unsigned long long borrow
= 0;
516 unsigned long long y
= (unsigned long long)*xa
++ - *xb
++ - borrow
;
517 borrow
= y
>> 32 & (uint32_t)1;
518 *xc
++ = (uint32_t)y
& 0xffffffffUL
;
521 unsigned long long y
= *xa
++ - borrow
;
522 borrow
= y
>> 32 & (uint32_t)1;
523 *xc
++ = (uint32_t)y
& 0xffffffffUL
;
528 uint32_t y
= (*xa
& 0xffff) - (*xb
& 0xffff) - borrow
;
529 borrow
= (y
& 0x10000) >> 16;
530 uint32_t z
= (*xa
++ >> 16) - (*xb
++ >> 16) - borrow
;
531 borrow
= (z
& 0x10000) >> 16;
532 xc
= storeInc(xc
, z
, y
);
535 uint32_t y
= (*xa
& 0xffff) - borrow
;
536 borrow
= (y
& 0x10000) >> 16;
537 uint32_t z
= (*xa
++ >> 16) - borrow
;
538 borrow
= (z
& 0x10000) >> 16;
539 xc
= storeInc(xc
, z
, y
);
547 static ALWAYS_INLINE
void d2b(BigInt
& b
, U
* d
, int* e
, int* bits
)
561 d0
&= 0x7fffffff; /* clear sign bit, which we ignore */
562 if ((de
= (int)(d0
>> Exp_shift
)))
565 if ((k
= lo0bits(&y
))) {
566 x
[0] = y
| (z
<< (32 - k
));
584 *e
= de
- Bias
- (P
- 1) + k
;
587 *e
= 0 - Bias
- (P
- 1) + 1 + k
;
588 *bits
= (32 * i
) - hi0bits(x
[i
- 1]);
594 static const double tens
[] = {
595 1e0
, 1e1
, 1e2
, 1e3
, 1e4
, 1e5
, 1e6
, 1e7
, 1e8
, 1e9
,
596 1e10
, 1e11
, 1e12
, 1e13
, 1e14
, 1e15
, 1e16
, 1e17
, 1e18
, 1e19
,
600 static const double bigtens
[] = { 1e16
, 1e32
, 1e64
, 1e128
, 1e256
};
602 #define Scale_Bit 0x10
605 static ALWAYS_INLINE
int quorem(BigInt
& b
, BigInt
& S
)
614 unsigned long long borrow
, carry
, y
, ys
;
616 uint32_t borrow
, carry
, y
, ys
;
619 ASSERT(b
.size() <= 1 || b
.words()[b
.size() - 1]);
620 ASSERT(S
.size() <= 1 || S
.words()[S
.size() - 1]);
623 ASSERT_WITH_MESSAGE(b
.size() <= n
, "oversize b in quorem");
630 q
= *bxe
/ (*sxe
+ 1); /* ensure q <= true quotient */
631 ASSERT_WITH_MESSAGE(q
<= 9, "oversized quotient in quorem");
637 ys
= *sx
++ * (unsigned long long)q
+ carry
;
639 y
= *bx
- (ys
& 0xffffffffUL
) - borrow
;
640 borrow
= y
>> 32 & (uint32_t)1;
641 *bx
++ = (uint32_t)y
& 0xffffffffUL
;
644 ys
= (si
& 0xffff) * q
+ carry
;
645 zs
= (si
>> 16) * q
+ (ys
>> 16);
647 y
= (*bx
& 0xffff) - (ys
& 0xffff) - borrow
;
648 borrow
= (y
& 0x10000) >> 16;
649 z
= (*bx
>> 16) - (zs
& 0xffff) - borrow
;
650 borrow
= (z
& 0x10000) >> 16;
651 bx
= storeInc(bx
, z
, y
);
656 while (--bxe
> bx
&& !*bxe
)
661 if (cmp(b
, S
) >= 0) {
671 y
= *bx
- (ys
& 0xffffffffUL
) - borrow
;
672 borrow
= y
>> 32 & (uint32_t)1;
673 *bx
++ = (uint32_t)y
& 0xffffffffUL
;
676 ys
= (si
& 0xffff) + carry
;
677 zs
= (si
>> 16) + (ys
>> 16);
679 y
= (*bx
& 0xffff) - (ys
& 0xffff) - borrow
;
680 borrow
= (y
& 0x10000) >> 16;
681 z
= (*bx
>> 16) - (zs
& 0xffff) - borrow
;
682 borrow
= (z
& 0x10000) >> 16;
683 bx
= storeInc(bx
, z
, y
);
689 while (--bxe
> bx
&& !*bxe
)
697 /* dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
699 * Inspired by "How to Print Floating-Point Numbers Accurately" by
700 * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 112-126].
703 * 1. Rather than iterating, we use a simple numeric overestimate
704 * to determine k = floor(log10(d)). We scale relevant
705 * quantities using O(log2(k)) rather than O(k) multiplications.
706 * 2. For some modes > 2 (corresponding to ecvt and fcvt), we don't
707 * try to generate digits strictly left to right. Instead, we
708 * compute with fewer bits and propagate the carry if necessary
709 * when rounding the final digit up. This is often faster.
710 * 3. Under the assumption that input will be rounded nearest,
711 * mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22.
712 * That is, we allow equality in stopping tests when the
713 * round-nearest rule will give the same floating-point value
714 * as would satisfaction of the stopping test with strict
716 * 4. We remove common factors of powers of 2 from relevant
718 * 5. When converting floating-point integers less than 1e16,
719 * we use floating-point arithmetic rather than resorting
720 * to multiple-precision integers.
721 * 6. When asked to produce fewer than 15 digits, we first try
722 * to get by with floating-point arithmetic; we resort to
723 * multiple-precision integer arithmetic only if we cannot
724 * guarantee that the floating-point calculation has given
725 * the correctly rounded result. For k requested digits and
726 * "uniformly" distributed input, the probability is
727 * something like 10^(k-15) that we must resort to the int32_t
730 * Note: 'leftright' translates to 'generate shortest possible string'.
732 template<bool roundingNone
, bool roundingSignificantFigures
, bool roundingDecimalPlaces
, bool leftright
>
733 void dtoa(DtoaBuffer result
, double dd
, int ndigits
, bool& signOut
, int& exponentOut
, unsigned& precisionOut
)
735 // Exactly one rounding mode must be specified.
736 ASSERT(roundingNone
+ roundingSignificantFigures
+ roundingDecimalPlaces
== 1);
737 // roundingNone only allowed (only sensible?) with leftright set.
738 ASSERT(!roundingNone
|| leftright
);
740 ASSERT(std::isfinite(dd
));
742 int bbits
, b2
, b5
, be
, dig
, i
, ieps
, ilim
= 0, ilim0
, ilim1
= 0,
743 j
, j1
, k
, k0
, k_check
, m2
, m5
, s2
, s5
,
748 BigInt b
, delta
, mlo
, mhi
, S
;
756 /* Infinity or NaN */
757 ASSERT((word0(&u
) & Exp_mask
) != Exp_mask
);
759 // JavaScript toString conversion treats -0 as 0.
769 if (word0(&u
) & Sign_bit
) {
771 word0(&u
) &= ~Sign_bit
; // clear sign bit
775 d2b(b
, &u
, &be
, &bbits
);
776 if ((i
= (int)(word0(&u
) >> Exp_shift1
& (Exp_mask
>> Exp_shift1
)))) {
777 dval(&d2
) = dval(&u
);
778 word0(&d2
) &= Frac_mask1
;
779 word0(&d2
) |= Exp_11
;
781 /* log(x) ~=~ log(1.5) + (x-1.5)/1.5
782 * log10(x) = log(x) / log(10)
783 * ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10))
784 * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2)
786 * This suggests computing an approximation k to log10(d) by
788 * k = (i - Bias)*0.301029995663981
789 * + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 );
791 * We want k to be too large rather than too small.
792 * The error in the first-order Taylor series approximation
793 * is in our favor, so we just round up the constant enough
794 * to compensate for any error in the multiplication of
795 * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077,
796 * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14,
797 * adding 1e-13 to the constant term more than suffices.
798 * Hence we adjust the constant term to 0.1760912590558.
799 * (We could get a more accurate k by invoking log10,
800 * but this is probably not worthwhile.)
806 /* d is denormalized */
808 i
= bbits
+ be
+ (Bias
+ (P
- 1) - 1);
809 x
= (i
> 32) ? (word0(&u
) << (64 - i
)) | (word1(&u
) >> (i
- 32))
810 : word1(&u
) << (32 - i
);
812 word0(&d2
) -= 31 * Exp_msk1
; /* adjust exponent */
813 i
-= (Bias
+ (P
- 1) - 1) + 1;
816 ds
= (dval(&d2
) - 1.5) * 0.289529654602168 + 0.1760912590558 + (i
* 0.301029995663981);
818 if (ds
< 0. && ds
!= k
)
819 k
--; /* want k = floor(ds) */
821 if (k
>= 0 && k
<= Ten_pmax
) {
822 if (dval(&u
) < tens
[k
])
849 if (roundingSignificantFigures
) {
852 ilim
= ilim1
= i
= ndigits
;
854 if (roundingDecimalPlaces
) {
864 if (ilim
>= 0 && ilim
<= Quick_max
) {
865 /* Try to get by with floating-point arithmetic. */
868 dval(&d2
) = dval(&u
);
871 ieps
= 2; /* conservative */
876 /* prevent overflows */
878 dval(&u
) /= bigtens
[n_bigtens
- 1];
881 for (; j
; j
>>= 1, i
++) {
888 } else if ((j1
= -k
)) {
889 dval(&u
) *= tens
[j1
& 0xf];
890 for (j
= j1
>> 4; j
; j
>>= 1, i
++) {
893 dval(&u
) *= bigtens
[i
];
897 if (k_check
&& dval(&u
) < 1. && ilim
> 0) {
905 dval(&eps
) = (ieps
* dval(&u
)) + 7.;
906 word0(&eps
) -= (P
- 1) * Exp_msk1
;
911 if (dval(&u
) > dval(&eps
))
913 if (dval(&u
) < -dval(&eps
))
918 /* Use Steele & White method of only
919 * generating digits needed.
921 dval(&eps
) = (0.5 / tens
[ilim
- 1]) - dval(&eps
);
923 L
= (long int)dval(&u
);
926 if (dval(&u
) < dval(&eps
))
928 if (1. - dval(&u
) < dval(&eps
))
936 /* Generate ilim digits, then fix them up. */
937 dval(&eps
) *= tens
[ilim
- 1];
938 for (i
= 1;; i
++, dval(&u
) *= 10.) {
939 L
= (int32_t)(dval(&u
));
940 if (!(dval(&u
) -= L
))
944 if (dval(&u
) > 0.5 + dval(&eps
))
946 if (dval(&u
) < 0.5 - dval(&eps
)) {
947 while (*--s
== '0') { }
957 dval(&u
) = dval(&d2
);
962 /* Do we have a "small" integer? */
964 if (be
>= 0 && k
<= Int_max
) {
967 if (ndigits
< 0 && ilim
<= 0) {
970 if (ilim
< 0 || dval(&u
) <= 5 * ds
)
974 for (i
= 1;; i
++, dval(&u
) *= 10.) {
975 L
= (int32_t)(dval(&u
) / ds
);
982 dval(&u
) += dval(&u
);
983 if (dval(&u
) > ds
|| (dval(&u
) == ds
&& (L
& 1))) {
1004 i
= denorm
? be
+ (Bias
+ (P
- 1) - 1 + 1) : 1 + P
- bbits
;
1009 if (m2
> 0 && s2
> 0) {
1010 i
= m2
< s2
? m2
: s2
;
1030 /* Check for special case that d is a normalized power of 2. */
1033 if ((roundingNone
|| leftright
) && (!word1(&u
) && !(word0(&u
) & Bndry_mask
) && word0(&u
) & (Exp_mask
& ~Exp_msk1
))) {
1034 /* The special case */
1040 /* Arrange for convenient computation of quotients:
1041 * shift left if necessary so divisor has 4 leading 0 bits.
1043 * Perhaps we should just compute leading 28 bits of S once
1044 * and for all and pass them and a shift to quorem, so it
1045 * can do shifts and ors to compute the numerator for q.
1047 if ((i
= ((s5
? 32 - hi0bits(S
.words()[S
.size() - 1]) : 1) + s2
) & 0x1f))
1065 if (cmp(b
, S
) < 0) {
1067 multadd(b
, 10, 0); /* we botched the k estimate */
1069 multadd(mhi
, 10, 0);
1073 if (ilim
<= 0 && roundingDecimalPlaces
) {
1077 // For IEEE-754 unbiased rounding this check should be <=, such that 0.5 would flush to zero.
1086 /* Compute mlo -- check for special case
1087 * that d is a normalized power of 2.
1095 dig
= quorem(b
, S
) + '0';
1096 /* Do we yet have the shortest decimal string
1097 * that will round to d?
1100 diff(delta
, S
, mhi
);
1101 j1
= delta
.sign
? 1 : cmp(b
, delta
);
1102 #ifdef DTOA_ROUND_BIASED
1105 // FIXME: ECMA-262 specifies that equidistant results round away from
1106 // zero, which probably means we shouldn't be on the unbiased code path
1107 // (the (word1(&u) & 1) clause is looking highly suspicious). I haven't
1108 // yet understood this code well enough to make the call, but we should
1109 // probably be enabling DTOA_ROUND_BIASED. I think the interesting corner
1110 // case to understand is probably "Math.pow(0.5, 24).toString()".
1111 // I believe this value is interesting because I think it is precisely
1112 // representable in binary floating point, and its decimal representation
1113 // has a single digit that Steele & White reduction can remove, with the
1114 // value 5 (thus equidistant from the next numbers above and below).
1115 // We produce the correct answer using either codepath, and I don't as
1116 // yet understand why. :-)
1117 if (!j1
&& !(word1(&u
) & 1)) {
1125 if (j
< 0 || (!j
&& !(word1(&u
) & 1))) {
1127 if ((b
.words()[0] || b
.size() > 1) && (j1
> 0)) {
1130 // For IEEE-754 round-to-even, this check should be (j1 > 0 || (!j1 && (dig & 1))),
1131 // but ECMA-262 specifies that equidistant values (e.g. (.5).toFixed()) should
1132 // be rounded away from zero.
1143 if (dig
== '9') { /* possible if i == 1 */
1155 multadd(mlo
, 10, 0);
1156 multadd(mhi
, 10, 0);
1160 *s
++ = dig
= quorem(b
, S
) + '0';
1161 if (!b
.words()[0] && b
.size() <= 1)
1169 /* Round off last digit */
1173 // For IEEE-754 round-to-even, this check should be (j > 0 || (!j && (dig & 1))),
1174 // but ECMA-262 specifies that equidistant values (e.g. (.5).toFixed()) should
1175 // be rounded away from zero.
1186 while (*--s
== '0') { }
1204 precisionOut
= s
- result
;
1207 void dtoa(DtoaBuffer result
, double dd
, bool& sign
, int& exponent
, unsigned& precision
)
1209 // flags are roundingNone, leftright.
1210 dtoa
<true, false, false, true>(result
, dd
, 0, sign
, exponent
, precision
);
1213 void dtoaRoundSF(DtoaBuffer result
, double dd
, int ndigits
, bool& sign
, int& exponent
, unsigned& precision
)
1215 // flag is roundingSignificantFigures.
1216 dtoa
<false, true, false, false>(result
, dd
, ndigits
, sign
, exponent
, precision
);
1219 void dtoaRoundDP(DtoaBuffer result
, double dd
, int ndigits
, bool& sign
, int& exponent
, unsigned& precision
)
1221 // flag is roundingDecimalPlaces.
1222 dtoa
<false, false, true, false>(result
, dd
, ndigits
, sign
, exponent
, precision
);
1225 const char* numberToString(double d
, NumberToStringBuffer buffer
)
1227 double_conversion::StringBuilder
builder(buffer
, NumberToStringBufferLength
);
1228 const double_conversion::DoubleToStringConverter
& converter
= double_conversion::DoubleToStringConverter::EcmaScriptConverter();
1229 converter
.ToShortest(d
, &builder
);
1230 return builder
.Finalize();
1233 static inline const char* formatStringTruncatingTrailingZerosIfNeeded(NumberToStringBuffer buffer
, double_conversion::StringBuilder
& builder
)
1235 size_t length
= builder
.position();
1236 size_t decimalPointPosition
= 0;
1237 for (; decimalPointPosition
< length
; ++decimalPointPosition
) {
1238 if (buffer
[decimalPointPosition
] == '.')
1242 // No decimal seperator found, early exit.
1243 if (decimalPointPosition
== length
)
1244 return builder
.Finalize();
1246 size_t truncatedLength
= length
- 1;
1247 for (; truncatedLength
> decimalPointPosition
; --truncatedLength
) {
1248 if (buffer
[truncatedLength
] != '0')
1252 // No trailing zeros found to strip.
1253 if (truncatedLength
== length
- 1)
1254 return builder
.Finalize();
1256 // If we removed all trailing zeros, remove the decimal point as well.
1257 if (truncatedLength
== decimalPointPosition
) {
1258 ASSERT(truncatedLength
> 0);
1262 // Truncate the StringBuilder, and return the final result.
1263 builder
.SetPosition(truncatedLength
+ 1);
1264 return builder
.Finalize();
1267 const char* numberToFixedPrecisionString(double d
, unsigned significantFigures
, NumberToStringBuffer buffer
, bool truncateTrailingZeros
)
1269 // Mimic String::format("%.[precision]g", ...), but use dtoas rounding facilities.
1270 // "g": Signed value printed in f or e format, whichever is more compact for the given value and precision.
1271 // The e format is used only when the exponent of the value is less than -4 or greater than or equal to the
1272 // precision argument. Trailing zeros are truncated, and the decimal point appears only if one or more digits follow it.
1273 // "precision": The precision specifies the maximum number of significant digits printed.
1274 double_conversion::StringBuilder
builder(buffer
, NumberToStringBufferLength
);
1275 const double_conversion::DoubleToStringConverter
& converter
= double_conversion::DoubleToStringConverter::EcmaScriptConverter();
1276 converter
.ToPrecision(d
, significantFigures
, &builder
);
1277 if (!truncateTrailingZeros
)
1278 return builder
.Finalize();
1279 return formatStringTruncatingTrailingZerosIfNeeded(buffer
, builder
);
1282 const char* numberToFixedWidthString(double d
, unsigned decimalPlaces
, NumberToStringBuffer buffer
)
1284 // Mimic String::format("%.[precision]f", ...), but use dtoas rounding facilities.
1285 // "f": Signed value having the form [ - ]dddd.dddd, where dddd is one or more decimal digits.
1286 // The number of digits before the decimal point depends on the magnitude of the number, and
1287 // the number of digits after the decimal point depends on the requested precision.
1288 // "precision": The precision value specifies the number of digits after the decimal point.
1289 // If a decimal point appears, at least one digit appears before it.
1290 // The value is rounded to the appropriate number of digits.
1291 double_conversion::StringBuilder
builder(buffer
, NumberToStringBufferLength
);
1292 const double_conversion::DoubleToStringConverter
& converter
= double_conversion::DoubleToStringConverter::EcmaScriptConverter();
1293 converter
.ToFixed(d
, decimalPlaces
, &builder
);
1294 return builder
.Finalize();
1297 namespace Internal
{
1299 double parseDoubleFromLongString(const UChar
* string
, size_t length
, size_t& parsedLength
)
1301 Vector
<LChar
> conversionBuffer(length
);
1302 for (size_t i
= 0; i
< length
; ++i
)
1303 conversionBuffer
[i
] = isASCII(string
[i
]) ? string
[i
] : 0;
1304 return parseDouble(conversionBuffer
.data(), length
, parsedLength
);
1307 } // namespace Internal