87 if(*x < (
ULong)0xffffffffL)
146 borrow = (y & 0x10000) >> 16;
148 }
while(borrow && x < xe);
229 (d, fpi, exp, bits, exact, rd, irv)
double d;
235 (
double d,
FPI* fpi,
Long* exp,
ULong* bits,
int exact,
int rd,
int* irv)
239 ULong carry, inex, lostbits;
240 int bdif, e, j, k, k1, nb, rv;
243 b =
d2b(d, &e, &bdif);
244 bdif -= nb = fpi->nbits;
259 #ifndef IMPRECISE_INEXACT
316 if((lostbits =
any_on(b, bdif)) != 0)
327 if((j = nb &
kmask) != 0)
338 lostbits = b->
x[0] & 1;
356 if(k > nb || fpi->sudden_underflow)
364 if(k1 > 0 && !lostbits)
370 if(!lostbits && !exact)
376 lostbits |= carry = b->
x[k1 >>
kshift] & (1 << (k1 &
kmask));
392 else if(e > fpi->emax)
422 if((L =
word1(d)) != 0)
437 (s00, se, fpi, exp, bits)
CONST char* s00;
446 int abe, abits, asub;
447 int bb0, bb2, bb5, bbe, bd2, bd5, bbbits, bs2, c, decpt, denorm;
448 int dsign, e, e1, e2, emin, esign, finished, i, inex, irv;
449 int j, k, nbits, nd, nd0, nf, nz, nz0, rd, rvbits, rve, rve1, sign;
450 int sudden_underflow = 0;
451 CONST char *s, *s0, *s1;
452 double adj, adj0, rv, tol;
455 Bigint *ab, *bb, *bb1, *bd, *bd0, *bs, *delta, *rvb, *rvb0;
458 denorm = sign = nz0 = nz = 0;
503 irv =
gethex(&s, fpi, exp, &rvb, sign);
526 sudden_underflow = fpi->sudden_underflow;
529 for(decpt = nd = nf = 0; (c = *s) >=
'0' && c <=
'9'; nd++, s++)
534 y = 10 * y + (unsigned)c -
'0';
538 z = 10 * z + (unsigned)c -
'0';
553 for(; c ==
'0'; c = *++s)
559 if(c >
'0' && c <=
'9')
568 for(; c >=
'0' && c <=
'9'; c = *++s)
575 for(i = 1; i < nz; i++)
590 y = 10 * y + (unsigned)c;
594 z = 10 * z + (unsigned)c;
602 if(c ==
'e' || c ==
'E')
604 if(!nd && !nz && !nz0)
619 if(c >=
'0' && c <=
'9')
627 if(c >
'0' && c <=
'9')
631 while((c = *++s) >=
'0' && c <=
'9')
634 L = 10 * L + c -
'0';
637 if(s - s1 > 8 || L > 19999)
687 if(!
match(&s,
"inity"))
698 *exp = fpi->emax + 1;
701 irv =
hexnan(&s, fpi, bits);
716 switch(fpi->rounding & 3)
752 if(
rvOK(
dval(rv), fpi, exp, bits, 1, rd, &irv))
768 if(
rvOK(
dval(rv), fpi, exp, bits, i, rd, &irv))
802 if(
rvOK(
dval(rv), fpi, exp, bits, 0, rd, &irv))
811 #ifndef Inaccurate_Divide 815 if(
rvOK(
dval(rv), fpi, exp, bits, 0, rd, &irv))
833 if((i = e1 & 15) != 0)
842 while(e1 >= (1 << (n_bigtens - 1)))
848 e1 -= 1 << (n_bigtens - 1);
853 for(j = 0; e1 > 0; j++, e1 >>= 1)
869 if((i = e1 & 15) != 0)
878 while(e1 >= (1 << (n_bigtens - 1)))
884 e1 -= 1 << (n_bigtens - 1);
889 for(j = 0; e1 > 0; j++, e1 >>= 1)
910 rvb =
d2b(
dval(rv), &rve, &rvbits);
912 if((j = rvbits - nbits) > 0)
919 e2 = rve + rvbits - nbits;
920 if(e2 > fpi->emax + 1)
926 rve1 = rve + rvbits - nbits;
927 if(e2 < (emin = fpi->emin))
950 rvb->
x[0] = rvb->
wds = rvbits = 1;
960 if(sudden_underflow && e2 + 1 < emin)
972 bd0 =
s2b(s0, nd0, nd, y);
980 bbbits = rvbits - bb0;
1007 j = nbits + 1 - bbbits;
1008 i = bbe + bbbits - nbits;
1017 i = bb2 < bd2 ? bb2 : bd2;
1070 delta =
diff(bb, bd);
1071 if(delta->
wds <= 1 && !delta->
x[0])
1077 dsign = delta->
sign;
1078 delta->
sign = finished = 0;
1084 if((finished = dsign ^ (rd & 1)) != 0)
1107 if(j > 1 &&
lo0bits(rvb->
x + i) < j - 1)
1114 rvb =
set_ones(rvb, rvbits = nbits);
1126 if(dsign || bbbits > 1 || denorm || rve1 == emin)
1132 delta =
lshift(delta, 1);
1133 if(
cmp(delta, bs) > 0)
1145 if(denorm &&
all_on(rvb, rvbits))
1150 rve = emin + nbits - (rvbits = 1);
1157 else if(bbbits == 1)
1165 if(rvb->
wds == 1 && rvb->
x[0] == 1)
1168 sudden_underflow = 1;
1174 rvb =
set_ones(rvb, rvbits = nbits);
1183 if(((bbbits < nbits) && !denorm) || !(rvb->
x[0] & 1))
1214 if((
dval(adj) =
ratio(delta, bs)) <= 2.)
1223 else if(denorm && bbbits <= 1)
1231 adj0 =
dval(adj) = 1.;
1235 adj0 =
dval(adj) *= 0.5;
1241 if(
dval(adj) < 2147483647.)
1256 if(asub && adj0 > 0.)
1264 if(!asub && adj0 > 0.)
1274 y = (
ULong)(rve + rvbits);
1279 if(!denorm && rvbits < nbits)
1281 rvb =
lshift(rvb, j = nbits - rvbits);
1285 ab =
d2b(
dval(adj), &abe, &abits);
1303 rvb =
diff(rvb, ab);
1336 if(++rvbits == nbits)
1361 z = (
ULong)(rve + rvbits);
1365 tol =
dval(adj) * 5e-16;
1366 dval(adj) = adj0 - .5;
1367 if(
dval(adj) < -tol)
1375 else if(
dval(adj) > tol && adj0 < 1. - tol)
1381 bb0 = denorm ? 0 :
trailz(rvb);
1387 if(!denorm && (j = nbits - rvbits))
1419 *exp = fpi->emax + 1;
1425 if(sudden_underflow)
1441 *se = (
char*)(uintptr_t)s;
int hexnan(CONST char **sp, FPI *fpi, ULong *x0)
ULong any_on(Bigint *b, int k)
static int all_on(Bigint *b, int n)
Bigint * lshift(Bigint *b, int k)
Bigint * diff(Bigint *a, Bigint *b)
Bigint * increment(Bigint *b)
struct lconv * localeconv(void)
int gethex(CONST char **sp, FPI *fpi, Long *exp, Bigint **bp, int sign)
Bigint * d2b(double d, int *e, int *bits)
#define rounded_product(a, b)
#define rounded_quotient(a, b)
double ratio(Bigint *a, Bigint *b)
void copybits(ULong *c, int n, Bigint *b)
Bigint * s2b(CONST char *s, int nd0, int nd, ULong y9)
static CONST int fivesbits[]
void rshift(Bigint *b, int k)
Bigint * pow5mult(Bigint *b, int k)
Bigint * set_ones(Bigint *b, int n)
static int rvOK(double d, FPI *fpi, Long *exp, ULong *bits, int exact, int rd, int *irv)
static int mantbits(double d)
int cmp(Bigint *a, Bigint *b)
Bigint * sum(Bigint *a, Bigint *b)
Bigint * mult(Bigint *a, Bigint *b)
int strtodg(CONST char *s00, char **se, FPI *fpi, Long *exp, ULong *bits)