[Rd] Friday question: negative zero
Jeffrey Horner
jeff.horner at vanderbilt.edu
Sat Sep 1 18:49:03 CEST 2007
deepayan.sarkar at gmail.com wrote:
>
> On 8/31/07, Duncan Murdoch <murdoch at stats.uwo.ca> wrote:
>> The IEEE floating point standard allows for negative zero, but it's hard
>> to know that you have one in R. One reliable test is to take the
>> reciprocal. For example,
>>
>> > y <- 0
>> > 1/y
>> [1] Inf
>> > y <- -y
>> > 1/y
>> [1] -Inf
>>
>> The other day I came across one in complex numbers, and it took me a
>> while to figure out that negative zero was what was happening:
>>
>> > x <- complex(real = -1)
>> > x
>> [1] -1+0i
>> > 1/x
>> [1] -1+0i
>> > x^(1/3)
>> [1] 0.5+0.8660254i
>> > (1/x)^(1/3)
>> [1] 0.5-0.8660254i
>>
>> (The imaginary part of 1/x is negative zero.)
>>
>> As a Friday question: are there other ways to create and detect
>> negative zero in R?
>>
>> And another somewhat more serious question: is the behaviour of
>> negative zero consistent across platforms? (The calculations above were
>> done in Windows in R-devel.)
>
> No, I get
>
>> 1/ Im(1/complex(real = -1))
> [1] Inf
>> sessionInfo()
> R version 2.6.0 Under development (unstable) (2007-08-16 r42532)
> x86_64-unknown-linux-gnu
>
> This is on an AMD 64, and I get -Inf on everything else I've tried so far, which are all Intel.
Okay, I hope this isn't line noise, but our AMD 64 with R version 2.5.1
gets:
> 1/ Im(1/complex(real = -1))
[1] -Inf
> sessionInfo()
R version 2.5.1 (2007-06-27)
x86_64-pc-linux-gnu
locale:
LC_CTYPE=en_US;LC_NUMERIC=C;LC_TIME=en_US;LC_COLLATE=en_US;LC_MONETARY=en_US;LC_MESSAGES=en_US;LC_PAPER=en_US;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=en_US;LC_IDENTIFICATION=C
attached base packages:
[1] "stats" "graphics" "grDevices" "utils" "datasets" "methods"
[7] "base"
Should I compile R-devel and post the results of the above?
Jeff
--
http://biostat.mc.vanderbilt.edu/JeffreyHorner
More information about the R-devel
mailing list