I was comparing two different formulas to calculate percentage on R and, despite they are equivalent and should produce exactly the same value (if my math is not gravely mistaken), they do not seem to produce the exact result.
Let me present you an example:
set.seed(123)
a<-rnorm(100)
perc_1<-(a/sum(a))*100
perc_2<-(a*100)/sum(a)
Now, you have differences according with the function you use to check if they are equal: all.equal(perc_1,perc_2)
is TRUE
but, all(perc_1==perc_2)
is FALSE
. However, I can understand that they produce different results, because the latter tests exact equality while the former tests near exact equality.
If I perform a summary of the difference, I get this:
summary(perc_1-perc_2)
Min. 1st Qu. Median Mean 3rd Qu. Max.
-3.553e-15 0.000e+00 0.000e+00 1.818e-17 0.000e+00 3.553e-15
So, my question is: does anyone has an explanation for this discrepancy?
Thanks in advance.