Exemple #1
0
        public void Go()
        {
            var stopwatch = Stopwatch.StartNew();

            HighPrecision.Precision = _dp;
            HighPrecision first  = 4 * Atan.Calculate(5);
            HighPrecision second = Atan.Calculate(239);

            var pi = 4 * (first - second);

            stopwatch.Stop();

            WriteLine($"I calculated Pi to {_dp} decimal places in {stopwatch.ElapsedMilliseconds}ms. The last digit is {pi.ToString().Last()}.");
        }
Exemple #2
0
        public IActionResult Index(int?dp = 6)
        {
            var stopwatch = Stopwatch.StartNew();

            HighPrecision.Precision = dp.Value;
            HighPrecision first  = 4 * Atan.Calculate(5);
            HighPrecision second = Atan.Calculate(239);

            var pi = 4 * (first - second);

            var model = new PiModel
            {
                DecimalPlaces       = dp.Value,
                Value               = pi.ToString(),
                ComputeMilliseconds = stopwatch.ElapsedMilliseconds,
                ComputeHost         = Environment.MachineName
            };

            return(View(model));
        }
        public static HighPrecision Calculate(int denominator)
        {
            HighPrecision result   = new HighPrecision(1, denominator);
            int           xSquared = denominator * denominator;

            int           divisor = 1;
            HighPrecision term    = result;

            while (!term.IsZero)
            {
                divisor += 2;
                term    /= xSquared;
                result  -= term / divisor;

                divisor += 2;
                term    /= xSquared;
                result  += term / divisor;
            }

            return(result);
        }