# Floating points and floor() ?

6 messages
Open this post in threaded view
|
Report Content as Inappropriate

## 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  [hidden email]         [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Floating points and floor() ?

 You are assuming that 0.1 is exactly represented. Since it is not, either 0.1 or 1-0.9 will be less than 0.1. As to what you should do about it... that depends on what you are trying to accomplish. Whatever it is, you need to use some other approach. Often the alternative involves using integers as much as possible. --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<[hidden email]> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. "Folkes, Michael" <[hidden email]> wrote: 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 [hidden email] [[alternative HTML version deleted]]_____________________________________________ [hidden email] 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.         [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Floating points and floor() ?

 In reply to this post by Folkes, Michael-2 On Mar 3, 2011, at 8:23 PM, Folkes, Michael wrote: > 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 > > Yes. It's a "floating point matter". What do you mean by "correct for   it"? What result would be "correct"? David Winsemius, MD Heritage Laboratories West Hartford, CT ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Floating points and floor() ?

 In reply to this post by Folkes, Michael-2 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: [hidden email] [mailto:[hidden email]] On Behalf Of Folkes, Michael Sent: Thursday, March 03, 2011 9:24 PM To: [hidden email] 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  [hidden email]         [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland 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. ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Floating points and floor() ?

 In reply to this post by Folkes, Michael-2 On Thu, Mar 03, 2011 at 06:23:36PM -0800, Folkes, Michael wrote: > 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 As others pointed out, 0.1 is not exactly representable in base 2, so we get   formatC(0.1, digits=20)   [1] "0.10000000000000000555"   formatC(100*0.1, digits=20, width=-1)   [1] "10"   formatC(100*(1 - 0.9), digits=20)   [1] "9.9999999999999982236" A correct result may be obtained, if you reorganize your calculation, so that all intermediate results are integers and the inaccurate division is only the last operation. Then, floor(n/10) will be correct and also n %/% 10 may be used. Alternatively, if you work with decimal numbers with 1 or 2 decimal digits, then also floor(round(x, 1)) or floor(round(x, 2)) work correctly, if x is not too large. See FAQ 7.31   http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003fand   http://rwiki.sciviews.org/doku.php?id=misc:r_accuracy:decimal_numbersfor further examples and some hints. Hope this helps. Petr Savicky. ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.