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.
You seem to be in Circle 1 of 'The R Inferno'. Your technique does work, just not the way that you expect. Try doing: range( (mat * 100) %% 1) The 'zapsmall' function might be of interest as well. On 26/03/2010 21:05, JustinNabble wrote:> > 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-- Patrick Burns pburns at pburns.seanet.com http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno')
FAQ 7.31 Follow the link to what you should know about floating point numbers. On Fri, Mar 26, 2010 at 5:05 PM, JustinNabble <justinmmcgrath@hotmail.com>wrote:> > 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. > > ______________________________________________ > R-help@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<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? [[alternative HTML version deleted]]