[Rd] seq() function accuracy inacceptable (PR#8779)

Gabor Grothendieck ggrothendieck at gmail.com
Wed Apr 19 04:17:54 CEST 2006


This is related to this FAQ:

http://stat.cmu.edu/R/CRAN/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003f

What you can do is create the sequence of integers from 6155 to 6200
rather than using floats and then divide by 100 in your subsequent
calculation.  Until the point you divide by 100, your numbers will be
exact.

> 6155:6200
 [1] 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169
[16] 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184
[31] 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199
[46] 6200
> class(6155:6200)
[1] "integer"


On 4/18/06, Johannes.Prix at wu-wien.ac.at <Johannes.Prix at wu-wien.ac.at> wrote:
> Full_Name: Johannes Prix
> Version: 2.1.1
> OS: WinXP, SuSE Linux
> Submission from: (NULL) (137.208.41.195)
>
>
>
> The seq-command produces unnescessary inaccurate results, which can be extremely
> annoying.  I absolutely do not see the nescessity of numerical garbage to appear
> in the following simple case.  E.g. try this:
>
> > seq ( 61.55 , 62.00 , by=0.01 ) - round ( seq ( 61.55 , 62.00 , by=0.01 ) ,
> digits=2 )
>
> Output looks like:
>
>  [1]  0.000000e+00 -7.105427e-15  0.000000e+00  0.000000e+00 -7.105427e-15
> -7.105427e-15  0.000000e+00  0.000000e+00
>  [9] -7.105427e-15  0.000000e+00  0.000000e+00  0.000000e+00 -7.105427e-15
> 0.000000e+00  0.000000e+00 -7.105427e-15
> [17] -7.105427e-15  0.000000e+00  0.000000e+00 -7.105427e-15  0.000000e+00
> 0.000000e+00 -7.105427e-15 -7.105427e-15
> [25]  0.000000e+00  0.000000e+00 -7.105427e-15  0.000000e+00  0.000000e+00
> -7.105427e-15 -7.105427e-15  0.000000e+00
> [33]  0.000000e+00 -7.105427e-15  0.000000e+00  0.000000e+00  0.000000e+00
> -7.105427e-15  0.000000e+00  0.000000e+00
> [41] -7.105427e-15 -7.105427e-15  0.000000e+00  0.000000e+00 -7.105427e-15
> 0.000000e+00
> >
>
> It is particularly dangerous to use such seq()-contructed lists (without
> rounding) when e.g. trying to find the first time a given number appears in the
> list and the number is given without numerical garbage.
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>



More information about the R-devel mailing list