#include <iostream>
#include <random>
#include <cmath>
#include <limits>
#include <ctime>
const std::string currentDateTime() {
time_t now = time(0);
struct tm tstruct;
char buf[80];
tstruct = *localtime(&now);
strftime(buf, sizeof(buf), "%Y-%m-%d.%X", &tstruct);
return buf;
}
int main()
{
typedef std::numeric_limits< double > dbl;
std::default_random_engine generator;
std::normal_distribution<double> distribution(0.0, 1.0);
std::cout.precision(dbl::digits10);
const long long N=1390737600000;
long long i, in;
double x, y, xz1, yz1, diff;
xz1 = 0.0;
yz1 = 0.0;
diff = 0.0;
x = distribution(generator);
y = 0.5*yz1 + x - 0.5*xz1;
diff = std::abs(x-y);
in = 1L;
for (i=0; i<N; i++){
x = distribution(generator);
y = 0.5*yz1 + x - 0.5*xz1;
diff = diff > std::abs(x-y) ? diff : std::abs(x-y);
xz1 = x;
yz1 = y;
if (i == in) {
std::cout << "[" << currentDateTime() << "] -- " << std::scientific << diff << " [" << in << "] " << std::endl;
in *= 10;
}
}
std::cout.precision(dbl::digits10);
std::cout << "Maximum difference is: " << std::scientific << diff << std::endl;
return 0;
}
/* Output:
[2015-02-25.15:00:56] -- 0.000000000000000e+00 [1]
[2015-02-25.15:00:56] -- 1.387778780781446e-17 [10]
[2015-02-25.15:00:56] -- 4.440892098500626e-16 [100]
[2015-02-25.15:00:56] -- 4.440892098500626e-16 [1000]
[2015-02-25.15:00:56] -- 4.440892098500626e-16 [10000]
[2015-02-25.15:00:56] -- 4.440892098500626e-16 [100000]
[2015-02-25.15:00:56] -- 8.881784197001252e-16 [1000000]
[2015-02-25.15:00:58] -- 8.881784197001252e-16 [10000000]
[2015-02-25.15:01:18] -- 8.881784197001252e-16 [100000000]
[2015-02-25.15:04:36] -- 8.881784197001252e-16 [1000000000]
[2015-02-25.15:37:35] -- 8.881784197001252e-16 [10000000000]
[2015-02-25.21:13:03] -- 8.881784197001252e-16 [100000000000]
[2015-02-28.05:09:45] -- 8.881784197001252e-16 [1000000000000]
Maximum difference is: 8.881784197001252e-16
*/