Exemple #1
0
        public void Factorial()
        {
            Assert.AreEqual(1L, GeneralMath.Factorial(0));
            var r = new Random();

            for (int i = 0; i < 16; i++)
            {
                int n = -r.Next();
                Assert.Throws <ArgumentException>(() => GeneralMath.Factorial(n));
                Assert.Throws <ArgumentException>(() => GeneralMath.Factorial((double)n));
            }

            long currentResult = 1;

            for (int multiplier = 1; multiplier <= 20; multiplier++)
            {
                currentResult *= multiplier;
                Assert.AreEqual(currentResult, GeneralMath.Factorial(multiplier));
                Assert.AreEqual((double)currentResult, GeneralMath.Factorial((double)multiplier));
            }

            double currentDoubleResult = currentResult;

            for (int multiplier = 21; ; multiplier++)
            {
                currentDoubleResult *= multiplier;
                if (double.IsPositiveInfinity(currentDoubleResult))
                {
                    break;
                }

                double delta = Math.Pow(10, (long)Math.Log10(currentDoubleResult) - 14);
                Assert.AreEqual(currentDoubleResult, GeneralMath.Factorial((double)multiplier), delta, multiplier.ToString());
            }
        }