Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <math.h>
- #include <iostream>
- using namespace std;
- struct result {
- int n;
- double abs, rn;
- };
- result func(double x, double eps) {
- int m = 1, k = 2;
- while (x > k) {
- k = k * 2;
- m++;
- }
- double z = x / k;
- double a = (1 - z) / (1 + z);
- double ln, sum_ln = 0;
- double e4 = eps * 4;
- int n = 0;
- for (int i = 1; i != 0; i++) {
- ln = (pow(a, (2 * i - 1)) / (2 * i - 1));
- sum_ln += ln;
- if (ln < e4) {
- n = i;
- break;
- }
- }
- double rn = ln / 4;
- return { n, abs(log(x) - (m * log(2) - 2 * sum_ln - rn)), rn };
- };
- result func2(double xi, int n) {
- int m = 1, k = 2;
- while (xi > k) {
- k = k * 2;
- m++;
- }
- double z = xi / k;
- double a = (1 - z) / (1 + z);
- double ln, lk = 0;
- for (int i = 1; i <= n; i++) {
- ln = (pow(a, (2 * i - 1)) / (2 * i - 1));
- lk += ln;
- }
- double rn = ln / 4;
- return { n, abs(log(xi) - (m * log(2) - 2 * lk - rn)), rn };
- }
- int main() {
- double a = 0.1;
- double b = 15;
- double x = (a + b) * 0.5;
- cout << "_____________________________________" << endl;
- cout << "| eps | n | Abs error | Rem term |" << endl;
- double eps = 1e-2;
- for (int i = 0; i < 5; i++) {
- result res = func(x, eps);
- cout << "| " << eps << " | " << res.n << " | " << res.abs << " | " << res.rn << " |" << endl;
- eps *= 1e-3;
- }
- int n = func(x, 1e-8).n;
- double h = (b - a) / 10;
- cout << "_____________________________________" << endl;
- cout << "| xi | Abs. error | Rem term |" << endl;
- for (int i = 0; i <= 10; i++) {
- double xi = a + h * i;
- result res = func2(xi, n);
- cout << "| " << xi << " | " << res.abs << " | " << res.rn << " |" << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement