Note that there are some explanatory texts on larger screens.

plurals
  1. POArbitrary Precision for decimals in C# help?
    primarykey
    data
    text
    <p>Here is my current code for computing Pi using the chudnovsky method in c#:</p> <pre><code>using System; using System.Diagnostics; using System.IO; using java.math; namespace pi.chudnovsky { public class Program { static Double Factorial(Double fact) { //begin factorial function if (fact &lt;= 1) return 1; else return fact * Factorial(fact - 1); //loops multiplication until the factorial is reached } static Double doSummation(Double maxPower) { //begin chudnovsky summation function Double sum = 0; for (int i = 0; i &lt;= maxPower; i++) //starts at i=0 { sum += ((Math.Pow(-1, i)) * Factorial(6 * i) * (13591409 + 5451401 * i)) / (Factorial(3 * i) * Factorial(i) * Factorial(i) * Factorial(i) * Math.Pow(640320, (3 * i + 1.5))); //chudnovsky algorithm } return sum; } static void Main(string[] args) { int num; Console.WriteLine("Enter how many terms to compute Chudnovsky summation: "); //begin stopwatch Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); //parse user input num = Convert.ToInt32(Console.ReadLine()); //perform calculation Double inv = 1 / (12 * doSummation(num)); //stop stopwatch stopwatch.Stop(); //display info Console.WriteLine(inv); Console.WriteLine("3.14159265358979323846264338327950288419716939937510"); Console.WriteLine("Time elapsed: {0}", stopwatch.Elapsed.TotalMilliseconds); //write to pi.txt TextWriter pi = new StreamWriter("pi.txt"); pi.WriteLine(inv); pi.Close(); //write to stats.txt TextWriter stats = new StreamWriter("stats.txt"); stats.WriteLine(stopwatch.Elapsed.TotalMilliseconds); stats.Close(); } } } </code></pre> <p>So, I've included the J# library, and included java.math. Now when I replace all the "double"s with "BigDecimal"s, I get these compile errors:</p> <p><a href="http://f.cl.ly/items/1r2X26470d0d0n260p0p/Image%202011-11-14%20at%206.16.19%20PM.png" rel="nofollow">http://f.cl.ly/items/1r2X26470d0d0n260p0p/Image%202011-11-14%20at%206.16.19%20PM.png</a></p> <p>I know that this isn't the problem with me using Int for the loops, as it worked perfectly with Doubles. My question is, how do you resolve these errors relating to int and BigDecimal, or can you recommend another arbitrary precision library?</p> <p>I've tried using XMPIR, have gotten everything to compile, but I get:</p> <p><a href="http://f.cl.ly/items/1l3C371j2u3z3n2g3a0j/Image%202011-11-14%20at%206.20.24%20PM.png" rel="nofollow">http://f.cl.ly/items/1l3C371j2u3z3n2g3a0j/Image%202011-11-14%20at%206.20.24%20PM.png</a></p> <p>So I can I use p/invoke to include xmpir so I can use whatever the bigdecimal class is?</p> <p>Thank you for your time!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload