-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpi_approximation.cpp
31 lines (26 loc) · 1.07 KB
/
pi_approximation.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
#include <iostream>
#include <cmath>
#include <limits>
int main() {
const double PI = 3.14159265358979323846;
double bestApproximation = 0.0;
int bestNumerator = 0;
int bestDenominator = 0;
double smallestDifference = std::numeric_limits<double>::max();
for (int numerator = 1; numerator < 256; ++numerator) {
for (int denominator = 1; denominator < 256; ++denominator) {
double approximation = static_cast<double>(numerator) / denominator;
double difference = std::abs(PI - approximation);
if (difference < smallestDifference) {
smallestDifference = difference;
bestApproximation = approximation;
bestNumerator = numerator;
bestDenominator = denominator;
}
}
}
std::cout << "Best approximation of PI with integers smaller than 30: "
<< bestNumerator << "/" << bestDenominator << " = " << bestApproximation << std::endl;
std::cout << "Difference from actual PI: " << smallestDifference << std::endl;
return 0;
}