[R] seq(...) strange logical value

Wacek Kusnierczyk Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
Mon Jun 8 15:51:53 CEST 2009


Allan Engelhardt wrote:
> See 
> http://wiki.r-project.org/rwiki/doku.php?id=misc:r_accuracy:decimal_numbers#sequences_of_decimal_numbers
>
>   

as usual, be careful about what is advertised in r docs and related
texts.  on the r_accuracy page, you'll read:

"For further information, see the digits and scipen arguments of
function options(). Even better control of the output format may be
achieved using functions sprintf(), formatC() and prettyNum(). "

but all these functions rely on the c-level formatting of numbers into
strings, and hence the result may differ from what you'd get with round:

    round(0.15, 1)
    # 0.2

    as.numeric(sprintf('%.1f', 0.15))
    # 0.1

    as.numeric(formatC(0.15, 1))
    # 0.1

be prepared for surprises.

> and also 
> http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003f
>
>   

also here, you can read that

"The only numbers that can be represented exactly in R's numeric type
are integers and fractions whose denominator is a power of 2."

which is accurate up to the point (hinted, but only hinted, in the very
next sentence there) that these numbers can be represented exactly only
if their binary expansion does not exceed the fixed capacity of r's
numeric representation --  as explained in r_accuracy, above.  the
number 1/2^10000 will not be represented exactly, even though it's
denominator is a power of 2.

vQ

> GrzeÅ› wrote:
>   
>> Do you heve any idea why I get after this instruction everywhere false?
>>   
>>     
>>> seq (0, 1, by=0.1) == 0.3
>>>     
>>>       
>>  [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
>>
>> But after different step it's ok:
>>   
>>     
>>> seq(0, 1, by=0.1) == 0.4
>>>     
>>>       
>>  [1] FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE
>>
>>   
>>     
>
> 	[[alternative HTML version deleted]]
>
>   
> ------------------------------------------------------------------------
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>




More information about the R-help mailing list