[R] problem with sum function
Petr Savicky
savicky at cs.cas.cz
Thu Mar 1 23:29:00 CET 2012
On Thu, Mar 01, 2012 at 04:55:55PM -0500, Sarah Goslee wrote:
> Of course there's rounding error: your computer can't
> store those decimal numbers precisely. See R FAQ 7.31 for
> details.
>
> See also:
> sum(10*c(-0.2, 0.8, 0.8, -3.2, 1.8)) / 10
Hi.
This is 0. This works without rounding for one digit precision,
since we always have i == 10*(i/10). Already for two digits, we
may have i != 100*(i/100). So, for example
sum(100*c(0.28, -0.21, 0.66, -0.73))/100
[1] 3.552714e-17
Rounding the multiples to be integers yields the expected result
sum(round(100*c(0.28, -0.21, 0.66, -0.73)))/100
[1] 0
Petr Savicky.
More information about the R-help
mailing list