From m@echler @ending from @t@t@m@th@ethz@ch  Mon Sep  3 15:35:45 2018
From: m@echler @ending from @t@t@m@th@ethz@ch (Martin Maechler)
Date: Mon, 3 Sep 2018 15:35:45 +0200
Subject: [Rd] compairing doubles
In-Reply-To: <d89880dd-5cdd-6fc8-0716-6c036507eb9c@sapo.pt>
References: <AM0PR0502MB387404BC690F9AF421B6F5EAD6080@AM0PR0502MB3874.eurprd05.prod.outlook.com>
 <CALEXWq3wSS4uaYu33oA-KUu7gSBuqKYF0ESemwvsbmp6KktOJg@mail.gmail.com>
 <95F39593-22F4-46B6-A766-8FF60F608C0C@dans.knaw.nl>
 <CAOKDuOgykFYMA-ZT4kUxr_NKXBEBsxZV8s-3sAuQ3Motc5M_Fw@mail.gmail.com>
 <CALEXWq2ke081QLqbv89z=yfTuoocLw9dzQWx6-05gzSFqAD3vQ@mail.gmail.com>
 <CAOKDuOg5bA0Uf5bmQX_9ZP6qbnO=hewb7cVbjEEZEvu5KOGC5w@mail.gmail.com>
 <e275b09d-39c8-a7e2-fc11-30c00034d1e4@insa-toulouse.fr>
 <CALEXWq3hNdLZwf9v=YZNTJ1FjTJfN6xuuUeSZ-zyZq1Sp+8yKA@mail.gmail.com>
 <CAJXDcw3CJsDoLN_=_jf7Ud554Cm6DikHOhwQLWw9aUOycuuy5A@mail.gmail.com>
 <d89880dd-5cdd-6fc8-0716-6c036507eb9c@sapo.pt>
Message-ID: <23437.14513.702181.359473@stat.math.ethz.ch>

>>>>> Rui Barradas 
>>>>>     on Mon, 3 Sep 2018 09:58:34 +0100 writes:

    > Hello, Watch out for operator precedence.

indeed!  (but not only)

> all.equal(0.3, 0.1*3)
> #[1] TRUE
> 
> 
> `%~~%` <- function (e1, e2)  all.equal(e1, e2)
> 
> 0.3 %~~% 0.1*3
> #Error in 0.3 %~~% 0.1 * 3 : argumento n?o-num?rico para operador bin?rio
> 
> 
> 0.3 %~~% (0.1*3)
> #[1] TRUE
> 
> 
> Now with isTRUE. The problem changes a bit.
> 
> 
> isTRUE(all.equal(0.3, 0.1*3))
> #[1] TRUE
> 
> 
> `%~~%` <- function (e1, e2)  isTRUE(all.equal(e1, e2))
> 
> 0.3 %~~% 0.1*3
> #[1] 0
> 
> 0.3 %~~% (0.1*3)
> #[1] TRUE
> 

> Hope this helps,
> Rui Barradas
> 
> ?s 08:20 de 03/09/2018, Juan Telleria Ruiz de Aguirre escreveu:
> > Maybe a new Operator could be defined for a fast and easy double
> > Comparison: `~~`
> > 
> > `~~` <- function (e1, e2)  all.equal(e1, e2)
> > 
> > And document it properly.
> > 

I would still quite strongly recommend against such a
definition:

If you ask for  help(all.equal)
you do see that it is a generic with a   all.equal.numeric()
method which has several extra arguments 
(new ones even in R-devel)  the most important one being the
numerical  'tolerance'  with a default of
sqrt(.Machine$double.eps)  { == 2^-26 == 1.490116e-08  on all current platforms}

Of course there is some arbitraryness in that choice
{{ but only *some*: the default is related to finding the minimum of
   smooth function which hence is locally quadratic at a "decent"
   minimum hence sqrt(.)
}}
but I find it important sometimes to increase the equality
strictness of that tolerance.

Hiding everything behind a new operator which does not allow to
take into account that there are quite a few versions of
near-equality --- only partly) mirrored by the existence of
extra arguments of all.equal() --- only encourages simplified
thinking about the underlying subtle issues  which already too
many people don't care to know about.

(( e.g. all those people only caring for speed, but not for
   accuracy and reliability ... ))

Martin Maechler
ETH Zurich and R Core



[Rd] compairing doubles
Martin Maechler