On Thursday, 18 June 2015 at 10:50:09 UTC, Andrea Fontana wrote:
On Thursday, 18 June 2015 at 10:46:18 UTC, Ilya Yaroshenko
wrote:
On Thursday, 18 June 2015 at 10:27:58 UTC, Russel Winder wrote:
On a given machine, the code:
double sequential_loop(const int n, const double delta) {
auto sum = 0.0;
foreach (immutable i; 1 .. n + 1) {
immutable x = (i - 0.5) * delta;
sum += 1.0 / (1.0 + x * x);
}
return 4.0 * delta * sum;
}
What about this:
double sequential_alternative(const int n, const double delta) {
return 4.0 * delta * iota(1, n+1).map!(x =>
(x-0.5)*delta).map!(x => 1.0/(1.0 + x*x)).sum;
}
?
This is not equivalent, because sum uses another (more precise)
algorithms to do summation. It should work a little bit slower.