[R] Bug or feature? sum(c(a, b, c)) != (a + b + c)
Daniel Lai
danlai at bccrc.ca
Tue Aug 23 21:17:20 CEST 2011
Greetings all,
I'm porting an algorithm from MATLAB to R, and noticed some minor
discrepancies in small decimal values using rowSums and colSums which
are exacerbated after heavy iteration and log space transformation.
This was rather perplexing as both programs claimed and appeared to use
the IEEE 754 standard for floating point arithmetic (confirmed with
manual basic operations). After some tracing and testing, I've managed
to isolated a minimal working example as follows:
a = 0.812672
b = 0.916541
c = 0.797810
sum(c(a, b, c)) == (a + b + c)
[1] FALSE
Should I attribute this to the woes of working with floating point
numbers and accept it? i.e.
sprintf("%.30f", sum(c(a, b, c)))
[1] "2.527022999999999797182681504637"
sprintf("%.30f", (a + b + c))
[1] "2.527023000000000241271891354700"
Change the OS or version I'm using?
MAC OSX 10.5.8:
sessionInfo()
R version 2.13.1 (2011-07-08)
Platform: i386-apple-darwin9.8.0/i386 (32-bit)
attached base packages:
[1] stats graphics grDevices utils datasets methods base
Linux 2.6.34:
R version 2.12.0 (2010-10-15)
Platform: x86_64-unknown-linux-gnu (64-bit)
attached base packages:
[1] stats graphics grDevices utils datasets methods base
Or report this as a bug?
Thanks,
Daniel
More information about the R-help
mailing list