2
* The authors of this software are Rob Pike and Ken Thompson,
3
* with contributions from Mike Burrows and Sean Dorward.
5
* Copyright (c) 2002-2006 by Lucent Technologies.
6
* Portions Copyright (c) 2004 Google Inc.
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.
13
* THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
14
* WARRANTY. IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES
15
* NOR GOOGLE INC MAKE ANY REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING
16
* THE MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
19
/* Copyright (c) 2002-2006 Lucent Technologies; see LICENSE */
33
* first few powers of 10, enough for about 1/2 of the
34
* total space for doubles.
36
static double pows10[] =
38
1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
39
1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
40
1e20, 1e21, 1e22, 1e23, 1e24, 1e25, 1e26, 1e27, 1e28, 1e29,
41
1e30, 1e31, 1e32, 1e33, 1e34, 1e35, 1e36, 1e37, 1e38, 1e39,
42
1e40, 1e41, 1e42, 1e43, 1e44, 1e45, 1e46, 1e47, 1e48, 1e49,
43
1e50, 1e51, 1e52, 1e53, 1e54, 1e55, 1e56, 1e57, 1e58, 1e59,
44
1e60, 1e61, 1e62, 1e63, 1e64, 1e65, 1e66, 1e67, 1e68, 1e69,
45
1e70, 1e71, 1e72, 1e73, 1e74, 1e75, 1e76, 1e77, 1e78, 1e79,
46
1e80, 1e81, 1e82, 1e83, 1e84, 1e85, 1e86, 1e87, 1e88, 1e89,
47
1e90, 1e91, 1e92, 1e93, 1e94, 1e95, 1e96, 1e97, 1e98, 1e99,
48
1e100, 1e101, 1e102, 1e103, 1e104, 1e105, 1e106, 1e107, 1e108, 1e109,
49
1e110, 1e111, 1e112, 1e113, 1e114, 1e115, 1e116, 1e117, 1e118, 1e119,
50
1e120, 1e121, 1e122, 1e123, 1e124, 1e125, 1e126, 1e127, 1e128, 1e129,
51
1e130, 1e131, 1e132, 1e133, 1e134, 1e135, 1e136, 1e137, 1e138, 1e139,
52
1e140, 1e141, 1e142, 1e143, 1e144, 1e145, 1e146, 1e147, 1e148, 1e149,
53
1e150, 1e151, 1e152, 1e153, 1e154, 1e155, 1e156, 1e157, 1e158, 1e159,
57
#define npows10 ((int)(sizeof(pows10)/sizeof(pows10[0])))
58
#define pow10(x) fmtpow10(x)
75
d = pows10[npows10-1];
82
d *= pows10[npows10 - 1];
91
* add 1 to the decimal integer string a of length n.
92
* if 99999 overflows into 10000, return 1 to tell caller
93
* to move the virtual decimal point.
101
if(n < 0 || n > NSIGNIF)
103
for(b = a+n-1; b >= a; b--) {
112
* need to overflow adding digit.
113
* shift number down and insert 1 at beginning.
114
* decimal is known to be 0s or we wouldn't
115
* have gotten this far. (e.g., 99999+1 => 00000)
122
* subtract 1 from the decimal integer string a.
123
* if 10000 underflows into 09999, make it 99999
124
* and return 1 to tell caller to move the virtual
125
* decimal point. this way, xsub1 is inverse of xadd1.
128
xsub1(char *a, int n)
133
if(n < 0 || n > NSIGNIF)
135
for(b = a+n-1; b >= a; b--) {
138
if(c == '0' && b == a) {
140
* just zeroed the top digit; shift everyone up.
141
* decimal is known to be 9s or we wouldn't
142
* have gotten this far. (e.g., 10000-1 => 09999)
153
* can't get here. the number a is always normalized
154
* so that it has a nonzero first digit.
160
* format exponent like sprintf(p, "e%+02d", e)
163
xfmtexp(char *p, int e, int ucase)
168
*p++ = ucase ? 'E' : 'e';
176
se[i++] = e % 10 + '0';
187
* compute decimal integer m, exp such that:
189
* m is as short as possible with losing exactness
190
* assumes special cases (NaN, +Inf, -Inf) have been handled.
193
xdtoa(double f, char *s, int *exp, int *neg, int *ns)
195
int c, d, e2, e, ee, i, ndigit, oerrno;
196
char tmp[NSIGNIF+10];
199
oerrno = errno; /* in case strtod smashes errno */
202
* make f non-negative.
211
* must handle zero specially.
222
* find g,e such that f = g*10^e.
223
* guess 10-exponent using 2-exponent, then fine tune.
226
e = (int)(e2 * .301029995664);
238
* convert NSIGNIF digits as a first approximation.
240
for(i=0; i<NSIGNIF; i++) {
248
* adjust e because s is 314159... not 3.14159...
251
xfmtexp(s+NSIGNIF, e, 0);
254
* adjust conversion until strtod(s) == f exactly.
256
for(i=0; i<10; i++) {
259
if(xadd1(s, NSIGNIF)) {
262
xfmtexp(s+NSIGNIF, e, 0);
267
if(xsub1(s, NSIGNIF)) {
270
xfmtexp(s+NSIGNIF, e, 0);
278
* play with the decimal to try to simplify.
282
* bump last few digits up to 9 if we can
284
for(i=NSIGNIF-1; i>=NSIGNIF-3; i--) {
297
* add 1 in hopes of turning 9s to 0s
299
if(s[NSIGNIF-1] == '9') {
302
if(xadd1(tmp, NSIGNIF)) {
304
xfmtexp(tmp+NSIGNIF, ee, 0);
306
g = strtod(tmp, nil);
314
* bump last few digits down to 0 as we can.
316
for(i=NSIGNIF-1; i>=NSIGNIF-3; i--) {
329
* remove trailing zeros.
332
while(ndigit > 1 && s[ndigit-1] == '0'){
343
static char *special[] = { "NaN", "NaN", "+Inf", "+Inf", "-Inf", "-Inf" };
345
static char *special[] = { "nan", "NAN", "inf", "INF", "-inf", "-INF" };
351
char buf[NSIGNIF+10], *dot, *digits, *p, *s, suf[10], *t;
353
int c, chr, dotwid, e, exp, fl, ndigits, neg, newndigits;
354
int pad, point, prec, realchr, sign, sufwid, ucase, wid, z1, z2;
357
if(fmt->flags&FmtLong)
358
f = va_arg(fmt->args, long double);
360
f = va_arg(fmt->args, double);
363
* extract formatting flags
383
* pick off special numbers.
386
s = special[0+ucase];
388
fmt->flags = fl & (FmtWidth|FmtLeft);
389
return __fmtcpy(fmt, s, strlen(s), strlen(s));
392
s = special[2+ucase];
396
s = special[4+ucase];
401
* get exact representation.
404
xdtoa(f, digits, &exp, &neg, &ndigits);
407
* get locale's decimal point.
412
dotwid = utflen(dot);
415
* now the formatting fun begins.
416
* compute parameters for actual fmt:
418
* pad: number of spaces to insert before/after field.
419
* z1: number of zeros to insert before digits
420
* z2: number of zeros to insert after digits
421
* point: number of digits to print before decimal point
422
* ndigits: number of digits to use from digits[]
423
* suf: trailing suffix, like "e-5"
429
* convert to at most prec significant digits. (prec=0 means 1)
434
if(digits[prec] >= '5' && xadd1(digits, prec))
441
* extra rules for %g (implemented below):
442
* trailing zeros removed after decimal unless FmtSharp.
443
* decimal point only if digit follows.
446
/* fall through to %e */
450
* one significant digit before decimal, no leading zeros.
456
* decimal point is after ndigits digits right now.
457
* slide to be after first.
459
e = exp + (ndigits-1);
462
* if this is %g, check exponent and convert prec
465
if(-4 <= e && e < prec)
467
prec--; /* one digit before decimal; rest after */
471
* compute trailing zero padding or truncate digits.
473
if(1+prec >= ndigits)
474
z2 = 1+prec - ndigits;
479
assert(realchr != 'g');
481
if(digits[newndigits] >= '5' && xadd1(digits, newndigits)) {
483
* had 999e4, now have 100e5
487
ndigits = newndigits;
490
xfmtexp(suf, e, ucase);
491
sufwid = strlen(suf);
497
* determine where digits go with respect to decimal point
499
if(ndigits+exp > 0) {
504
z1 = 1 + -(ndigits+exp);
508
* %g specifies prec = number of significant digits
509
* convert to number of digits after decimal point
515
* compute trailing zero padding or truncate digits.
517
if(point+prec >= z1+ndigits)
518
z2 = point+prec - (z1+ndigits);
523
assert(realchr != 'g');
524
newndigits = point+prec - z1;
528
} else if(newndigits == 0) {
529
/* perhaps round up */
530
if(digits[0] >= '5'){
535
} else if(digits[newndigits] >= '5' && xadd1(digits, newndigits)) {
537
* digits was 999, is now 100; make it 1000
539
digits[newndigits++] = '0';
542
* account for new digit
544
if(z1) /* 0.099 => 0.100 or 0.99 => 1.00*/
546
else /* 9.99 => 10.00 */
550
ndigits = newndigits;
557
* if %g is given without FmtSharp, remove trailing zeros.
558
* must do after truncation, so that e.g. print %.3g 1.001
559
* produces 1, not 1.00. sorry, but them's the rules.
561
if(realchr == 'g' && !(fl & FmtSharp)) {
562
if(z1+ndigits+z2 >= point) {
563
if(z1+ndigits < point)
564
z2 = point - (z1+ndigits);
567
while(z1+ndigits > point && digits[ndigits-1] == '0')
574
* compute width of all digits and decimal point and suffix if any
579
else if(wid == point){
583
point++; /* do not print any decimal point */
593
else if(fl & FmtSign)
595
else if(fl & FmtSpace)
604
if((fl & FmtWidth) && fmt->width > wid)
605
pad = fmt->width - wid;
606
if(pad && !(fl & FmtLeft) && (fl & FmtZero)){
613
* format the actual field. too bad about doing this twice.
616
if(pad && !(fl & FmtLeft) && __rfmtpad(fmt, pad) < 0)
619
rs = (Rune*)fmt->stop;
621
FMTRCHAR(fmt, rt, rs, sign);
622
while(z1>0 || ndigits>0 || z2>0) {
626
}else if(ndigits > 0){
633
FMTRCHAR(fmt, rt, rs, c);
636
p += chartorune(&r, p);
637
FMTRCHAR(fmt, rt, rs, r);
641
fmt->nfmt += rt - (Rune*)fmt->to;
643
if(sufwid && __fmtcpy(fmt, suf, sufwid, sufwid) < 0)
645
if(pad && (fl & FmtLeft) && __rfmtpad(fmt, pad) < 0)
648
if(pad && !(fl & FmtLeft) && __fmtpad(fmt, pad) < 0)
651
s = (char*)fmt->stop;
653
FMTCHAR(fmt, t, s, sign);
654
while(z1>0 || ndigits>0 || z2>0) {
658
}else if(ndigits > 0){
665
FMTCHAR(fmt, t, s, c);
668
FMTCHAR(fmt, t, s, *p);
670
fmt->nfmt += t - (char*)fmt->to;
672
if(sufwid && __fmtcpy(fmt, suf, sufwid, sufwid) < 0)
674
if(pad && (fl & FmtLeft) && __fmtpad(fmt, pad) < 0)