[R] bug?
Spencer Graves
spencer.graves at pdf.com
Tue Jul 15 01:38:05 CEST 2003
This may be picking nits, but from "%~=%" as written, I think I would
get 1e-50 %~=% 5e-50. With numbers ranging from 1e-51 to 1e-48, this is
rarely what I would want. Change "-" to "+" in the above example, and I
get the same result.
Something like the following might be more appropriate:
"%~=%"<-function(x,y){
if(is.numeric(x) & is.numeric(y)){
xpy <- (x+y)
xpy[xpy==0] <- 4*.Machine$double.eps
return((abs(x-y)/xpy)<(4*.Machine$double.eps))
}
else return(x==y)
}
This is close but not quite there yet, I don't think. Consider the
following:
> 1e-50 %~=% 2e-50
[1] FALSE
> 0 %~=% 2e-50
[1] FALSE
> 1e+50 %~=% 2e+50
[1] FALSE
> 0 %~=% 2e+50
[1] FALSE
> 11e-50 %~=% 2e-50
[1] FALSE
> 1 %~=% (1+.Machine$double.eps)
[1] TRUE
> 1 %~=% (1+3*.Machine$double.eps)
[1] TRUE
?????? spencer graves
(Ted Harding) wrote:
>>At 7/14/2003 at 03:29 AM, Marc Vandemeulebroecke wrote:
>>Dear R programmers,
>>
>>is there a sensible explanation for the following behaviour?
>>
>>
>>>seq(0.7, 0.9, by=0.1) == 0.8
>>
>>[1] FALSE FALSE FALSE
>
>
> Yet another %**% function ...
>
>
>>"%~=%"<-function(x,y){abs(x-y)<1e-15}
>>seq(0.7, 0.9, by=0.1) %~=% 0.8
>
> [1] FALSE TRUE FALSE
>
> Ted.
>
>
> --------------------------------------------------------------------
> E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk>
> Fax-to-email: +44 (0)870 167 1972
> Date: 14-Jul-03 Time: 22:16:38
> ------------------------------ XFMail ------------------------------
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
More information about the R-help
mailing list