X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=lib%2Ffloor.c;h=05a6591a4b40a183307ed04d9f4e04f22a4f64d1;hb=56093e4d947bcc87bbc05ac3e8645509274f57b4;hp=7b5c9c9e33dcd1b3ccf6b33c8bb12adbd4c6d9fa;hpb=57fdfd3f8ec62b105c53bcdf6f127c35c7fe7391;p=gnulib.git diff --git a/lib/floor.c b/lib/floor.c index 7b5c9c9e3..05a6591a4 100644 --- a/lib/floor.c +++ b/lib/floor.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 down. */ + 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 down. */ + if (z > y) + z -= L_(1.0); + } } - /* Enforce rounding down. */ - if (z > y) - z -= L_(1.0); - return z; }