C# - Using Cosine Series to find COS Value
I have given here a C# program to find the value of cos using Cosine Series and without using the library function. This program will print the values got from the series function MyCos and System.Math.Cos.
Cosine series = 1 - x^2/2! + x^4/4! - x^6 / 6! + x^8 / 8! - .....
Source Code
using System;
using System.Collections.Generic;
using System.Text;
namespace SoftwareAndFinance
{
class Math
{
const double PI = 3.14159265;
// cos series = 1 - x^2/2! + x^4/4! - x^6 / 6!
public static double MyCos(double x)
{
double sqx = x * x;
double cosineresult = 1;
double fact = 2;
int index = 2;
int term = 0;
double termfactor = 0;
for (term = 1; term < 20; term++)
{
termfactor = 0;
termfactor = sqx / fact;
if ( (term % 2) == 1)
{
cosineresult = cosineresult - termfactor;
}
else
{
cosineresult = cosineresult + termfactor;
}
index++;
fact *= index;
index++;
fact *= index;
sqx *= (x * x);
}
return cosineresult;
}
static void Main(string[] args)
{
for (double d = 0; d <= 360; d += 2.5)
{
Console.WriteLine("{0,6:f2} = {1,8:f5} {2,8:f5}", d, (double)System.Math.Cos(d * PI / 180.0), MyCos(d * PI / 180.0));
}
}
}
}
Click here to get the C# project along with executable
Output
0.00 = 1.00000 1.00000
2.50 = 0.99905 0.99905
5.00 = 0.99619 0.99619
7.50 = 0.99144 0.99144
10.00 = 0.98481 0.98481
12.50 = 0.97630 0.97630
15.00 = 0.96593 0.96593
17.50 = 0.95372 0.95372
20.00 = 0.93969 0.93969
22.50 = 0.92388 0.92388
25.00 = 0.90631 0.90631
27.50 = 0.88701 0.88701
30.00 = 0.86603 0.86603
32.50 = 0.84339 0.84339
35.00 = 0.81915 0.81915
37.50 = 0.79335 0.79335
40.00 = 0.76604 0.76604
42.50 = 0.73728 0.73728
45.00 = 0.70711 0.70711
47.50 = 0.67559 0.67559
50.00 = 0.64279 0.64279
52.50 = 0.60876 0.60876
55.00 = 0.57358 0.57358
57.50 = 0.53730 0.53730
60.00 = 0.50000 0.50000
62.50 = 0.46175 0.46175
65.00 = 0.42262 0.42262
67.50 = 0.38268 0.38268
70.00 = 0.34202 0.34202
72.50 = 0.30071 0.30071
75.00 = 0.25882 0.25882
77.50 = 0.21644 0.21644
80.00 = 0.17365 0.17365
82.50 = 0.13053 0.13053
85.00 = 0.08716 0.08716
87.50 = 0.04362 0.04362
90.00 = 0.00000 0.00000
92.50 = -0.04362 -0.04362
95.00 = -0.08716 -0.08716
97.50 = -0.13053 -0.13053
100.00 = -0.17365 -0.17365
102.50 = -0.21644 -0.21644
105.00 = -0.25882 -0.25882
107.50 = -0.30071 -0.30071
110.00 = -0.34202 -0.34202
112.50 = -0.38268 -0.38268
115.00 = -0.42262 -0.42262
117.50 = -0.46175 -0.46175
120.00 = -0.50000 -0.50000
122.50 = -0.53730 -0.53730
125.00 = -0.57358 -0.57358
127.50 = -0.60876 -0.60876
130.00 = -0.64279 -0.64279
132.50 = -0.67559 -0.67559
135.00 = -0.70711 -0.70711
137.50 = -0.73728 -0.73728
140.00 = -0.76604 -0.76604
142.50 = -0.79335 -0.79335
145.00 = -0.81915 -0.81915
147.50 = -0.84339 -0.84339
150.00 = -0.86603 -0.86603
152.50 = -0.88701 -0.88701
155.00 = -0.90631 -0.90631
157.50 = -0.92388 -0.92388
160.00 = -0.93969 -0.93969
162.50 = -0.95372 -0.95372
165.00 = -0.96593 -0.96593
167.50 = -0.97630 -0.97630
170.00 = -0.98481 -0.98481
172.50 = -0.99144 -0.99144
175.00 = -0.99619 -0.99619
177.50 = -0.99905 -0.99905
180.00 = -1.00000 -1.00000
182.50 = -0.99905 -0.99905
185.00 = -0.99619 -0.99619
187.50 = -0.99144 -0.99144
190.00 = -0.98481 -0.98481
192.50 = -0.97630 -0.97630
195.00 = -0.96593 -0.96593
197.50 = -0.95372 -0.95372
200.00 = -0.93969 -0.93969
202.50 = -0.92388 -0.92388
205.00 = -0.90631 -0.90631
207.50 = -0.88701 -0.88701
210.00 = -0.86603 -0.86603
212.50 = -0.84339 -0.84339
215.00 = -0.81915 -0.81915
217.50 = -0.79335 -0.79335
220.00 = -0.76604 -0.76604
222.50 = -0.73728 -0.73728
225.00 = -0.70711 -0.70711
227.50 = -0.67559 -0.67559
230.00 = -0.64279 -0.64279
232.50 = -0.60876 -0.60876
235.00 = -0.57358 -0.57358
237.50 = -0.53730 -0.53730
240.00 = -0.50000 -0.50000
242.50 = -0.46175 -0.46175
245.00 = -0.42262 -0.42262
247.50 = -0.38268 -0.38268
250.00 = -0.34202 -0.34202
252.50 = -0.30071 -0.30071
255.00 = -0.25882 -0.25882
257.50 = -0.21644 -0.21644
260.00 = -0.17365 -0.17365
262.50 = -0.13053 -0.13053
265.00 = -0.08716 -0.08716
267.50 = -0.04362 -0.04362
270.00 = 0.00000 0.00000
272.50 = 0.04362 0.04362
275.00 = 0.08716 0.08716
277.50 = 0.13053 0.13053
280.00 = 0.17365 0.17365
282.50 = 0.21644 0.21644
285.00 = 0.25882 0.25882
287.50 = 0.30071 0.30071
290.00 = 0.34202 0.34202
292.50 = 0.38268 0.38268
295.00 = 0.42262 0.42262
297.50 = 0.46175 0.46175
300.00 = 0.50000 0.50000
302.50 = 0.53730 0.53730
305.00 = 0.57358 0.57358
307.50 = 0.60876 0.60876
310.00 = 0.64279 0.64279
312.50 = 0.67559 0.67559
315.00 = 0.70711 0.70711
317.50 = 0.73728 0.73728
320.00 = 0.76604 0.76604
322.50 = 0.79335 0.79335
325.00 = 0.81915 0.81915
327.50 = 0.84339 0.84339
330.00 = 0.86603 0.86603
332.50 = 0.88701 0.88701
335.00 = 0.90631 0.90631
337.50 = 0.92388 0.92388
340.00 = 0.93969 0.93969
342.50 = 0.95372 0.95372
345.00 = 0.96593 0.96593
347.50 = 0.97630 0.97630
350.00 = 0.98481 0.98481
352.50 = 0.99144 0.99144
355.00 = 0.99619 0.99619
357.50 = 0.99905 0.99905
360.00 = 1.00000 1.00000
|