-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblatt02_func_param.cpp
59 lines (52 loc) · 1.86 KB
/
blatt02_func_param.cpp
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
#include <limits>
#include <cmath>
#include <iostream>
#include <iomanip>
using namespace std;
// iteration nimmt eine Funktion vom Typ (double &) --> void als Parameter
void pi_approximation(double iteration(double))
{
double s = sqrt(2.0) / 2.0;
int n = 2;
cout<< ' ' << setw( 3) << "n "
<< ' ' << setw(10) << "Ecken "
<< ' ' << setw(23) << "Seitenlaenge "
<< ' ' << setw(18) << "Inneneck "
<< ' ' << setw(18) << "Auszeneck "
<< ' ' << '\n' << endl;
while (s*s > numeric_limits<double>::epsilon() / 2.0)
{
double u = ldexp(s, n);
double U = u / sqrt(1.0 - s*s);
cout<< ' ' << setw( 3) << setprecision( 0) << fixed << n
<< ' ' << setw(10) << setprecision( 0) << fixed << ldexp(1.0, n)
<< ' ' << setw(23) << setprecision(15) << scientific << s
<< ' ' << setw(18) << setprecision(15) << fixed << u
<< ' ' << setw(18) << setprecision(15) << fixed << U
<< endl;
// Hier wird die Parameter-Funktion aufgerufen.
// Sie berechnet s_(n+1) aus s_n in einer vom Aufrufer vorgegebenen Weise.
s = iteration(s);
n += 1;
}
}
double instabile_iteration(double s)
{
return sqrt((1.0 - sqrt(1.0 - s*s)) / 2.0);
}
double stabile_iteration(double s)
{
return s / sqrt(2.0 * (1.0 + sqrt(1.0 - s*s)));
}
int main()
{
cout << "Instabile Iteration\n" << endl;
// Die Funktion pi_approximation wird mit einer Funktion als Parameter aufgerufen.
// Der Vorteil dieser Herangehensweise ist offensichtlich: pi_approximation braucht nur
// einmal implementiert werden und man kann die Iteration einfach austauschen.
pi_approximation(instabile_iteration);
cout << '\n' << endl;
cout << "Stabile Iteration\n" << endl;
pi_approximation(stabile_iteration);
return 0;
}