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); } } }
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 byz/nand then add it to the sum. - i-one