Task: Given a numerical series and some number e> 0. Find the sum of those sequence members of the series whose modulus is greater than or equal to the given e.
Given a sequence of the form An = n! / (2n)!
My code is:
class Program { public static double fact(double f) { double fa = 1; for (int i = 1; i <= f; i++) { fa *= i; } return fa; } public static void Main(string[] args) { Console.Write("Enter E = "); double e = System.Convert.ToDouble(Console.ReadLine()); double j = 1; double n = fact(j) / fact(2*j); double sum = 0; while(Math.Abs(n) >= e) { Console.WriteLine(n); sum += n; j++; n = fact(j) / fact(2*j); } Console.WriteLine("SUM= {0}", sum); Console.ReadKey(); } } There is a problem when counting, when we start to consider an element in the order of 5 or more.
For example:
A5 = fact (5) / fact (5 * 2); // C # brings us 3,30687830687831E-05
We consider this value manually. We get => fact (5) = 120; fact (10) = 3628800;
120/3628800 = 0.00003306878
Question: Why does C # not calculate this exactly? Because of this, the code ignores these values and, by condition, they do not pass into the sum.
3,30687830687831E-05equivalent to3,30687830687831 * 10^-5or0,0000330687830687831- tym32167