-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaylorSeriesFunctions.cs
More file actions
48 lines (40 loc) · 1.48 KB
/
TaylorSeriesFunctions.cs
File metadata and controls
48 lines (40 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
using System;
namespace Application1 {
/// <summary>
/// Evaluation arctg function with using Taylor Series.
/// </summary>
class TaylorSeriesFunctions {
static double ToTwoPlusOne(double n) {
return n++ + n;
}
static int SignValue(int value) {
return value % 2 == 0 ? 1 : -1;
}
static double X(double r, double x, int n) {
if ( n == 0 ) {
return r;
}
int signValue = SignValue(n);
double k = ToTwoPlusOne(n);
double currentR = r + signValue * Math.Pow(x, k) / k;
return X(currentR, x, --n);
}
/// <summary>
/// Calculate arctg function with using Taylor Series for
/// specified x and n.
/// </summary>
/// <param name="x">Value х(from -1 to 1).</param>
/// <param name="n">Number of elements in Taylor Series.</param>
public static double Arctg(double x, int n) {
return Math.Abs(x) <= 1 ? X(x, x, n) : double.NaN;
}
/// <summary>
/// Show result to Console stream out in readable format.
/// </summary>
/// <param name="x">Value х(from -1 to 1).</param>
/// <param name="n">Number of elements in Taylor Series.</param>
public static void View(double x, int n) {
Console.WriteLine("arctg (" + x + ") = " + Arctg(x, n));
}
}
}