1

This is just out of curiosity. We know that for cryptology applications two large prime numbers (of more than 100 digits) are multiplied to form a part of the public key. So which Microsoft.NET number type can store the largest prime numbers, of hundreds of digits ? In other words which numeric data type can accommodate the largest whole number in .NET ? I understand that System.Double which uses 8 bytes, can store a max value of (1.79769313486232 × 10^308) - this probably accommodates 309 digits. However, System.Decimal uses 16 bytes and can store a max of 79228162514264337593543950335 which is only 29 decimal digits (lesser digits than Double ?)

Thanks, Chak.

Chakra
  • 2,525
  • 8
  • 43
  • 82
  • "What Every Computer Programmer Should Know About Floating-Point Arithmetic" - http://blogs.sun.com/darcy/resource/Wecpskafpa-StanfordIcme500.pdf – Jonas Elfström Nov 29 '10 at 09:05
  • For RSA you need the less significant digits too(you're doing integer calculations modulo n), so you can't use any floating-point type(Double,Decimal,...) but need a big enough integer. – CodesInChaos Nov 29 '10 at 09:30

1 Answers1

2

If you're using .NET 4, have a look at System.Numerics.BigInteger which can store arbitrarily large integers.

double does not store 309 digits - it has a wide range, but it only has 15-16 digits of precision. In other words, if you convert large long values (e.g. those just below long.MaxValue) to double and back you'll lose information. See my articles on binary and decimal floating point types for more information.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Thanks. This is an Out of thread question- if one is preparing for the 70-536 .NET Framework Foundation exam we will not be aware of System.Numerics.BigInteger, which you pointed out. Is there an updated exam for .NET 4 ? – Chakra Nov 29 '10 at 09:16
  • @Chakra: I have no idea, I'm afraid. I don't really "do" certification. – Jon Skeet Nov 29 '10 at 09:18