Mercurial > cpython-lru_cache-weakref
diff Python/dtoa.c @ 58278:b4f301cea115 legacy-trunk
Issue #7632: Fix a bug in dtoa.c that could lead to incorrectly-rounded results.
| author | Mark Dickinson <dickinsm@gmail.com> |
|---|---|
| date | Tue, 12 Jan 2010 22:55:51 +0000 |
| parents | 2a9f66df4dc4 |
| children | 57102283ab5e |
line wrap: on
line diff
--- a/Python/dtoa.c Tue Jan 12 22:23:56 2010 +0000 +++ b/Python/dtoa.c Tue Jan 12 22:55:51 2010 +0000 @@ -1130,6 +1130,26 @@ return q; } +/* version of ulp(x) that takes bc.scale into account. + + Assuming that x is finite and nonzero, and x / 2^bc.scale is exactly + representable as a double, sulp(x) is equivalent to 2^bc.scale * ulp(x / + 2^bc.scale). */ + +static double +sulp(U *x, BCinfo *bc) +{ + U u; + + if (bc->scale && 2*P + 1 - ((word0(x) & Exp_mask) >> Exp_shift) > 0) { + /* rv/2^bc->scale is subnormal */ + word0(&u) = (P+2)*Exp_msk1; + word1(&u) = 0; + return u.d; + } + else + return ulp(x); +} /* return 0 on success, -1 on failure */ @@ -1289,12 +1309,12 @@ else if (dd < 0) { if (!dsign) /* does not happen for round-near */ retlow1: - dval(rv) -= ulp(rv); + dval(rv) -= sulp(rv, bc); } else if (dd > 0) { if (dsign) { rethi1: - dval(rv) += ulp(rv); + dval(rv) += sulp(rv, bc); } } else {
