-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblatt04.cpp
More file actions
150 lines (126 loc) · 4.17 KB
/
blatt04.cpp
File metadata and controls
150 lines (126 loc) · 4.17 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <iostream>
#include <iomanip>
#include <limits>
#include <cmath>
#include <cstring>
#include <cerrno>
using namespace std;
/// Konstante, die die Maschinengenauigkeit beschreibt.
double const EPS = numeric_limits<double>::epsilon() / 2;
/// Die Funktion liest eine Zahl ein und gibt tabellarisch
/// die Annäherung an den Loarithmus derselben aus.
int main_a()
{
// a einlesen.
double a;
cout << "a: " << flush;
cin >> a;
// Wertebereich überprüfen.
if (not (a > 0))
{
cout << "a > 0 erwartet!" << endl;
return 0;
}
// Startwerte festlegen.
double x = sqrt(a), // x = root(a, 2^n) = sqrt(a), da n = 0.
l1 = a - 1.0, // Logarithmus 1. Formel
l2 = a - 1.0; // Logarithmus 2. Formel
double y = x - 1.0; // Praktische Abkürzung.
// Präzises Ausgabeformat festlegen; wirt daueraft.
cout << setprecision(15) << fixed;
const int width = 25;
// Überschriften
cout << setw(width) << "Naeherung1"
<< setw(width) << "Naeherung2"
<< setw(width) << "Std-Log"
<< endl << endl;
// für alle n ist x = (2^n)-te Wurzel von a.
// In jedem Schritt wird n erhöht und x und y erhalten ihre entsprechenden
// Werte für den nächsten Durchlauf.
for (unsigned n = 1; abs(y) > EPS; ++n, x = sqrt(x), y = x - 1.0)
{
l1 = ldexp(y, n);
l2 *= 2.0 / (1.0 + x);
cout << setw(width) << l1
<< setw(width) << l2
<< setw(width) << log(a) << '\n';
}
// Ausgabe der Ergebnisse.
cout << "Naeherung1: " << l1 << '\n'
<< "Naeherung2: " << l2 << '\n'
<< "log(a) = " << log(a) << endl;
return 0;
}
/// Die Funktion versucht den Logarithmus des Zahl a zu bestimmen.
/// Für Werte, die das Resultat
double mylog(double a)
{
if (a == numeric_limits<double>::infinity())
{
errno = ERANGE;
return a; // log(unendlich) = unendlich.
}
else if (a > 0.0)
{
double x = a--; // Setze x = a - 1, dann verringere a um 1.
// Solange die Bedingung gilt:
// - Ziehe die Wurzel aus x mittels x = sqrt(x).
// - Multipliziere a mit 2 und teile durch x (nach dem Ziehen der Wurze).
while (abs(x - 1.0) > EPS) a *= 2.0 / (1.0 + (x = sqrt(x)));
return a;
}
else if (a == 0.0) // besser wäre a < eps (wobei eps klein, z. B. M.-Genauigkeit); (*)
{
errno = ERANGE; // ERANGE = Ergebniswert außerhalb des Wertebreichs.
return -numeric_limits<double>::infinity();
}
else // a ist nicht im Definitionsbereich (auch nicht im weitesten Sinne, d. h. mit 0)
{
errno = EDOM; // EDOM = Paramter außerhalb des Definitionsbereichs.
return numeric_limits<double>::quiet_NaN();
}
// (*) abs nicht notwendig, da a >= 0 aus obiger Bedingung folgt.
// Es gibt Zahlen, die so klein sind, dass ihr Inverses nicht darstellbar ist.
}
/// Liest eine Zahl ein und gibt den mit mylog approximierten Wert aus.
/// Es findet eine pauschale a-posteriori-Fehlerbehandlung statt.
int main_b()
{
// a einlesen.
double a;
cout << "a: " << flush;
cin >> a;
errno = 0; // Alte Fehler ignoreren.
double l = mylog(a); // Berechnung durchführen.
if (errno) // Auf Fehler prüfen.
{
// Fehlermedlung ausgeben.
cout << "Fehler aufgetreten. Details: " << strerror(errno) << endl;
}
else
{
// Ergebnis präsentieren.
cout << setprecision(17) << fixed;
cout << "Näherung: " << l << endl;
cout << "log(a) = " << log(a) << endl;
}
return 0;
}
/// Leitet den Benutzer zu einem der Aufgabenteile.
int main()
{
for(;;)
{
cout << "Teilaufgabe: " << flush;
char aufg;
cin >> aufg;
// Entsprechend Teilaufgabe in Unterfunktion weitermachen.
switch (aufg)
{
case 'a': cout << endl << endl; return main_a();
case 'b': cout << endl << endl; return main_b();
default:
cout << "Es gibt nur Aufgabenteil a und b." << endl;
}
}
}