C++ Bc. 9 cpp: Porovnání verzí
m +kategorie programovani, c++ |
zjednodusena verze tridy chyba |
||
Řádek 5: | Řádek 5: | ||
struct Chyba | struct Chyba | ||
{ | { | ||
Chyba(std::string t) : text(t) {} | |||
std::string text; | std::string text; |
Verze z 8. 12. 2006, 15:40
#include <iostream> #include <iomanip> #include <cmath> struct Chyba { Chyba(std::string t) : text(t) {} std::string text; }; double maclaurin (double x, int a, int& iter, double rel_chyba = 1e-12); int main () { using namespace std; double m, mstd; int pocet_iteraci; for (double x = -1.20; x <= 1.21; x += 0.4) { for (int a = -2; a <= 2; a++) { try { cout.setf (ios_base::fixed, ios_base::floatfield); // explicitne se uvadi znamenko '+' cout.setf (ios_base::showpos); cout << setprecision(2) << setw (3) << x << ' ' << setw(3) << a << ' '; m = maclaurin (x, a, pocet_iteraci); mstd = pow (1 + x, a); cout << setprecision( 6) << setw (10) << m << ' ' << setw (10) << mstd; cout.setf (ios_base::scientific, ios_base::floatfield); cout.unsetf(ios_base::showpos); cout << ' ' << std::abs(m - mstd); cout.setf (ios_base::fixed); cout << ' ' << pocet_iteraci << endl; } catch (Chyba& err) { cout << err.text << endl; } } for (int i = 0; i < 72; i++) { cout << "-"; } cout << endl; } return 0; } double maclaurin (double x, int a, int& iter, double rel_chyba) { if ( a < 0 && (x >= 1.0 || x <= -1.0)) { throw Chyba (" ..."); } double dif = a * x; double vysledek = 1.0 + dif; int n = 2; while (std::abs (dif) / rel_chyba > std::abs (vysledek)) { dif *= x * (a - (n - 1)) / n; vysledek += dif; n++; } iter = n; return vysledek; }
Příklad výstupu:
-1.20 -2 ... -1.20 -1 ... -1.20 0 +1.000000 +1.000000 0.000000e+00 2 -1.20 +1 -0.200000 -0.200000 0.000000e+00 3 -1.20 +2 +0.040000 +0.040000 5.551115e-17 4 ------------------------------------------------------------------------ -0.80 -2 +25.000000 +25.000000 8.908785e-11 133 -0.80 -1 +5.000000 +5.000000 1.834444e-11 118 -0.80 0 +1.000000 +1.000000 0.000000e+00 2 -0.80 +1 +0.200000 +0.200000 0.000000e+00 3 -0.80 +2 +0.040000 +0.040000 6.938894e-18 4 ------------------------------------------------------------------------ -0.40 -2 +2.777778 +2.777778 1.755041e-12 34 -0.40 -1 +1.666667 +1.666667 7.680523e-13 31 -0.40 0 +1.000000 +1.000000 0.000000e+00 2 -0.40 +1 +0.600000 +0.600000 0.000000e+00 3 -0.40 +2 +0.360000 +0.360000 0.000000e+00 4 ------------------------------------------------------------------------ +0.00 -2 +1.000000 +1.000000 2.220446e-16 2 +0.00 -1 +1.000000 +1.000000 1.110223e-16 2 +0.00 0 +1.000000 +1.000000 0.000000e+00 2 +0.00 +1 +1.000000 +1.000000 0.000000e+00 2 +0.00 +2 +1.000000 +1.000000 2.220446e-16 2 ------------------------------------------------------------------------ +0.40 -2 +0.510204 +0.510204 1.236788e-13 36 +0.40 -1 +0.714286 +0.714286 1.315614e-13 32 +0.40 0 +1.000000 +1.000000 0.000000e+00 2 +0.40 +1 +1.400000 +1.400000 0.000000e+00 3 +0.40 +2 +1.960000 +1.960000 0.000000e+00 4 ------------------------------------------------------------------------ +0.80 -2 +0.308642 +0.308642 1.266764e-13 153 +0.80 -1 +0.555556 +0.555556 2.191580e-13 128 +0.80 0 +1.000000 +1.000000 0.000000e+00 2 +0.80 +1 +1.800000 +1.800000 0.000000e+00 3 +0.80 +2 +3.240000 +3.240000 4.440892e-16 4 ------------------------------------------------------------------------ +1.20 -2 ... +1.20 -1 ... +1.20 0 +1.000000 +1.000000 0.000000e+00 2 +1.20 +1 +2.200000 +2.200000 0.000000e+00 3 +1.20 +2 +4.840000 +4.840000 0.000000e+00 4
[ Zpět ]