3

i want to create pi more than 16 thousand numbers after the decimal point but the problam is that i kant find any type that could fill my wishes. I tried to use object but it rounds the number after 10 I thought using string but string type wont be able to do minus or plus and if i will convert to int it will become round. do you know any other types i can use or how can i make it work with a string for example http://files.extremeoverclocking.com/file.php?f=36 this program is able to calculate up to 32 million

thnx

Timothy Shields
  • 75,459
  • 18
  • 120
  • 173
Superzarzar
  • 509
  • 2
  • 8
  • 16
  • 1
    Perhaps try a [BitArray](http://msdn.microsoft.com/en-us/library/system.collections.bitarray.aspx). See [this question](http://stackoverflow.com/questions/11282727/how-can-i-use-a-very-large-string-500-million-characters-in-my-program) – tnw May 20 '13 at 18:56
  • 1
    What you're looking for would be called, in more technical language, an arbitrary precision decimal (or arbitrary precision float). I don't have any personal experience with them, but you could try the answers from [this](http://stackoverflow.com/questions/4116078/need-arbitrary-precision-arithmetic-in-c-sharp) question? – neminem May 20 '13 at 19:00
  • This is a possible duplicate of this thread: http://stackoverflow.com/questions/176775/big-integers-in-c-sharp – OnoSendai May 20 '13 at 19:18
  • 2
    @OnoSendai - Not really, since that's about integers and this is about decimals. They're certainly similar in concept, though. – Bobson May 20 '13 at 19:35
  • @Bobson, you're completely right; thanks for pointing out. – OnoSendai May 20 '13 at 20:03
  • 1
    Download Microsoft solver foundation. Use the Rational type. – Eric Lippert May 21 '13 at 00:13

1 Answers1

5

Take a look at this page and it's BigNumber class.

The first thing to note is that we can't use any of the primitive types in C#: they just don't have enough precision (the double type, for example, only has 15 significant digits, and we want to calculate, say, 10000). We need a large precision number library, but there's nothing like that in the .NET Framework, and so the first thing we'll have to do is write a BigNumber class.

That page also provides an example usage for calculating pi out to 1000 decimal places, so it should be fairly trivial to run it for 16000.


Running it for 16000 digits takes me about a second and a half, for 160,000 digits takes me a bit over 2 minutes.

Here's my PrintAsTable() function, which the linked post leaves as an exercise for the reader:

public string PrintAsTable() {
    var data = Print();
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < data.Length; i++)
    {
        if (data[i] == '.') sb.AppendLine(".");
        else if ((i -1) % 50 == 0) sb.AppendLine(data[i].ToString());
        else if ((i -1) % 10 == 0) {sb.Append(data[i].ToString()); sb.Append(" ");}
        else sb.Append(data[i].ToString());
    }
    return sb.ToString();
}
Bobson
  • 13,498
  • 5
  • 55
  • 80