为什么ceil()会在没有分数部分的情况下进行均匀浮动?
当我尝试这样做时:
double x = 2.22;
x *= 100; //which becomes 222.00...
printf("%lf", ceil(x)); //prints 223.00... (?)
Run Code Online (Sandbox Code Playgroud)
但是当我将2.22的值更改为2.21时
x *= 100; //which becomes 221.00...
printf("%lf", ceil(x)); //prints 221.00... as expected
Run Code Online (Sandbox Code Playgroud)
我尝试用另一种方式使用modf()并遇到另一个奇怪的事情:
double x = 2.22 * 100;
double num, fraction;
fraction = modf(x, &num);
if(fraction > 0)
num += 1; //goes inside here even when fraction is 0.00...
Run Code Online (Sandbox Code Playgroud)
那么会发生什么是0.000 ...大于0?
谁能解释为什么这两种情况都会发生?此外,我正在使用RedHat中的cc版本4.1.2进行编译.