math.h ceil not working as expected in C

How come ceil() rounds up an even floating with no fractional parts? When I try to do this: double x = 2.22; x *= 100; //which becomes 222.00... printf("%lf", ceil(x)); //prints 223.00... (?) But when I change the value of 2.22 to 2.21 x *= 100; //which becomes 221.00... printf("%lf", ceil(x)); //prints 221.00... as expected I tried to do it another way like this using modf() and encountered another strange thing: 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... So what happens is 0.000... is greater than 0? Can anyone explain why both of these situations are happening? Also I'm compiling using cc version 4.1.2 in RedHat.

以上就是math.h ceil not working as expected in C的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » CSS3 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准