两个值看似相同,但是matlab说 [英] two values are seemingly the same, yet matlab says the aren't
本文介绍了两个值看似相同,但是matlab说的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我无法解释这一点.我有两个具有相同值的变量,但是它们并不相同.谁能告诉我我在这里想念的东西吗?
I cannot explain this. I have two variables having the same value, yet they are not identical. Can anybody tell me what I'm missing here?
>> y
y =
3.4000
>> x
x =
3.4000
>> y==x
ans =
0
>> whos x
Name Size Bytes Class Attributes
x 1x1 8 double
>> whos y
Name Size Bytes Class Attributes
y 1x1 8 double
这真的让我感到困惑,我发誓这不是在开玩笑.
It's really puzzling for me and I swear it's not a joke.
推荐答案
这是因为浮点精度.试试
It's because of floating point precision. Try
format long g
,然后再次查看x
和y
.最好将x-y
与一些较小的公差值进行比较,而不是对浮点数进行相等性测试
and then look at x
and y
again. It's better to compare x-y
to some small tolerance value rather than doing an equality test on floating point numbers
这篇关于两个值看似相同,但是matlab说的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文