[R] Bug or feature? sum(c(a, b, c)) != (a + b + c)

Clint Bowman clint at ecy.wa.gov
Tue Aug 23 22:51:11 CEST 2011


?all.equal

-- 
Clint Bowman			INTERNET:	clint at ecy.wa.gov
Air Quality Modeler		INTERNET:	clint at math.utah.edu
Department of Ecology		VOICE:		(360) 407-6815
PO Box 47600			FAX:		(360) 407-7534
Olympia, WA 98504-7600


         USPS:           PO Box 47600, Olympia, WA 98504-7600
         Parcels:        300 Desmond Drive, Lacey, WA 98503-1274


On Tue, 23 Aug 2011, Daniel Lai wrote:

> 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
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list