[R] Rounding error in seq(...)

Duncan Murdoch murdoch at stats.uwo.ca
Wed Sep 30 22:32:15 CEST 2009


On 9/30/2009 3:59 PM, Ista Zahn wrote:
> For my own edification more than anything (I never took computer science): is
> 
>> a = seq(0.1,0.9,by=0.1)
>> a <- as.character(a)
>> a[3] == "0.3"
> [1] TRUE
> 
> safe?

No.  Someone might be in a locale where the comma is used as the decimal 
separator.  Take a look at your code after options(OutDec=",") to see 
the result.

 > options(OutDec=",")
 > 0.3
[1] 0,3
 > a = seq(0.1,0.9,by=0.1)
 > a <- as.character(a)
 > a[3] == "0.3"
[1] FALSE

You'd have better luck with a[3] == as.character(0.3), but you should 
use all.equal(), that's what it is designed for.

Duncan Murdoch

> 
> -Ista
> 
> On Wed, Sep 30, 2009 at 3:46 PM, cls59 <chuck at sharpsteen.net> wrote:
>>
>>
>> Martin Batholdy wrote:
>>>
>>> hum,
>>>
>>> can you explain that a little more detailed?
>>> Perhaps I miss the background knowledge - but it seems just absurd to
>>> me.
>>>
>>> 0.1+0.1+0.1 is 0.3 - there is no rounding involved, is there?
>>>
>>>
>>
>> Unfortunately this comes as an utter shock to many people who never take a
>> Computer Science course. I watch it nail engineering students all the time.
>>
>> Basically, if you have a fraction and the denominator is not equal to 2^n
>> for some integer n, that fraction will NEVER be stored as an exact "floating
>> point" number-- instead it will contain some error due to concessions that
>> must be made in order to use an efficient binary number scheme.
>>
>> These errors are generally small, but they do propagate-- especially if you
>> are carrying the same numbers through a large computation. A good example is
>> large-scale numerical solutions to nonlinear problems where iterative
>> algorithms are employed repetitively at each solution step. As the
>> calculation progresses the roundoff error can rot away the computational
>> soundness of the algorithm.
>>
>> If this concerns you, I would suggest reading up on common internal
>> representations of floating point numbers as well as the propagation of
>> roundoff error.
>>
>> At the very least I hope this revelation will instill an appropriate sense
>> of paranoia concerning the numbers calculated by those magic boxes sitting
>> on our desks.
>>
>> -Charlie
>>
>> -----
>> Charlie Sharpsteen
>> Undergraduate
>> Environmental Resources Engineering
>> Humboldt State University
>> --
>> View this message in context: http://www.nabble.com/Rounding-error-in-seq%28...%29-tp25686630p25687626.html
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> ______________________________________________
>> 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