1

为什么这两个不返回相同的结果?

    D = data.frame( x=c( 0.6 ) )

    D$binned = cut( D$x, seq( 0.50,0.70,0.025 ), include.lowest=TRUE, right=FALSE )
    D # 0.6 is binned correctly as [0.6,0.625)

    D$binned = cut( D$x, seq( 0.55,0.65,0.025 ), include.lowest=TRUE, right=FALSE )
    D # 0.6 is binned incorrectly as [0.575,0.6)
4

2 回答 2

5

表示错误。仅当数字是 2 的某些幂的组合时,数字的浮点近似才是精确的。其他数字映射到这些数字。产生数字的不同算法可能会以不同的方式进行,并具有与之相关的不同错误(即高于或低于预期值)。在这种情况下:

print(D$x,digits=22)
[1] 0.5999999999999999777955
print(seq(0.5,0.7,0.025)[5],digits=22)
[1] 0.5999999999999999777955
> print(seq(0.55,0.65,0.025)[3],digits=22)
[1] 0.6000000000000000888178
于 2013-07-12T14:39:22.377 回答
1

D$binned = cut( D$x, round(seq( 0.55,0.65,0.025 ),3), include.lowest=TRUE, right=FALSE )

D

x binned

1 0.6 [0.6,0.625)

于 2013-07-13T08:41:06.187 回答