-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path8.cpp
More file actions
41 lines (32 loc) · 737 Bytes
/
8.cpp
File metadata and controls
41 lines (32 loc) · 737 Bytes
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
#include <iostream>
#include <cmath>
#include <iomanip>
long long factorial(int n) {
long long ans = 1;
for (int i = 1; i <= n; i++) {
ans *= i;
}
return ans;
}
double cos_taylor(double x, int terms) {
double result = 0.0;
for (int n = 0; n < terms; n++) {
double term = std::pow(-1, n) * std::pow(x, 2 * n) / factorial(2 * n);
result += term;
}
return result;
}
void solve() {
double x;
int n;
std::cin >> x >> n;
double my_cos = cos_taylor(x, n);
double std_cos = std::cos(x);
std::cout << std::fixed << std::setprecision(6);
std::cout << my_cos << std::endl;
std::cout << std_cos << std::endl;
}
int main() {
solve();
return 0;
}