C ++怀疑 [英] C++ doubt

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

问题描述

您好,

我对C ++有两个疑问。

1. float i = 1.1;

double d = 1.1;

if(i == d)

printf(" equal \ n");

else

printf (not-equal\\\
);

输出是什么?

我得到的不等于作为输出。

为什么会这样?

Hello,
I have two doubts on C++.
1. float i=1.1;
double d=1.1;
if(i==d)
printf("equal\n");
else
printf("not-equal\n");
what is the output?
I am getting "not-equal" as output.
Why it is so?

推荐答案

这是精确问题

对于2个浮点数,你应该使用''> =''或''< =''运算符,而不是''==''

运算符来判断它们是否相等

This is a precision problem
For 2 float numbers you should use ''>='' or ''<='' operator, not ''==''
operator to judge if they equal




" santosh" < SA ************* @ in.bosch.com> skrev i en meddelelse

news:d6 ********** @ ns2.fe.internet.bosch.com ...

"santosh" <sa*************@in.bosch.com> skrev i en meddelelse
news:d6**********@ns2.fe.internet.bosch.com...
你好, 1. float i = 1.1;
double d = 1.1;
if(i == d)
printf(equal \\ n \\ n;);
其他
printf(not-equal \ n);
输出是什么?
我得到的不等于作为输出。
为什么会这样?
Hello,
I have two doubts on C++.
1. float i=1.1;
double d=1.1;
if(i==d)
printf("equal\n");
else
printf("not-equal\n");
what is the output?
I am getting "not-equal" as output.
Why it is so?



float和double是不精确的类型,它们都不能准确

代表1.1 。当float和double得到比较时,float是

首先将类型提升为double。在这个过程中,两种类型的

准确度之间的差异表现为:两个值不同。


/ Peter


float and double are imprecise types and none of them can accurately
represent 1.1. When the float and the double get compared, the float is
first type-promoted to double. In this process, the difference between the
accuracy of the two types manifests itself: the two values are different.

/Peter


santosh写道:
santosh wrote:
你好,
我对C ++有两个疑问。
1. float i = 1.1;
double d = 1.1;
if(i == d)
printf(equal\ n);

printf(" not-equal\\\
) ;
输出是什么?
我得到的不平等作为输出。
为什么会这样?
Hello,
I have two doubts on C++.
1. float i=1.1;
double d=1.1;
if(i==d)
printf("equal\n");
else
printf("not-equal\n");
what is the output?
I am getting "not-equal" as output.
Why it is so?




C和C ++都可以输出不是 - 等于"对于这个。


它与如何在可用的有限位中表示无限范围的可能

值有关

存储值。大多数浮点值都不能使用计算机中使用的二进制表示

精确存储。由于''float''和''double''可能是不同的大小(即使用不同数量的二进制

位来保存它们的值),它们每个都存储一个稍微

不同的值接近1.1。


有关详细信息,请参阅常见问题解答第29.16和29.17节:

http://www.parashift.com/c++-faq-点亮... html#faq-29.16


问候,

拉里


-

反垃圾邮件地址,将每个''X'改为''。''直接回复。



Both ''C'' and ''C++'' may output "not-equal" for this.

It has to do with how an infinite range of possible
values are represented in the limited bits available
to store the values. Most floating point values can
not be stored precisely using the binary representation
used in computers. Since ''float'' and ''double'' may be
different sizes (i.e. use differing number of binary
bits to hold their value), they each store a slightly
different value that approximates 1.1.

See the FAQ sections 29.16 and 29.17 for more details:

http://www.parashift.com/c++-faq-lit...html#faq-29.16

Regards,
Larry

--
Anti-spam address, change each ''X'' to ''.'' to reply directly.


这篇关于C ++怀疑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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