Repository navigation
Expand file tree
/
Copy pathphi.cpp
More file actions
47 lines (36 loc) · 1.35 KB
/
Copy pathphi.cpp
File metadata and controls
47 lines (36 loc) · 1.35 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
#include <iostream>
#include <string>
#include <stdexcept>
#include <chrono> // Für die Zeitmessung
#include <boost/multiprecision/gmp.hpp>
namespace mp = boost::multiprecision;
using high_prec_float = mp::number<mp::gmp_float<0>>;
void print_usage(const char* prog_name) {
std::cerr << "Benutzung: " << prog_name << " <Anzahl_der_Nachkommastellen>" << std::endl;
}
int main(int argc, char* argv[]) {
if (argc != 2) {
print_usage(argv[0]);
return 1;
}
int digits = 0;
try {
digits = std::stoi(argv[1]);
if (digits <= 0) throw std::invalid_argument("Anzahl muss positiv sein.");
} catch (const std::exception& e) {
std::cerr << "Fehler: Ungültige Anzahl an Nachkommastellen. Bitte eine positive Zahl angeben." << std::endl;
print_usage(argv[0]);
return 1;
}
high_prec_float::default_precision(digits * 3.32193 + 16);
std::cout.precision(digits + 1);
auto start = std::chrono::high_resolution_clock::now();
high_prec_float one = 1;
high_prec_float five = 5;
high_prec_float phi = (one + mp::sqrt(five)) / 2;
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> duration = end - start;
std::cout << phi << std::endl;
std::cerr << "\nBerechnung dauerte " << duration.count() << " Sekunden." << std::endl;
return 0;
}