Re: [R] Floating points and floor() ?

From: Folkes, Michael <Michael.Folkes_at_dfo-mpo.gc.ca>
Date: Fri, 04 Mar 2011 10:36:42 -0800

This was most helpful Rex. It makes clear how I can compensate for the matter. Too bad it's not March 15, I could declare "beware the 0.Ides of March!" Michael

-----Original Message-----
From: rex.dwyer_at_syngenta.com [mailto:rex.dwyer_at_syngenta.com] Sent: Thu 03/03/2011 8:24 PM
To: Folkes, Michael; r-help_at_r-project.org Subject: RE: Floating points and floor() ?  

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.

        [[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. Received on Fri 04 Mar 2011 - 18:45:10 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 04 Mar 2011 - 19:00:21 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive