Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <stdio.h>
- #include <time.h>
- #include <windows.h>
- #include <iomanip>
- #include <iostream>
- #include <string>
- using namespace std;
- void main()
- {
- float a = 0, b = 0, step = 0, x = 0;
- cout << "Enter A: "; cin >> a;
- cout << "Enter B: "; cin >> b;
- step = (b - a) / 10000;
- cout << left << setw(4) << "Step" << " | " << setw(10) << "x" << " | " << setw(10) << "sqrt(1-x)" << " | " << setw(10) << "Taylor" << endl;
- clock_t start = clock();
- int count = 1;
- for (float stepX = a; stepX < b; stepX += step, count++) {
- x = stepX;
- float summ = 1;
- summ -= x / 2;
- for (int i = 1; i < 25; i++) {
- float numerator = 1, denominator = 1;
- string strnumerator = "1*";
- string strdenominator = "";
- int tmp = 1;
- for (int j = 0; j < i; j++) {
- if (j < i - 1)
- strnumerator += to_string(tmp) + "*";
- else
- strnumerator += to_string(tmp);
- numerator *= tmp;
- tmp += 2;
- }
- tmp = 0;
- for (int j = 0; j <= i; j++) {
- tmp += 2;
- if (j < i)
- strdenominator += to_string(tmp) + "*";
- else
- strdenominator += to_string(tmp);
- denominator *= tmp;
- }
- summ -= (numerator / denominator)*pow(x, (i + 1));
- }
- float sqr = sqrt(1 - x);
- cout << left << setw(4) << count << " | " << setw(10) << x << " | " << setw(10) << sqr << " | " << setw(10) << summ << endl;
- }
- clock_t end = clock();
- double seconds = (double)(end - start) / CLOCKS_PER_SEC;
- cout << "Execution time: " << seconds << " seconds" << endl;
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment