I am a statistician and I come up to an interesting problem in cryptography. I would like to use R since there are some statistical procedures that I need to use. However, I run into a problem when using the modulus operator %%. I am using R 2.2.1 and when I calculate modulus for large numbers (that I need with my problem) R gives me warnings. For instance if one does: a=1:40; 8^a %% 41 one obtains zeros which is not possible since 8 to any power is not a multiple of 41. In addition when working with numbers larger that this and with the mod operator R crashes randomly. I believe this is because R stores large integers as real numbers thus there may be lack of accuracy when applying the modulus operator and converting back to integers. So my question is this: Is it possible to increase the size of memory used for storing integers? Say from 32 bits to 512 bits (Typical size of integers in cryptography). Thank you, any help would be greatly appreciated. Ionut Florescu
You have reached the maximum value that can be stored accurately in a floating point number. That is what the error message is telling you. I get 21 warnings and this says that at 8^20 I am now truncating digits in the variable. You only have about 54 bits in the floating point number and you exceed this about 8^19.> a=1:40; > 8^a %% 41[1] 8 23 20 37 9 31 2 16 5 40 33 18 21 4 32 10 39 25 36 1 8 23 20 37 9 31 2 16 5 40 33 [32] 18 21 4 32 10 0 0 0 0 There were 21 warnings (use warnings() to see them)> warnings()Warning messages: 1: probable complete loss of accuracy in modulus 2: probable complete loss of accuracy in modulus 3: probable complete loss of accuracy in modulus 4: probable complete loss of accuracy in modulus 5: probable complete loss of accuracy in modulus 6: probable complete loss of accuracy in modulus 7: probable complete loss of accuracy in modulus 8: probable complete loss of accuracy in modulus 9: probable complete loss of accuracy in modulus 10: probable complete loss of accuracy in modulus 11: probable complete loss of accuracy in modulus 12: probable complete loss of accuracy in modulus 13: probable complete loss of accuracy in modulus 14: probable complete loss of accuracy in modulus 15: probable complete loss of accuracy in modulus 16: probable complete loss of accuracy in modulus 17: probable complete loss of accuracy in modulus 18: probable complete loss of accuracy in modulus 19: probable complete loss of accuracy in modulus 20: probable complete loss of accuracy in modulus 21: probable complete loss of accuracy in modulus> > 8^35[1] 4.056482e+31> 8^36[1] 3.245186e+32> 8^19[1] 1.441152e+17> 8^19%%41[1] 36> 8^20[1] 1.152922e+18> 8^20%%41[1] 1 Warning message: probable complete loss of accuracy in modulus>On 1/30/06, Ionut Florescu <ifloresc@stevens.edu> wrote:> > I am a statistician and I come up to an interesting problem in > cryptography. I would like to use R since there are some statistical > procedures that I need to use. > However, I run into a problem when using the modulus operator %%. > > I am using R 2.2.1 and when I calculate modulus for large numbers (that > I need with my problem) R gives me warnings. For instance if one does: > a=1:40; > 8^a %% 41 > one obtains zeros which is not possible since 8 to any power is not a > multiple of 41. > In addition when working with numbers larger that this and with the mod > operator R crashes randomly. > > I believe this is because R stores large integers as real numbers thus > there may be lack of accuracy when applying the modulus operator and > converting back to integers. > > So my question is this: Is it possible to increase the size of memory > used for storing integers? Say from 32 bits to 512 bits (Typical size of > integers in cryptography). > > Thank you, any help would be greatly appreciated. > Ionut Florescu > > ______________________________________________ > R-help@stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >-- Jim Holtman Cincinnati, OH +1 513 247 0281 What the problem you are trying to solve? [[alternative HTML version deleted]]
On 1/30/2006 11:32 AM, Ionut Florescu wrote:> I am a statistician and I come up to an interesting problem in > cryptography. I would like to use R since there are some statistical > procedures that I need to use. > However, I run into a problem when using the modulus operator %%. > > I am using R 2.2.1 and when I calculate modulus for large numbers (that > I need with my problem) R gives me warnings. For instance if one does: > a=1:40; > 8^a %% 41 > one obtains zeros which is not possible since 8 to any power is not a > multiple of 41. > In addition when working with numbers larger that this and with the mod > operator R crashes randomly.Could you keep a record of the random crashes, and see if you can make any of them repeatable? R shouldn't crash. If you can find a repeatable way to make it crash, then that's a bug that needs to be fixed. (If it crashes at random it should still be fixed, but it's so much harder to fix that it's unlikely to happen unless the cases are ones that look likely to come up in normal situations.)> > I believe this is because R stores large integers as real numbers thus > there may be lack of accuracy when applying the modulus operator and > converting back to integers. > > So my question is this: Is it possible to increase the size of memory > used for storing integers? Say from 32 bits to 512 bits (Typical size of > integers in cryptography).No, but there is at least one contributed package that does multiple precision integer arithmetic. I can't remember the name of it right now, but Google should be able to find it for you... Duncan Murdoch> > Thank you, any help would be greatly appreciated. > Ionut Florescu > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
R is probably not the best tool for handling large integers... AFAIK Python has the interesting feature of not only having a `long' integer type (that can store arbitrarily large integers), but can convert a regular 4-byte int to the `long' type when necessary. Perhaps that would be a better tool for the purpose. Andy From: jim holtman> > You have reached the maximum value that can be stored accurately in a > floating point number. That is what the error message is > telling you. I > get 21 warnings and this says that at 8^20 I am now > truncating digits in the > variable. You only have about 54 bits in the floating point > number and you > exceed this about 8^19. > > > a=1:40; > > 8^a %% 41 > [1] 8 23 20 37 9 31 2 16 5 40 33 18 21 4 32 10 39 25 36 > 1 8 23 20 > 37 9 31 2 16 5 40 33 > [32] 18 21 4 32 10 0 0 0 0 > There were 21 warnings (use warnings() to see them) > > warnings() > Warning messages: > 1: probable complete loss of accuracy in modulus > 2: probable complete loss of accuracy in modulus > 3: probable complete loss of accuracy in modulus > 4: probable complete loss of accuracy in modulus > 5: probable complete loss of accuracy in modulus > 6: probable complete loss of accuracy in modulus > 7: probable complete loss of accuracy in modulus > 8: probable complete loss of accuracy in modulus > 9: probable complete loss of accuracy in modulus > 10: probable complete loss of accuracy in modulus > 11: probable complete loss of accuracy in modulus > 12: probable complete loss of accuracy in modulus > 13: probable complete loss of accuracy in modulus > 14: probable complete loss of accuracy in modulus > 15: probable complete loss of accuracy in modulus > 16: probable complete loss of accuracy in modulus > 17: probable complete loss of accuracy in modulus > 18: probable complete loss of accuracy in modulus > 19: probable complete loss of accuracy in modulus > 20: probable complete loss of accuracy in modulus > 21: probable complete loss of accuracy in modulus > > > > 8^35 > [1] 4.056482e+31 > > 8^36 > [1] 3.245186e+32 > > 8^19 > [1] 1.441152e+17 > > 8^19%%41 > [1] 36 > > 8^20 > [1] 1.152922e+18 > > 8^20%%41 > [1] 1 > Warning message: > probable complete loss of accuracy in modulus > > > > > > On 1/30/06, Ionut Florescu <ifloresc at stevens.edu> wrote: > > > > I am a statistician and I come up to an interesting problem in > > cryptography. I would like to use R since there are some statistical > > procedures that I need to use. > > However, I run into a problem when using the modulus operator %%. > > > > I am using R 2.2.1 and when I calculate modulus for large > numbers (that > > I need with my problem) R gives me warnings. For instance > if one does: > > a=1:40; > > 8^a %% 41 > > one obtains zeros which is not possible since 8 to any > power is not a > > multiple of 41. > > In addition when working with numbers larger that this and > with the mod > > operator R crashes randomly. > > > > I believe this is because R stores large integers as real > numbers thus > > there may be lack of accuracy when applying the modulus operator and > > converting back to integers. > > > > So my question is this: Is it possible to increase the size > of memory > > used for storing integers? Say from 32 bits to 512 bits > (Typical size of > > integers in cryptography). > > > > Thank you, any help would be greatly appreciated. > > Ionut Florescu > > > > ______________________________________________ > > R-help at stat.math.ethz.ch mailing list > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide! > > http://www.R-project.org/posting-guide.html > > > > > > -- > Jim Holtman > Cincinnati, OH > +1 513 247 0281 > > What the problem you are trying to solve? > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > >
The other thing that you have to be aware of is that 8^n is not 8 multiplied by itself n times. You are probably using logs to compute this. Here is a sample of 8^(1:20). The value of 8^2 is 64.000000000000004 (not exactly an integer); roundoff errors are apparent in the other values.> 8^(1:20)[1] 8.0000000000000000e+00 6.4000000000000004e+01 5.1200000000000001e+02 4.0960000000000001e+03 [5] 3.2768000000000002e+04 2.6214400000000002e+05 2.0971519999999999e+06 1.6777215999999999e+07 [9] 1.3421772800000000e+08 1.0737418240000001e+09 8.5899345920000005e+09 6.8719476736000003e+10 [13] 5.4975581388799997e+11 4.3980465111039999e+12 3.5184372088832001e+13 2.8147497671065600e+14 [17] 2.2517998136852482e+15 1.8014398509481984e+16 1.4411518807585588e+17 1.1529215046068471e+18>On 1/30/06, Ionut Florescu <ifloresc@stevens.edu> wrote:> > I am a statistician and I come up to an interesting problem in > cryptography. I would like to use R since there are some statistical > procedures that I need to use. > However, I run into a problem when using the modulus operator %%. > > I am using R 2.2.1 and when I calculate modulus for large numbers (that > I need with my problem) R gives me warnings. For instance if one does: > a=1:40; > 8^a %% 41 > one obtains zeros which is not possible since 8 to any power is not a > multiple of 41. > In addition when working with numbers larger that this and with the mod > operator R crashes randomly. > > I believe this is because R stores large integers as real numbers thus > there may be lack of accuracy when applying the modulus operator and > converting back to integers. > > So my question is this: Is it possible to increase the size of memory > used for storing integers? Say from 32 bits to 512 bits (Typical size of > integers in cryptography). > > Thank you, any help would be greatly appreciated. > Ionut Florescu > > ______________________________________________ > R-help@stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >-- Jim Holtman Cincinnati, OH +1 513 247 0281 What the problem you are trying to solve? [[alternative HTML version deleted]]