# [R] Bug in rep() function

Samuel Knapp samuel.k at gmx.de
Mon Sep 15 19:12:20 CEST 2014

```Thank you.
I got the point with non-integer values in rep(). I also red FAQ 7.3:
"The only numbers that can be represented exactly in R’s numeric type
are integers and fractions whose denominator is a power of 2."

But then I still don't understand:

> for (b in seq(0.2,0.8,0.2))
+ {
+   a <- (1-b)*10
+
+   print(1-b,digits=20)
+   print(a,digits=22)
+   print(trunc(a))
+   print("///////")
+ }
[1] 0.80000000000000004441
[1] 8
[1] 8
[1] "///////"
[1] 0.5999999999999999778
[1] 6
[1] 6
[1] "///////"
[1] 0.39999999999999991118
[1] 3.999999999999999111822
[1] 3
[1] "///////"
[1] 0.19999999999999995559
[1] 1.999999999999999555911
[1] 1
[1] "///////"

Why are the first two yielding an integer after multiplying, and the
last two don't? Apparently, c(0.8,0.6,0.4,0.2) can't be represented exactly.

What would be your approach? Always round numbers first, before giving
them to rep() ?

Thanks,
Samuel

On 15.09.2014 18:36, Prof Brian Ripley wrote:
> On 15/09/2014 16:30, Samuel Knapp wrote:
>> Dear all,
>>
>> I have discovered a bug in the standard rep() function: At certain
>
> Not so:
>
> > a <- (1-0.9)*100
> > trunc(a)
> [1] 9
>
> As the help says
>
>      Non-integer values of ‘times’ will be truncated towards zero.  If
>      ‘times’ is a computed quantity it is prudent to add a small fuzz.
>
> And as the posting guide said
>
> Do your homework before posting:
> ...
> e.g., ?prod, at the R prompt)
>
>
>> values, rep() does not replicate the element by the proper number of
>> times:
>>
>>  > a <- (1-0.9)*100
>>  > a
>> [1] 10
>>  > length(rep(1,times=a))
>> [1] 9
>>  > length(rep(1,each=a))
>> [1] 9
>>
>> As shown, this happens as well for the times= as for the each=
>> parameter. It does not depend on the kind of element that is to be
>> repeated:
>>
>>  > length(rep("abc",each=a))
>> [1] 9
>>
>> I tried to narrow down the bug, but haven't really managed to find a
>> pattern behind the bug. Here is a list with values for a (see above)
>> that returns a false object ( after the value for a, i've collected the
>> expected length and the length that is produced by r):
>>
>> # mistake at
>> (1-0.9)*100       10           9
>> (1-0.8)*100       20          19
>> (1-0.8)*1000      200       199
>> (1-0.9)*1000      100       99
>> (1-0.9)*10            1         0
>> (1-0.8)*10            2         1
>> (1-0.9)*1000000000      100000000       99999999
>> (2-1-0.9)*100         10      9
>> (10/10-0.9)*100     10      9
>>
>> # the following sets for a work fine
>> (1+0.1)*100
>> (1-0.1)*100
>> (1-0.7)*100
>> (1-0.99)*1000
>> (1-0.7)*10
>> (1-0.90)*10
>> (1-0.95)*100
>> (1-0.95)*1000
>> (2-0.9)*1000
>> (2-1.9)*100
>> (1.1-1)*100
>> (10-9)*100
>>
>> Did I make any mistake? Or where else should I address this problem?
>>
>> Thanks and best regards,
>> Samuel
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help