Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

select the right type of variable C#

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

like image 740
Superzarzar Avatar asked Dec 06 '25 04:12

Superzarzar


1 Answers

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();
}
like image 60
Bobson Avatar answered Dec 08 '25 01:12

Bobson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!