X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=lib%2Fceil.c;h=b5a0316cf1798e2a773da99103f04b696d3681ed;hb=9fbf8e1a391ab49dca9c608a32af9a42831f3445;hp=2538bf64d412720275960a58995f09d5b3bdc0fb;hpb=57fdfd3f8ec62b105c53bcdf6f127c35c7fe7391;p=gnulib.git diff --git a/lib/ceil.c b/lib/ceil.c index 2538bf64d..b5a0316cf 100644 --- a/lib/ceil.c +++ b/lib/ceil.c @@ -41,7 +41,7 @@ #endif /* 2^(MANT_DIG-1). */ -static const double TWO_MANT_DIG = +static const DOUBLE TWO_MANT_DIG = /* Assume MANT_DIG <= 5 * 31. Use the identity n = floor(n/5) + floor((n+1)/5) + ... + floor((n+4)/5). */ @@ -63,20 +63,31 @@ FUNC (DOUBLE x) volatile DOUBLE y = x; volatile DOUBLE z = y; - /* Round to the next integer (nearest or up or down, doesn't matter). */ if (z > L_(0.0)) { - z += TWO_MANT_DIG; - z -= TWO_MANT_DIG; + /* Avoid rounding errors for values near 2^k, where k >= MANT_DIG-1. */ + if (z < TWO_MANT_DIG) + { + /* Round to the next integer (nearest or up or down, doesn't matter). */ + z += TWO_MANT_DIG; + z -= TWO_MANT_DIG; + /* Enforce rounding up. */ + if (z < y) + z += L_(1.0); + } } else if (z < L_(0.0)) { - z -= TWO_MANT_DIG; - z += TWO_MANT_DIG; + /* Avoid rounding errors for values near -2^k, where k >= MANT_DIG-1. */ + if (z > - TWO_MANT_DIG) + { + /* Round to the next integer (nearest or up or down, doesn't matter). */ + z -= TWO_MANT_DIG; + z += TWO_MANT_DIG; + /* Enforce rounding up. */ + if (z < y) + z += L_(1.0); + } } - /* Enforce rounding up. */ - if (z < y) - z += L_(1.0); - return z; }