Created
March 5, 2017 00:57
-
-
Save ryancerium/76b95e7ce4a8520ca8b06e193e84099c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cmath> | |
#include <cstdio> | |
#include <numeric> | |
#include <random> | |
class point { | |
double x, y; | |
public: | |
constexpr point(const double x, const double y) : x{x}, y{y} {}; | |
constexpr double inCircle(const double radius) const { | |
return (x * x + y * y) < (radius * radius); | |
}; | |
}; | |
point make_point(std::uniform_real_distribution<double>& urd) { | |
static std::random_device rand; | |
return {urd(rand), urd(rand)}; | |
}; | |
class pi_maker { | |
double mRadius; | |
uint32_t mRemainingPoints; | |
double mInside; | |
mutable std::uniform_real_distribution<double> mUrd; | |
double mValue; | |
double generate_point() const { | |
return make_point(mUrd).inCircle(mRadius) ? mInside : 0.0; | |
} | |
public: | |
pi_maker() : pi_maker{0.0, 0} {}; | |
pi_maker(const double radius, const uint32_t nPoints) | |
: mRadius{radius}, | |
mRemainingPoints{nPoints}, | |
mInside{nPoints > 0 ? (4.0 / nPoints) : 1.0}, | |
mUrd{0, radius}, | |
mValue{generate_point()} {}; | |
double operator*() const { return mValue; }; | |
pi_maker& operator++() { | |
--mRemainingPoints; | |
mValue = generate_point(); | |
return *this; | |
}; | |
bool equal(const pi_maker& rhs) const { | |
return (mRadius > 0.0 && rhs.mRadius > 0.0) | |
? (mRadius == rhs.mRadius && mInside == rhs.mInside && | |
mRemainingPoints == rhs.mRemainingPoints) | |
: (mRemainingPoints == rhs.mRemainingPoints); | |
}; | |
}; | |
bool operator!=(const pi_maker& lhs, const pi_maker& rhs) { | |
return !lhs.equal(rhs); | |
} | |
constexpr uint32_t pow_10(const uint32_t exp) { | |
return exp == 0 ? 1 : 10 * pow_10(exp - 1); | |
} | |
int main(int argc, char* argv[]) { | |
constexpr auto kPi = 3.14159265359; | |
for (uint32_t n = 0; n <= 9; ++n) { | |
for (uint32_t m = 0; m <= 7; ++m) { | |
const auto pi = | |
std::accumulate(pi_maker(pow_10(n), pow_10(m)), pi_maker(), 0.0); | |
printf("%1.8f ", std::abs(kPi - pi)); | |
fflush(stdout); | |
} | |
printf("\n"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment