Good day.

The program for calculating the function (exp ^ z) with some accuracy, by expanding into a series. Implementation through the structure. This program performs correctly with small powers (1 + 1i), (2 + 1i). On degree (3 + 3i) and more issues "NaN". I think that dealing with data types, because factorials of large numbers are no longer processed correctly. Is there any way to solve this problem?

Thank you in advance.

class Program { // Π”Π°Π½ΠΎ комплСксноС число z ΠΈ вСщСствСнноС число eps.Π’Ρ‹Ρ‡ΠΈΡΠ»ΠΈΡ‚ΡŒ с Ρ‚ΠΎΡ‡Π½ΠΎΡΡ‚ΡŒΡŽ Π΅ps Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ комплСксной Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ //exp(z) = 1 + z/1! + z^2/2! +...+ z^n/n! +... public struct Complex { public double i, r; public Complex(double r,double i) { this.i = i; this.r = r; } static public Complex Sum(Complex a,Complex b) { Complex cx = new Complex(); cx.r = ar + br; cx.i = ai + bi; return cx; } static public Complex Power(Complex a,long n) { if (n == 0 || n == 1) return a; Complex cx = new Complex(); double arg=0; if (ar > 0) arg =Math.Atan(ai /ar); if (ar < 0 && ai >= 0) arg = Math.PI + Math.Atan(ai/ar); if (ar < 0 && ai < 0) arg = -Math.PI +Math.Atan(ai /ar); if (ar == 0&&a.i>0) arg =Math.PI / 2; if (ar == 0 && ai < 0) arg =-Math.PI / 2; cx.r =Math.Pow(Complex.abs(a), n) * (Math.Cos(n*arg)); cx.i =Math.Pow(Complex.abs(a), n) * (Math.Sin(n * arg)); return cx; } static public Complex Division(Complex a,long n)// Π΄Π΅Π»Π΅Π½ΠΈΠ΅ комплСксного числа Π½Π° Ρ†Π΅Π»ΠΎΠ΅ { Complex cx = new Complex(); cx.r = (ar * n) / (n * n); cx.i = (ai * n) / (n * n); return cx; } static public double abs (Complex a) { double result =Math.Sqrt((ar * ar) +(ai * ai)); return result; } static public Complex exp(Complex z, double eps) { long i = 1; Complex sum = new Complex(); sum.r = 1; sum.i = 0; Complex tmp = new Complex(); tmp.i = 0; tmp.r = 1; while (Complex.abs(tmp) > eps) { tmp = Complex.Division(Complex.Power(z, i), Factorial(i)); sum = Complex.Sum(sum, tmp); i++; } return sum; } } static public long Factorial(long n) { long result = 1; if (n == 0) return result; for (long i = n; i >= 1; i--) result = result * i; return result; } static void Main(string[] args) { Complex z1 = new Complex(); Complex z2 = new Complex(); z1.r = 4; z1.i = -2; double eps =0.1; z2 = Complex.exp(z1, eps); Console.WriteLine(z2.r); Console.WriteLine(z2.i); } } } 
  • five
    It is not necessary to calculate the sum of a series by programming it "in the forehead" by the formula. No need to calculate z^n/n! . You need to save the previous term in the sequence to a variable, and then calculate the next term by multiplying the previous by z/n and then add it to the sum. - i-one
  • If anything, with .NET 4 there is a special type Complex . - andreycha

0