-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathTaylor.cpp
More file actions
68 lines (67 loc) · 1.38 KB
/
Taylor.cpp
File metadata and controls
68 lines (67 loc) · 1.38 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "Taylor.h"
myExp::myExp(size_t n = 0)
{
elements.resize(n);
}
void myExp::setSize(size_t n)
{
elements.resize(n);
}
size_t myExp::getSize()
{
return elements.size();
}
void myExp::findElements(double _a)
{
a = _a;
double buff = 1;
double exp_a = exp(a);
for (int i = 0; i < elements.size(); i++)
{
elements[i] = exp_a / buff;
buff *= (i + 1);
}
}
void myExp::showEquation()
{
std::cout << elements[0];
for (int i = 1; i < elements.size(); i++)
{
std::cout << "+" << elements[i] << "(x-" << a << ")^" << i;
}
std::cout << std::endl;
}
double myExp::operator[](size_t index)
{
return elements[index];
}
double myExp::calculate(double x)
{
double res = elements[0];
for (int i = 1; i < elements.size(); i++)
{
res += elements[i] * pow(x - a, i);
}
return res;
}
double myExp::accuracy(double x)
{
return abs(calculate(x) - exp(x));
}
std::ofstream& operator<<(std::ofstream& ofs, myExp& obj)
{
for (int i = 0; i < obj.elements.size(); i++)
{
ofs << std::fixed << obj.elements[i] << " ";
}
ofs << std::endl;
return ofs;
}
std::ifstream& operator>>(std::ifstream& ifs, myExp& obj)
{
for (int i = 0; i < obj.elements.size(); i++)
{
ifs >> obj.elements[i];
}
return ifs;
}