Re: [Rd] compairing doubles

2018-09-03 Thread Martin Maechler
> 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

Re: [Rd] compairing doubles

2018-09-03 Thread Rui Barradas
Hello, Watch out for operator precedence. 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. isTR

Re: [Rd] compairing doubles

2018-09-03 Thread Juan Telleria Ruiz de Aguirre
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. __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel

Re: [Rd] compairing doubles

2018-08-31 Thread Iñaki Ucar
El vie., 31 ago. 2018 a las 17:08, Serguei Sokol () escribió: > > Le 31/08/2018 à 16:25, Mark van der Loo a écrit : > > Ah, my bad, you're right of course. > > > > sum(abs(diff(diff( sort(x) < eps > > > > for some reasonable eps then, would do as a oneliner, or > > > > all(abs(diff(diff(sort(x)

Re: [Rd] compairing doubles

2018-08-31 Thread Marc Schwartz via R-devel
> On Aug 31, 2018, at 9:36 AM, Iñaki Ucar wrote: > > El vie., 31 ago. 2018 a las 15:10, Felix Ernst > () escribió: >> >> Dear all, >> >> I a bit unsure, whether this qualifies as a bug, but it is definitly a >> strange behaviour. That why I wanted to discuss it. >> >> With the following fu

Re: [Rd] compairing doubles

2018-08-31 Thread Serguei Sokol
Le 31/08/2018 à 16:25, Mark van der Loo a écrit : Ah, my bad, you're right of course. sum(abs(diff(diff( sort(x) < eps for some reasonable eps then, would do as a oneliner, or all(abs(diff(diff(sort(x < eps) or max(abs(diff(diff(sort(x) < eps Or with only four function calls: di

Re: [Rd] compairing doubles

2018-08-31 Thread Mark van der Loo
Ah, my bad, you're right of course. sum(abs(diff(diff( sort(x) < eps for some reasonable eps then, would do as a oneliner, or all(abs(diff(diff(sort(x < eps) or max(abs(diff(diff(sort(x) < eps -Mark Op vr 31 aug. 2018 om 16:14 schreef Iñaki Ucar : > El vie., 31 ago. 2018 a las

Re: [Rd] compairing doubles

2018-08-31 Thread Iñaki Ucar
El vie., 31 ago. 2018 a las 16:00, Mark van der Loo () escribió: > > how about > > is_evenly_spaced <- function(x,...) all.equal(diff(sort(x)),...) This doesn't work, because 1. all.equal does *not* return FALSE. Use of isTRUE or identical(., TRUE) is required if you want a boolean. 2. all.equal

Re: [Rd] compairing doubles

2018-08-31 Thread Mark van der Loo
Sorry for the second e-mail: this is worth watching: https://www.youtube.com/watch?v=3Bu7QUxzIbA&t=1s It's Martin Maechler's talk at useR!2018. This kind of stuff should be mandatory material for any aspiring programmer/data scientist/statistician. -Mark Op vr 31 aug. 2018 om 16:00 schreef Mar

Re: [Rd] compairing doubles

2018-08-31 Thread Mark van der Loo
how about is_evenly_spaced <- function(x,...) all.equal(diff(sort(x)),...) (use ellipsis to set tolerance if necessary) Op vr 31 aug. 2018 om 15:46 schreef Emil Bode : > Agreed that's it's rounding error, and all.equal would be the way to go. > I wouldn't call it a bug, it's simply part of wor

Re: [Rd] compairing doubles

2018-08-31 Thread Iñaki Ucar
FYI, more fun with floats: > 0.1+0.1==0.2 [1] TRUE > 0.1+0.1+0.1+0.1==0.4 [1] TRUE > 0.1+0.1+0.1==0.3 [1] FALSE > 0.1+0.1+0.1==0.1*3 [1] TRUE > 0.3==0.1*3 [1] FALSE ¯\_(ツ)_/¯ But this is not R's fault. See: https://0.30004.com Iñaki El vie., 31 ago. 2018 a las 15:36, Iñaki Ucar ()

Re: [Rd] compairing doubles

2018-08-31 Thread Emil Bode
Agreed that's it's rounding error, and all.equal would be the way to go. I wouldn't call it a bug, it's simply part of working with floating point numbers, any language has the same issue. And while we're at it, I think the function can be a lot shorter: .is_continous_evenly_spaced <- function(n)

Re: [Rd] compairing doubles

2018-08-31 Thread Iñaki Ucar
El vie., 31 ago. 2018 a las 15:10, Felix Ernst () escribió: > > Dear all, > > I a bit unsure, whether this qualifies as a bug, but it is definitly a > strange behaviour. That why I wanted to discuss it. > > With the following function, I want to test for evenly space numbers, > starting from anyw