双重不等式,同时对三个浮点数进行求和 [英] Double inequality while comparying three floating point numbers
本文介绍了双重不等式,同时对三个浮点数进行求和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有人可以告诉我以下内容为什么不起作用吗?(我的意思是没有输出)
Can somebody tell me why the following does not work?(I mean no output)
if(0.0001<0.001<0.01)
cout<<"hi\n"<<endl;
output: (blank)
以下工作原理:
if(0.0001<0.001 && 0.001<0.01)
cout<<"hi\n"<<endl;
output:hi
推荐答案
因为C ++中没有神奇的n元<
运算符.
Because there is no magical n-ary <
operator in C++.
0.0001 < 0.001 < 0.01
被解析为(因为<
是左关联的)
is parsed (since <
is left-associative) as
(0.0001 < 0.001) < 0.01
和0.0001 < 0.001
返回值为true
的类型为bool
的值.现在你有
and 0.0001 < 0.001
returns a value of type bool
with value true
. Now you have
true < 0.01
但是根据标准,true
布尔值在转换为整数类型时具有值1,因此您具有
but according to the standard a true
boolean has value 1 when converted to an integral type so you have
1 < 0.01
这是错误的.
这篇关于双重不等式,同时对三个浮点数进行求和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文