R: floating point errors in simple math operations

45 views Asked by At

So of course:

1023.1 - 0.1 < 1023
[1] FALSE

but...

1024.1 - 0.1 < 1024
[1] TRUE

Is this a bug? Is it expected behaviour? Is there some way I can avoid this, such as rounding all my numbers to like 10 significant digits (but this sounds terrible). I know about functions such as all.equal() but I am not attempting comparisons, I got here because this leads to unpredicted failures in other functions such as:

start = 1024
end = 1024 - 0.1
seq(start, end, by=0.1)

given that the numbers are variable and out of my control what is the best practice to clean/proof my variables to avoid these type of issues?

This is on windows 10 if it makes any difference. R version 4.2.2

0

There are 0 answers