小编use*_*274的帖子

如何检查浮动的依赖关系

我想确定(在c ++中)一个浮点数是否是另一个浮点数的乘法逆.问题是我必须使用第三个变量来完成它.例如这段代码:

float x=5,y=0.2;
if(x==(1/y)) cout<<"They are the multiplicative inverse of eachother"<<endl;
else cout<<"They are NOT the multiplicative inverse of eachother"<<endl;
Run Code Online (Sandbox Code Playgroud)

将输出:"他们不是......"这是错的,这段代码:

float x=5,y=0.2,z;
z=1/y;
if(x==z) cout<<"They are the multiplicative inverse of eachother"<<endl;
else cout<<"They are NOT the multiplicative inverse of eachother"<<endl;
Run Code Online (Sandbox Code Playgroud)

将输出:"他们......"这是正确的.
为什么会这样?

c++ algorithm floating-point inverse floating-accuracy

12
推荐指数
3
解决办法
794
查看次数