[英]Compare sequence of floats to vector
This is so silly I'm almost afraid to ask.这太愚蠢了,我几乎不敢问。 But here goes.
但在这里。
> b <- c( 0.54, 0.56, 0.58, 0.6 )
> s <- seq( 0.54, 0.60, 0.02 )
I would expect these two to be equal and yet...我希望这两个是平等的,但......
> b == s
[1] TRUE TRUE FALSE TRUE
What gives?是什么赋予了? I must be missing something really dumb.
我一定错过了一些非常愚蠢的东西。 By the way, I had a colleague reproduce the same on his machine.
顺便说一下,我有一个同事在他的机器上复制了同样的内容。 I'm using
我正在使用
> sessionInfo()
R version 3.0.0 (2013-04-03)
Platform: x86_64-w64-mingw32/x64 (64-bit)
locale:
[1] LC_COLLATE=English_Canada.1252 LC_CTYPE=English_Canada.1252 LC_MONETARY=English_Canada.1252
[4] LC_NUMERIC=C LC_TIME=English_Canada.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] tools_3.0.0
A detail about the behaviour of seq
in the docs, maybe?可能是文档中有关
seq
行为的详细信息?
Updated: See answer below and here .更新:见下面和这里的答案。 The lesson: Never forget that it's all floating point arithmetic .
教训:永远不要忘记这都是浮点运算。
Try using all.equal
function尝试使用
all.equal
函数
> all.equal(b,s)
[1] TRUE
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.