蒟蒻求助

学术版

**希望更丰富的展现?使用Markdown**
by Limit @ 2019-03-16 17:42:13


``` #include<bits/stdc++.h> using namespace std; int main(){ double i=10; double x=10*0.1; if(x==1) cout<<"x==1\n"; if(10*0.1==1) cout<<"10*0.1==1\n"; if(i*0.1==1) cout<<"i*0.1==1\n"; return 0; } ``` 求助,为什么第三个判断不会成立?
by yaowei123 @ 2019-03-16 17:43:33


@[yaowei123](/space/show?uid=174057) 浮点误差,请使用EPS修正
by ZPC2048 @ 2019-03-16 17:57:04


@[ZPC2048](/space/show?uid=10337) 但是为什么10*0.1就没有浮点误差?
by yaowei123 @ 2019-03-16 18:02:23


@[yaowei123](/space/show?uid=174057) 这涉及到double的存储形式,百度一下吧,我也不是很懂QWQ23333
by ZPC2048 @ 2019-03-16 18:07:14


不会不成立啊
by SSerxhs @ 2019-03-16 18:07:31


本机三条都输出了
by SSerxhs @ 2019-03-16 18:07:45


@[yaowei123](/space/show?uid=174057) 对了,涉及到double的相等性判断,切忌使用==,EPS修正是常规操作。
by ZPC2048 @ 2019-03-16 18:11:05


```cpp #include<bits/stdc++.h> using namespace std; int main(){ double i=10.0; double x=10.0*0.1; if(x==1.0) cout<<"1\n"; if(10.0*0.1==1.0) cout<<"2\n"; if(i*0.1==1.0) cout<<"3\n"; return 0; } ``` 这样试试
by DARKSTALKING @ 2019-03-16 18:19:08


@[yaowei123](/space/show?uid=174057)
by DARKSTALKING @ 2019-03-16 18:19:17


| 下一页