From simon.urbanek at r-project.org  Wed Mar 25 15:32:59 2009
From: simon.urbanek at r-project.org (Simon Urbanek)
Date: Wed, 25 Mar 2009 10:32:59 -0400
Subject: [Rd] Error in FrF2 example on Mac OS
In-Reply-To: <22681913.post@talk.nabble.com>
References: <22675998.post@talk.nabble.com>
	<49C8E415.3060702@statistik.tu-dortmund.de>
	<20090324141728.GC4963@cs.cas.cz> <22681913.post@talk.nabble.com>
Message-ID: <AF49CB02-992F-4EE9-A763-534191EA5D72@r-project.org>


On Mar 24, 2009, at 10:41 , Ulrike Gr?mping wrote:

>
>
> Petr Savicky wrote:
>>
>> On Tue, Mar 24, 2009 at 02:45:57PM +0100, Uwe Ligges wrote:
>>>> gives the custom error message "nruns must be a power of 2.",  
>>>> which is
>>>> generated in the first check within function FrF2:
>>>>
>>>>   if (!is.null(nruns)){
>>>>      k <- floor(log2(nruns))
>>>>      if (!2^k==nruns) stop("nruns must be a power of 2.")}
>>>
>>>
>>> Probably a rounding issue on different platforms?
>>> I guess the test should be something like:
>>>
>>> if (!is.null(nruns)){
>>>  if(!isTRUE(all.equal(log2(nruns) %% 1, 0)))
>>>    stop("nruns must be a power of 2.")
>>> }
>>
>> Probably, k is needed also later. Assumig that 2^k works correctly,
>> the following could be sufficient
>>
>>   if (!is.null(nruns)){
>>      k <- round(log2(nruns))
>>      if (!2^k==nruns) stop("nruns must be a power of 2.")}
>>
>> In order to test the assumption, one can use
>>
>>  x <- 2^(0:100 + 0) # use double exponent to be sure
>>  all(x == floor(x))
>>
>> Powers of two are represented exactly, since they have only one
>> significant bit.
>>
>> Petr.
>>
>
> Yes, round instead of floor should also do the job, if rounding is the
> issue. But then, with powers of 2 indeed being represented exactly  
> (I would
> expect even on Macs), maybe rounding is not the issue? I have no  
> possibility
> to check this, since I do not have access to a Mac with R installed.  
> On my
> windows machine,
> all(log2(x)==floor(log2(x)))
> with x as defined above yields TRUE.
>

What you're missing is that you cannot rely on log2 to give you an  
integer. The test above bears no relevance to your problem - this is  
not about representing 2^x - this is about log2 which you cannot  
expect to satisfy log2(2^b) == b numerically since it could as well be  
computed log(x)/log(2) which is not exactly representable. Use round  
and all is well :).

 > which(floor(log2(2^x))!=x)
  [1]  4  7  8 13 14 15 25 27 29 49 53 57 64 97
 > which(round(log2(2^x))!=x)
integer(0)

Cheers,
Simon

>
> View this message in context: http://www.nabble.com/Error-in-FrF2-example-on-Mac-OS-tp22675998p22681913.html
> Sent from the R devel mailing list archive at Nabble.com.
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>
>



[Rd] Error in FrF2 example on Mac OS
Simon Urbanek