floating point issue in R?

floating-accuracy, floating-point, r

Solution

From the Floating-Point Guide:

Why don’t my numbers, like 0.1 + 0.2 add up to a nice round 0.3, and instead I get a weird result like 0.30000000000000004?

Because internally, computers use a format (binary floating-point) that cannot accurately represent a number like 0.1, 0.2 or 0.3 at all.

When the code is compiled or interpreted, your “0.1” is already rounded to the nearest number in that format, which results in a small rounding error even before the calculation happens.

What can I do to avoid this problem?

That depends on what kind of calculations you’re doing.

- If you really need your results to add up exactly, especially when you work with money: use a special decimal datatype.

- If you just don’t want to see all those extra decimal places: simply format your result rounded to a fixed number of decimal places when displaying it.

- If you have no decimal datatype available, an alternative is to work with integers, e.g. do money calculations entirely in cents. But this is more work and has some drawbacks.

Problem

Possible Duplicate: Why are these numbers not equal? The below expression, which evaluates to 0.1, is considered larger than 0.1. ``` > round(1740/600,0) - 1740/600 [1] 0.1 > (round(1740/600,0) - 1740/600) <= 0.1 [1] FALSE //???!!??? > (round(1740/600,0) - 1740/600) <= 0.1000000000000000000000000000000000000001 [1] TRUE ``` Thinking that the issue might be due to rounding I tried this with the same result: ``` > 3 - 2.9 [1] 0.1 > (3 - 2.9) <=0.1 [1] FALSE ``` So, what gives and how do I fix it without fudging the cutoff?

Original source

Related problems