1 /* The authors of this software are Rob Pike and Ken Thompson.
2 * Copyright (c) 2002 by Lucent Technologies.
3 * Permission to use, copy, modify, and distribute this software for any
4 * purpose without fee is hereby granted, provided that this entire notice
5 * is included in all copies of any software which is or includes a copy
6 * or modification of this software and in all copies of the supporting
7 * documentation for such software.
8 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
9 * WARRANTY. IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE ANY
10 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
11 * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
23 typedef unsigned long ulong;
25 enum { NSIGNIF = 17 };
28 * first few powers of 10, enough for about 1/2 of the
29 * total space for doubles.
31 static double pows10[] =
33 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
34 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
35 1e20, 1e21, 1e22, 1e23, 1e24, 1e25, 1e26, 1e27, 1e28, 1e29,
36 1e30, 1e31, 1e32, 1e33, 1e34, 1e35, 1e36, 1e37, 1e38, 1e39,
37 1e40, 1e41, 1e42, 1e43, 1e44, 1e45, 1e46, 1e47, 1e48, 1e49,
38 1e50, 1e51, 1e52, 1e53, 1e54, 1e55, 1e56, 1e57, 1e58, 1e59,
39 1e60, 1e61, 1e62, 1e63, 1e64, 1e65, 1e66, 1e67, 1e68, 1e69,
40 1e70, 1e71, 1e72, 1e73, 1e74, 1e75, 1e76, 1e77, 1e78, 1e79,
41 1e80, 1e81, 1e82, 1e83, 1e84, 1e85, 1e86, 1e87, 1e88, 1e89,
42 1e90, 1e91, 1e92, 1e93, 1e94, 1e95, 1e96, 1e97, 1e98, 1e99,
43 1e100, 1e101, 1e102, 1e103, 1e104, 1e105, 1e106, 1e107, 1e108, 1e109,
44 1e110, 1e111, 1e112, 1e113, 1e114, 1e115, 1e116, 1e117, 1e118, 1e119,
45 1e120, 1e121, 1e122, 1e123, 1e124, 1e125, 1e126, 1e127, 1e128, 1e129,
46 1e130, 1e131, 1e132, 1e133, 1e134, 1e135, 1e136, 1e137, 1e138, 1e139,
47 1e140, 1e141, 1e142, 1e143, 1e144, 1e145, 1e146, 1e147, 1e148, 1e149,
48 1e150, 1e151, 1e152, 1e153, 1e154, 1e155, 1e156, 1e157, 1e158, 1e159,
50 #define npows10 ((int)(sizeof(pows10)/sizeof(pows10[0])))
51 #define pow10(x) fmtpow10(x)
68 d = pows10[npows10-1];
75 d *= pows10[npows10 - 1];
84 * add 1 to the decimal integer string a of length n.
85 * if 99999 overflows into 10000, return 1 to tell caller
86 * to move the virtual decimal point.
94 if(n < 0 || n > NSIGNIF)
96 for(b = a+n-1; b >= a; b--) {
105 * need to overflow adding digit.
106 * shift number down and insert 1 at beginning.
107 * decimal is known to be 0s or we wouldn't
108 * have gotten this far. (e.g., 99999+1 => 00000)
115 * subtract 1 from the decimal integer string a.
116 * if 10000 underflows into 09999, make it 99999
117 * and return 1 to tell caller to move the virtual
118 * decimal point. this way, xsub1 is inverse of xadd1.
121 xsub1(char *a, int n)
126 if(n < 0 || n > NSIGNIF)
128 for(b = a+n-1; b >= a; b--) {
131 if(c == '0' && b == a) {
133 * just zeroed the top digit; shift everyone up.
134 * decimal is known to be 9s or we wouldn't
135 * have gotten this far. (e.g., 10000-1 => 09999)
146 * can't get here. the number a is always normalized
147 * so that it has a nonzero first digit.
153 * format exponent like sprintf(p, "e%+d", e)
156 js_fmtexp(char *p, int e)
169 se[i++] = e % 10 + '0';
180 * compute decimal integer m, exp such that:
182 * m is as short as possible with losing exactness
183 * assumes special cases (NaN, +Inf, -Inf) have been handled.
186 js_dtoa(double f, char *s, int *exp, int *neg, int *ns)
188 int c, d, e2, e, ee, i, ndigit, oerrno;
189 char tmp[NSIGNIF+10];
192 oerrno = errno; /* in case strtod smashes errno */
195 * make f non-negative.
204 * must handle zero specially.
215 * find g,e such that f = g*10^e.
216 * guess 10-exponent using 2-exponent, then fine tune.
219 e = (int)(e2 * .301029995664);
231 * convert NSIGNIF digits as a first approximation.
233 for(i=0; i<NSIGNIF; i++) {
241 * adjust e because s is 314159... not 3.14159...
244 js_fmtexp(s+NSIGNIF, e);
247 * adjust conversion until strtod(s) == f exactly.
249 for(i=0; i<10; i++) {
250 g = js_strtod(s, NULL);
252 if(xadd1(s, NSIGNIF)) {
255 js_fmtexp(s+NSIGNIF, e);
260 if(xsub1(s, NSIGNIF)) {
263 js_fmtexp(s+NSIGNIF, e);
271 * play with the decimal to try to simplify.
275 * bump last few digits up to 9 if we can
277 for(i=NSIGNIF-1; i>=NSIGNIF-3; i--) {
281 g = js_strtod(s, NULL);
290 * add 1 in hopes of turning 9s to 0s
292 if(s[NSIGNIF-1] == '9') {
295 if(xadd1(tmp, NSIGNIF)) {
297 js_fmtexp(tmp+NSIGNIF, ee);
299 g = js_strtod(tmp, NULL);
307 * bump last few digits down to 0 as we can.
309 for(i=NSIGNIF-1; i>=NSIGNIF-3; i--) {
313 g = js_strtod(s, NULL);
322 * remove trailing zeros.
325 while(ndigit > 1 && s[ndigit-1] == '0'){
336 umuldiv(ulong a, ulong b, ulong c)
340 d = ((double)a * (double)b) / (double)c;
347 * This routine will convert to arbitrary precision
348 * floating point entirely in multi-precision fixed.
349 * The answer is the closest floating point number to
350 * the given decimal number. Exactly half way are
351 * rounded ala ieee rules.
352 * Method is to scale input decimal between .500 and .999...
353 * with external power of 2, then binary search for the
354 * closest mantissa to this decimal number.
355 * Nmant is is the required precision. (53 for ieee dp)
356 * Nbits is the max number of bits/word. (must be <= 28)
357 * Prec is calculated - the number of words of fixed mantissa.
361 Nbits = 28, /* bits safely represented in a ulong */
362 Nmant = 53, /* bits of precision required */
363 Prec = (Nmant+Nbits+1)/Nbits, /* words of Nbits each to represent mantissa */
364 Sigbit = 1<<(Prec*Nbits-Nmant), /* first significant bit of Prec-th word */
366 One = (ulong)(1<<Nbits),
367 Half = (ulong)(One>>1),
370 Fsign = 1<<0, /* found - */
371 Fesign = 1<<1, /* found e- */
372 Fdpoint = 1<<2, /* found . */
374 S0 = 0, /* _ _S0 +S1 #S2 .S3 */
376 S2, /* _+# #S2 .S4 eS5 */
378 S4, /* _+#.# #S4 eS5 */
379 S5, /* _+#.#e +S6 #S7 */
380 S6, /* _+#.#e+ #S7 */
381 S7 /* _+#.#e+# #S7 */
384 static int xcmp(char*, char*);
385 static int fpcmp(char*, ulong*);
386 static void frnorm(ulong*);
387 static void divascii(char*, int*, int*, int*);
388 static void mulascii(char*, int*, int*, int*);
390 typedef struct Tab Tab;
399 js_strtod(const char *as, char **aas)
401 int na, ex, dp, bp, c, i, flag, state;
402 ulong low[Prec], hig[Prec], mid[Prec];
406 flag = 0; /* Fsign, Fesign, Fdpoint */
407 na = 0; /* number of digits of a[] */
408 dp = 0; /* na of decimal point */
409 ex = 0; /* exonent */
412 for(s=(char*)as;; s++) {
414 if(c >= '0' && c <= '9') {
430 ex = ex*10 + (c-'0');
433 if(na == 0 && c == '0') {
469 if(state == S0 || state == S1) {
480 if(state == S2 || state == S4) {
490 * clean up return char-pointer
494 if(xcmp(s, "nan") == 0) {
501 if(xcmp(s, "infinity") == 0) {
506 if(xcmp(s, "inf") == 0) {
515 goto ret0; /* no digits found */
517 s--; /* back over +- */
520 s--; /* back over e */
527 while(na > 0 && a[na-1] == '0')
530 goto ret0; /* zero */
532 if(!(flag & Fdpoint))
539 goto ret0; /* underflow by exp */
542 goto retinf; /* overflow by exp */
545 * normalize the decimal ascii number
546 * to range .[5-9][0-9]* e0
548 bp = 0; /* binary exponent */
550 divascii(a, &na, &dp, &bp);
551 while(dp < 0 || a[0] < '5')
552 mulascii(a, &na, &dp, &bp);
554 /* close approx by naive conversion */
557 for(i=0; (c=a[i]) != '\0'; i++) {
558 mid[0] = mid[0]*10 + (c-'0');
563 low[0] = umuldiv(mid[0], One, mid[1]);
564 hig[0] = umuldiv(mid[0]+1, One, mid[1]);
565 for(i=1; i<Prec; i++) {
570 /* binary search for closest mantissa */
572 /* mid = (hig + low) / 2 */
574 for(i=0; i<Prec; i++) {
575 mid[i] = hig[i] + low[i];
587 for(i=0; i<Prec; i++)
588 if(low[i] != mid[i]) {
593 break; /* between mid and hig */
597 for(i=0; i<Prec; i++)
602 /* only hard part is if even/odd roundings wants to go up */
603 c = mid[Prec-1] & (Sigbit-1);
604 if(c == Sigbit/2 && (mid[Prec-1]&Sigbit) == 0)
606 break; /* exactly mid */
609 /* normal rounding applies */
610 c = mid[Prec-1] & (Sigbit-1);
613 mid[Prec-1] += Sigbit;
628 * Unix strtod requires these. Plan 9 would return Inf(0) or Inf(-1). */
636 for(i=0; i<Prec; i++)
640 d = ldexp(d, bp - Prec*Nbits);
641 if(d == 0){ /* underflow */
653 for(i=Prec-1; i>0; i--) {
662 fpcmp(char *a, ulong* f)
667 for(i=0; i<Prec; i++)
672 for(i=0; i<Prec; i++)
675 d = (tf[0] >> Nbits) + '0';
678 /* compare next digit */
685 for(i=1; i<Prec; i++)
700 divby(char *a, int *na, int b)
751 { 23, 7, "8388607" },
752 { 26, 8, "67108863" },
753 { 27, 9, "134217727" },
757 divascii(char *a, int *na, int *dp, int *bp)
763 if(d >= (int)(nelem(tab1)))
764 d = (int)(nelem(tab1))-1;
767 if(memcmp(a, t->cmp, t->siz) > 0)
775 mulby(char *a, char *p, char *q, int b)
803 { 1, 1, "" }, /* dp = 0-0 */
807 { 13, 10, "1220703125" },
808 { 16, 12, "152587890625" },
809 { 19, 14, "19073486328125" },
810 { 23, 17, "11920928955078125" },
811 { 26, 19, "1490116119384765625" },
812 { 27, 19, "7450580596923828125" }, /* dp 8-9 */
816 mulascii(char *a, int *na, int *dp, int *bp)
823 if(d >= (int)(nelem(tab2)))
824 d = (int)(nelem(tab2))-1;
827 if(memcmp(a, t->cmp, t->siz) < 0)
837 xcmp(char *a, char *b)
841 while((c1 = *b++) != '\0') {
843 if(c2 >= 'A' && c2 <= 'Z')