[Rd] identical(0, -0)
Martin Maechler
maechler at stat.math.ethz.ch
Tue Aug 11 10:04:20 CEST 2009
>>>>> "DM" == Duncan Murdoch <murdoch at stats.uwo.ca>
>>>>> on Mon, 10 Aug 2009 11:51:53 -0400 writes:
DM> For people who want to play with these, here are some functions that let
DM> you get or set the "payload" value in a NaN. NaN and NA, Inf and -Inf
DM> are stored quite similarly; these functions don't distinguish which of
DM> those you're working with. Regular finite values give NA for the
DM> payload value, and elements of x are unchanged if you try to set their
DM> payload to NA.
DM> By the way, this also shows that R *can* distinguish different NaN
DM> values, but you need some byte-level manipulations.
yes; very nice code, indeed!
I propose a version of the showBytes() utility should be added
either as an example e.g. in writeBin() or even an exported
function in package 'utils'
[.........]
> Example:
>> x <- c(NA, NaN, 0, 1, Inf)
>> NaNpayload(x)
> [1] 0.5 -0.5 NA NA 0.0
Interestingly, on 64-bit, I get a slightly different answer above,
(when all the following code gives exactly the same results,
and of course, that was your main point !), namely
4.338752e-13 instead of 0.5 for 'NA',
see below.
.. and your nice tools also let me detect an even simpler way
to get *two* versions of NA, and NaN, each :
Conclusion: Both NaN and NA (well NA_real_) have a sign, too !
NaNpayload(NA_real_)
##[1] 4.338752e-13
NaNpayload(-NA_real_)
##[1] -4.338752e-13 ## !! different
str(NApm <- c(1[2], -1[2]))
t(sapply(NApm, showBytes))
## [1,] a2 07 00 00 00 00 f0* 7f
## [2,] a2 07 00 00 00 00 f0* ff
## or summarizing things :
## Or, "in summary" -- Duncan's original example slightly extended:
x <- c(NaN, -NaN, NA, -NA_real_, 0, 0.1, Inf, -Inf)
x
names(x) <- format(x)
sapply(x, showBytes)
## NaN NaN NA NA 0.0 0.1 Inf -Inf
## [1,] 00 00 a2 a2 00 9a 00 00
## [2,] 00 00 07 07 00 99 00 00
## [3,] 00 00 00 00 00 99 00 00
## [4,] 00 00 00 00 00 99 00 00
## [5,] 00 00 00 00 00 99 00 00
## [6,] 00 00 00 00 00 99 00 00
## [7,] f8 f8 f8* f8* 00 b9 f0 f0
## [8,] ff 7f 7f ff 00 3f 7f ff
## (*) NOTE: the 'f0*' or 'f8*' above are
## --- 'f8' on 32-bit, 'f0' on 64-bit
>> NaNpayload(x) <- -0.4
>> x
> [1] NaN NaN NaN NaN NaN
>> y <- x
>> NaNpayload(y) <- 0.6
>> y
> [1] NaN NaN NaN NaN NaN
>> NaNpayload(x)
> [1] -0.4 -0.4 -0.4 -0.4 -0.4
>> NaNpayload(y)
> [1] 0.6 0.6 0.6 0.6 0.6
>> identical(x, y)
> [1] TRUE
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
More information about the R-devel
mailing list