[R] Floating points and floor() ?

rex.dwyer at syngenta.com rex.dwyer at syngenta.com
Fri Mar 4 05:24:23 CET 2011


Hi Michael,

In floating point calculation, 1.0-.9 is not exactly 0.1.  This is easily seen by subtracting.
> (1.0-.9)-0.1
[1] -2.775558e-17
> (1.0-.9)==0.1
[1] FALSE

David is right, you can't "correct" this.  You can only compensate by taking care that you never, ever test whether 2 FP numbers are equal, because they almost never are.  You must always ask whether the difference is small.

> round(1.0-.9-.1,15)==0
[1] TRUE

Unfortunately, most of us forget this rule once in a while and write a loop like "while (x!=0)..." that won't terminate.
HTH
Rex



-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Folkes, Michael
Sent: Thursday, March 03, 2011 9:24 PM
To: r-help at r-project.org
Subject: [R] Floating points and floor() ?

Perhaps somebody could clarify for me if the following is a floating
point matter or otherwise, and how am I to correct for it?

> floor(100*.1)
[1] 10

> 100*(1.0-.9)
[1] 10

> floor(100*(1-0.9))
[1] 9


Thanks!
Michael
_______________________________________________________
Michael Folkes
Salmon Stock Assessment
Canadian Dept. of Fisheries & Oceans
Pacific Biological Station
3190 Hammond Bay Rd.
Nanaimo, B.C., Canada
V9T-6N7
Ph (250) 756-7264 Fax (250) 756-7053  Michael.Folkes at dfo-mpo.gc.ca


        [[alternative HTML version deleted]]

______________________________________________
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.




message may contain confidential information. If you are not the designated recipient, please notify the sender immediately, and delete the original and any copies. Any use of the message by you is prohibited. 


More information about the R-help mailing list