Ceil双重问题 [英] Ceil double problem

查看:77
本文介绍了Ceil双重问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我从此得到NO?


double du =(double)3.1415;

du = ceil(du);

if(du ==(double)4)

printf(" YES \ n");

else

printf(NO \ n);

Why do I get NO as result from this ??

double du = (double)3.1415;
du = ceil(du);
if(du == (double)4)
printf("YES\n");
else
printf("NO\n");

推荐答案


mi ************** @ gmail.com 写道:
为什么我从这里得到NO?

double du =(double)3.1415;
du = ceil(du);
if(du = =(双)4)
printf(" YES \ n");

printf(" NO\ n");
Why do I get NO as result from this ??

double du = (double)3.1415;
du = ceil(du);
if(du == (double)4)
printf("YES\n");
else
printf("NO\n");




我得到YES(mingw / gcc)。您的机器可能无法完全代表4.0

。您的`ceil()`的实现可能会被破坏。你有

包括< math.h>?您是否尝试在

`ceil()之前和之后打印`du`?我可能错过了其他一些可能性......


-

BR,弗拉基米尔



I get YES (mingw/gcc). Your machine may not be able to represent 4.0
exactly. Your implementation of `ceil()` may be broken. Have you
included <math.h>? Did you try to print out `du` before and after
`ceil()`? I may have missed some other possibilities...

--
BR, Vladimir

mi ************** @ gmail。 com 写道:
为什么我从这里得到NO?

double du =(double)3.1415;
du = ceil( ()=(=) );
Why do I get NO as result from this ??

double du = (double)3.1415;
du = ceil(du);
if(du == (double)4)
printf("YES\n");
else
printf("NO\n");




我不知道。我希望它能打印出来,当我编译你的代码时,它就会这样做 - 但只有在我把它放到一个实际程序之后才会这样做。


你已经发布了一个代码片段,而不是一个程序,所以严格来说它

根本不会产生任何输出。


我能想到的唯一解释是你的实际节目

没有必要的#include< math.h>。这可能导致

编译器假设ceil()返回一个int而不是double,

导致未定义的行为。


如果你仍然没有想到它,请发布一个完整的可编辑的

程序来重现问题。


顺便提一下,演员阵容是不必要的。一个浮点字面值

如3.1415已经是float类型;在比较中,它使用4.0更简单,更清洁。而不是(双倍)4。


-

基思汤普森(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。



I don''t know. I''d expect it to print YES, and it did so when I
compiled your code -- but only after I put it into an actual program.

You''ve posted a code fragment, not a program, so strictly speaking it
doesn''t produce any output at all.

The only explanation I can think of is that your actual program
doesn''t have the required "#include <math.h>". This could cause the
compiler to assume that ceil() returns an int rather than a double,
resulting in undefined behavior.

If you still haven''t figured it out, post a complete compilable
program that reproduces the problem.

Incidentally, the casts are unnecessary. A floating-point literal
such as 3.1415 is already of type float; in the comparison, it''s
simpler and cleaner to use "4.0" rather than "(double)4)".

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


" Vladimir S. Oka" <无**** @ btopenworld.com>写道:
"Vladimir S. Oka" <no****@btopenworld.com> wrote:
mi ******* *******@gmail.com 写道:
为什么我从这里得到NO?

double du =(double) 3.1415;
du = ceil(du);
if(du ==(double)4)
printf(" YES \ n");
else printf(NO \ n);
Why do I get NO as result from this ??

double du = (double)3.1415;
du = ceil(du);
if(du == (double)4)
printf("YES\n");
else
printf("NO\n");



我得到YES(mingw / gcc)。你的机器可能无法完全代表4.0。



I get YES (mingw/gcc). Your machine may not be able to represent 4.0
exactly.




如果是这样,它就坏了。


Richard



If so, it''s broken.

Richard


这篇关于Ceil双重问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆