为什么浮动取0.699999而不是0.7 [英] Why float taking 0.699999 instead of 0.7
本文介绍了为什么浮动取0.699999而不是0.7的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这里,x取0.699999而不是0.7,但y取分配的0.5。你能告诉我这种行为的确切原因是什么。
Here x is taking 0.699999 instead of 0.7 but y is taking 0.5 as assigned. Can you tell me what is the exact reason for this behavior.
#include<iostream>
using namespace std;
int main()
{
float x = 0.7;
float y = 0.5;
if (x < 0.7)
{
if (y < 0.5)
cout<<"2 is right"<<endl;
else
cout<<"1 is right"<<endl;
}
else
cout<<"0 is right"<<endl;
cin.get();
return 0;
}
推荐答案
关于IEEE浮点的互联网。
There are lots of things on the internet about IEEE floating point.
0.5 = 1/2
所以可以写成两个权力的总和。
so can be written exactly as a sum of powers of two
0.7 = 7/10 = 1/2 + 1/5 = 1/2 + 1/8 + a bit more... etc
位更多
永远不能完全是一个二的幂,所以你得到最近的它可以管理。
The bit more
can never be exactly a power of two, so you get the closest it can manage.
这篇关于为什么浮动取0.699999而不是0.7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文