Bug in std::floor?
Vincent Lefevre
vincent+gcc@vinc17.org
Thu Nov 9 15:53:00 GMT 2017
On 2017-11-09 16:46:58 +0100, Vincent Lefevre wrote:
> On 2017-11-09 10:01:45 +0100, Mason wrote:
> > Basically, using "IEEE 754 double-precision binary floating-point format"
> > 4.6 is approximated as D=4.5999999999999996447286321199499070644378662109375
> > ( https://en.wikipedia.org/wiki/Double-precision_floating-point_format )
> >
> > Thus, it is obvious why floor(D*100) equals 459.
>
> No, this is not obvious. The multiplication by 100 introduces a
> rounding error. Thus this doesn't prove that the rounded value
> D*100 is strictly less than 460.
For instance:
#include <stdio.h>
#include <math.h>
int main (void)
{
double x = 1.7;
printf ("%.30g\n", x);
printf ("%.30g\n", 100 * x);
printf ("%.30g\n", floor (100 * x));
return 0;
}
outputs:
1.69999999999999995559107901499
170
170
despite the fact that 1.7 is approximated by a value less than 1.7.
--
Vincent Lefèvre <vincent@vinc17.net> - Web: <https://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <https://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / AriC project (LIP, ENS-Lyon)
More information about the Gcc-help
mailing list