# [R] Odd results with %% and conserving memory

JustinNabble justinmmcgrath at hotmail.com
Fri Mar 26 22:05:36 CET 2010

```Can anyone explain this?

I have a matrix with double components. It's taking up a lot of memory, so I
want to multiply then turn it to integers. I'm pretty certain that there are
only 2 decimal places, but I wanted to check by using modulo. E.g.

mat = matrix(11:50/100, ncol=4,nrow=10) #Matrix with values out to the
hundredths
any((mat * 100)%%1!=0)

But oddly enough it doesn't work. Even in this simple example the result I
get is this:
[,1]         [,2] [,3] [,4]
[1,] 0.000000e+00 0.000000e+00    0    0
[2,] 0.000000e+00 0.000000e+00    0    0
[3,] 0.000000e+00 0.000000e+00    0    0
[4,] 1.776357e-15 0.000000e+00    0    0
[5,] 0.000000e+00 0.000000e+00    0    0
[6,] 0.000000e+00 0.000000e+00    0    0
[7,] 0.000000e+00 0.000000e+00    0    0
[8,] 0.000000e+00 3.552714e-15    0    0
[9,] 0.000000e+00 1.000000e+00    0    0
[10,] 0.000000e+00 0.000000e+00    0    0

Two non-zero values are just very small, but one is value is actually 1. Can
someone explain this?

If you pick just a single number you can see some odd results too.

> (4.1*100)%/%1
[1] 409
> (4.1*10*10)%/%1
[1] 410

Shouldn't the result be 410 each time?

I think in this case it should have returned all 0s, and I could have done
something like

newmat = as.integer(mat*100)
dim(newmat) = dim(mat)
rm(mat)

Is there a better way to convert my double matrix to an integer matrix
without losing precision? Or are there better ways to conserve memory? I'm
at the limit.

Thanks,
Justin
--
View this message in context: http://n4.nabble.com/Odd-results-with-and-conserving-memory-tp1692845p1692845.html
Sent from the R help mailing list archive at Nabble.com.

```