Created
December 16, 2012 22:18
-
-
Save skaslev/4313664 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 <algorithm> | |
#include <cmath> | |
#include <iomanip> | |
#include <iostream> | |
using namespace std; | |
#define STR_(X) (#X) | |
#define STR(X) (STR_(X)) | |
#define ACTUAL 2.71828182845904523536028747135266249776 | |
template<class Int, class Real=long double> | |
struct E { | |
void operator()() const { | |
Real actual = ACTUAL; | |
Real e = 1; | |
Int fact = 1; | |
int i = 1; | |
while (fact > 0) { | |
fact *= i; | |
e += 1.0 / fact; | |
++i; | |
} | |
Real error = abs(actual - e); | |
Real log_error = -(1/log(10) * log(error)); | |
Real sqrt_log_error = sqrt(log_error); | |
const int P = 40; | |
cout | |
<< setprecision(P) | |
//<< setw(P) << i << "\titerations in " | |
//<< (t2-t1).count() << "ns\n" | |
//<< e << "\tcomputed\n" | |
//<< STR(ACTUAL) << "\tactual_str\n" | |
//<< actual << "\tactual\n" | |
//<< setprecision(P-4) << error << "\terror\n" | |
//<< setprecision(P) << log_error << "\t-log10(error)\n" | |
//<< sqrt_log_error << "\tsqrt(-log10(error)\n" | |
<< setw(P) << round(sqrt_log_error) << "\tround(sqrt(-log10(error)))\n" | |
//<< setw(P) << sizeof(Int) << "\tsizeof(Int)\n" | |
<< "\n"; | |
} | |
}; | |
int main() { | |
E<char>()(); | |
E<short>()(); | |
E<int>()(); | |
E<long>()(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment