X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=lib%2Ffloor.c;h=05a6591a4b40a183307ed04d9f4e04f22a4f64d1;hb=56093e4d947bcc87bbc05ac3e8645509274f57b4;hp=7ee748601daeae96d4740eabcdbf1b845b1ab405;hpb=d9cd580560f5ba3bd4c669d78321eb9947f4d568;p=gnulib.git diff --git a/lib/floor.c b/lib/floor.c index 7ee748601..05a6591a4 100644 --- a/lib/floor.c +++ b/lib/floor.c @@ -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; }