Skip to content

Instantly share code, notes, and snippets.

@skaslev
Created December 16, 2012 20:42
Show Gist options
  • Select an option

  • Save skaslev/4312690 to your computer and use it in GitHub Desktop.

Select an option

Save skaslev/4312690 to your computer and use it in GitHub Desktop.
#include <algorithm>
#include <chrono>
#include <cmath>
#include <iostream>
#define STR_(X) (#X)
#define STR(X) (STR_(X))
#define ACTUAL 2.71828182845904523536028747135266249776
typedef long long int Int;
typedef long double Real;
typedef std::chrono::high_resolution_clock Clock;
int main() {
Real actual = ACTUAL;
Real e = 1;//0
Int fact = 1;
int i = 1;//0
auto t1 = Clock::now();
while (fact > 0) {
fact *= i;//std::max(1, i);
e += 1.0 / fact;
++i;
}
auto t2 = Clock::now();
std::cout.precision(50);
std::cout
<< i << " iterations in "
<< (t2-t1).count() << " ns\n"
<< e << " computed\n"
<< STR(ACTUAL) << " actual_str\n"
<< actual << " actual\n"
<< std::abs(actual - e) << " error\n";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment