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

Ista Zahn istazahn at gmail.com
Wed Sep 30 21:59:26 CEST 2009


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?

-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.
>



-- 
Ista Zahn
Graduate student
University of Rochester
http://yourpsyche.org




More information about the R-help mailing list