[Rd] 0.45<0.45 = TRUE (PR#10744)

Prof Brian Ripley ripley at stats.ox.ac.uk
Wed Feb 13 14:37:10 CET 2008

On Wed, 13 Feb 2008, Heikki Kaskelma wrote:

> hadley wickham:
>> It's more than that as though, as floating point addition is no longer
>> guaranteed to be commutative or associative, and multiplication does
>> not distribute over addition.  Many concepts that are clear cut in
>> pure math become fuzzy in floating point math - equality, singularity
>> of matrices etc etc.
> Even simple averages may be complicated:
> eps=1
> while(1-eps != 1) eps=eps/2
> eps=eps*2

Such calculations are dangerous: we provide .Machine to avoid the need for 
them.  (They have plagued various LAPACK implementations, for example.)

Most R implementations these days use higher-precision registers for 
intermediate internal calculations such as those in mean() and sum().

> (1-1*eps+1-2*eps+1-3*eps)/3 == 1-1:3*eps    #  TRUE FALSE FALSE

Which is not an average, and the difference from the last line below just 
illustrates Hadley's point.

> mean(1-1:3*eps) == 1-1:3*eps    # FALSE  TRUE FALSE
> sum(1-1:3*eps)/3 == 1-1:3*eps   # FALSE FALSE  TRUE
> ((1-1*eps)+(1-2*eps)+(1-3*eps))/3 == 1-1:3*eps   # FALSE FALSE  TRUE
> so sometimes all the values may lie on the same side of the "average".

Only if you DIY average!  Note that mean() does a better job than you 
managed, because it uses a higher-precision accumulator.

I think the moral is to rely on the work of those to whom this is not at 
all 'complicated' (you should check out some of the rest of the R 
internals to see 'complicated').

> [Win R 2.6.1]
> Heikki Kaskelma
> Munkkiniemi, Finland
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

More information about the R-devel mailing list