小编boj*_*oja的帖子

math.h ceil在C中没有按预期工作

为什么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进行编译.

c compiler-construction math floating-point

6
推荐指数
3
解决办法
2374
查看次数

标签 统计

c ×1

compiler-construction ×1

floating-point ×1

math ×1