[R] Basic question on function "identical"
(Ted Harding)
Ted.Harding at nessie.mcc.ac.uk
Mon Dec 15 18:22:59 CET 2003
On 15-Dec-03 Thomas Lumley wrote:
>
> One reason that which.max() exists is that we cannot guarantee
> which(x==max(x)) to work. It is possible, though rather unlikely, for
> there to be no x such that x==max(x). One reason is the unpredictable
> use of 10-byte wide floating point registers on Intel chips.
Hmmm ...
I'd be interested to learn more of what you mean. For instance,
in C-speak, to find the maximum of an array of double x[], of
length n, something like the following code could be written:
xmax=x[1];
for(i=1;i<n;i++) if(x[i+1]>x[i]) xmax=x[i+1];
Regardless of the accuracy of the comparison, each assignment
xmax = ... should make xmax an internally exact copy of the
thing on the righthand side. However, your reply suggests that this
may not happen, as a result of "unpredictable use of 10-byte wide
floating point registers on Intel chips".
Is this really the case? If so, how would a discrepancy arise?
(I know C programmers like to use "register variables" where
possible, for speed, but the copying should be faithful, surely).
On the other hand, R has to deal with things which may be a mixture
of types, so has to cope with the coercions implied by the hierarchy
of types; but for instance
> x<-c(-1.0, 0.5, TRUE, as.integer(0))
> which(x==max(x))
[1] 3
>
still gives the answer one would expect.
But in any case, the usual sort of application is which(x==max(x))
where the values in x are doubles, so the first question remains.
Futhermore, "which(x==max(x))" has a useful feature which
"which.max(x))" does not have::
> x<-c(0.1, 0.7, 5.1, 5.1, 1.5, 5.1, 3.4, 2.2, 4.3)
> which(x==max(x))
[1] 3 4 6
> which.max(x)
[1] 3
so I'd feel a bit deprived if the former were discouraged!
With thanks,
Ted.
--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk>
Fax-to-email: +44 (0)870 167 1972
Date: 15-Dec-03 Time: 17:22:59
------------------------------ XFMail ------------------------------
More information about the R-help
mailing list