[R] arithmetic and logical operators
wdunlap at tibco.com
Wed Jan 30 22:09:44 CET 2013
> Why, in R, does (0.1 + 0.05) > 0.15 evaluate to True?
Because floating point arithmetic is done with a fixed
number of digits. If you are working in base 10 and have
2 digits to work with you would have
1/3 -> .33
2/3 -> .67
1/3 + 1/3 < 2/3
> How can I ensure this (ostensibly incorrect) behavior doesn't
> introduce bugs into my code?
You can index things with integers. E.g., instead of storing 0.1, 0.05,
and 0.15, store 2, 1, and 3. Do your comparisons on the integers
and multiply by 0.05 when you need to use them in that format.
You can work with numbers that are exactly representable with
52 binary digits, such as 1/1024 or 10^14+1/8.
You can work with smooth functions so the it doesn't matter
much what the sign of 0.15 - (0.10 + 0.05) is.
Spotfire, TIBCO Software
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf
> Of Dave Mitchell
> Sent: Wednesday, January 30, 2013 12:32 PM
> To: r-help at r-project.org
> Subject: [R] arithmetic and logical operators
> Why, in R, does (0.1 + 0.05) > 0.15 evaluate to True? What am I missing
> here? How can I ensure this (ostensibly incorrect) behavior doesn't
> introduce bugs into my code? Thanks for your time.
> Dave Mitchell
> [[alternative HTML version deleted]]
> R-help at r-project.org mailing list
> 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