[R] equality of values

Rui Barradas ruipbarradas at sapo.pt
Mon Jun 25 08:58:42 CEST 2012


Hello,

Just as a complement, if you want to see a solution, and why.


x <- 3/5
y <- 2/5
z <- 1/5
#
x - (y + z) # not zero
x == y + z
all.equal(x, y + z)
#
x - y - z   # not zero
x - y == z
all.equal(x - y, z)

# To see why it works: abs diff less than tolerance
equal <- function(x, y, tol=.Machine$double.eps) abs(x - y) < tol
equal(x, y + z)
equal(x - y, z)


Hope this helps,

Rui Barradas


Em 24-06-2012 23:14, Andreia Leite escreveu:
> Thanks a lot for your explanation!
>
> I see the point and I think I'll solve my question using a different method!
>
> Andreia Leite
>
> On Sun, Jun 24, 2012 at 10:53 PM, Oliver Ruebenacker <curoli at gmail.com>wrote:
>
>>      Hello,
>>
>>   The FAQ answer 7.31 may be confusing to noobs and the referenced
>> paper is long. The essence is:
>>
>>   (1) It's not an R issue, it is a floating point (flop) arithmetic issue.
>>   (2) The issue is that flop arithmetic is almost always only approximate.
>>   (3) Therefore, asking for exact equality of two flops is almost
>> always a mistake.
>>   (4) In simple cases, you can take the difference of two flops and
>> ask whether it is good enough
>>   (5) For extended calculations, you need to keep in mind that errors
>> propagate and accumulate.
>>   (6) Some ill-defined problems may lead to "solutions" that aren't
>> solutions (e.g. trying to invert a singular matrix)
>>   (7) Some well-defined problems may defy a straight-forward approach
>> (e.g. solving stiff differential equations)
>>
>>      Take care
>>      Oliver
>>
>> On Sun, Jun 24, 2012 at 5:29 PM, Sarah Goslee <sarah.goslee at gmail.com>
>> wrote:
>>> Please read R FAQ 7.31, Why doesn't R think these numbers are equal?
>>>
>>> Sarah
>>>
>>> On Sun, Jun 24, 2012 at 1:58 PM, Andreia Leite
>>> <andreiaheitorleite at gmail.com> wrote:
>>>> Dear list,
>>>>
>>>> I'm trying to do a loop where I should choose a cell of a data frame
>>>> comparing it with another. The code actually works for the majority of
>> the
>>>> loop but for some reason it doesn't and I can't figure out why.
>>>>
>>>> When I tried to understand why I've noticed this:
>>>>
>>>>> dif[11]
>>>> [1] 118.8333
>>>>> linhasUpdate[10,6]
>>>> [1] 118.8333
>>>>> dif[11]==linhasUpdate[10,6]
>>>> [1] FALSE
>>>>
>>>> Even though the values are the same R says they aren't! This happens for
>>>> some of the values (44) and for the others (128) it works fine.
>>>>
>>>> Does anybody why is this happening or a way to fix it? I' using 2.15.0.
>>>>
>>>> Thanks a lot!
>>>>
>>>> Andreia Leite
>>>>
>>>> --
>>> --
>>> Sarah Goslee
>>> http://www.functionaldiversity.org
>>>
>>> ______________________________________________
>>> R-help at r-project.org 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.
>>
>>
>>
>> --
>> Oliver Ruebenacker, Bioinformatics and Network Analysis Consultant
>> President and Founder of Knowomics
>> (http://www.knowomics.com/wiki/Oliver_Ruebenacker)
>> Consultant at Predictive Medicine
>> (http://predmed.com/people/oliverruebenacker.html)
>> SBPAX: Turning Bio Knowledge into Math Models (http://www.sbpax.org)
>>
>
>
>



More information about the R-help mailing list