Calculating the value of e: Difference between revisions
Updated Processing implementation.
No edit summary |
(Updated Processing implementation.) |
||
Line 2,811:
=={{header|Processing}}==
Updated to show that computed value matches library value after 21 iterations.
<lang processing>void setup() {▼
double e = 0;
long factorial = 1;
for (int i = 0; i < iterations; i++) {
e += (double) (2 * i + 1) / factorial;
factorial *= (2 * i + 1) * (2 * i + 2);
}
println("After
println("Computed value: " + e);
println("Real value: " + Math.E);
println("Error: " + (e - Math.E));
▲}</lang>
iterations = 21;
for (int i = 11; i < iterations; i++) {
e += (double) (2 * i + 1) / factorial;
factorial *= (2 * i + 1) * (2 * i + 2);
}
println("After " + iterations + " iterations");
println("Computed value: " + e);
println("Real value: " + Math.E);
println("Error: " + (e - Math.E));
}
</lang>
{{out}}
<pre>
After 11 iterations
Computed value: 2.7182818284590455
Real value: 2.718281828459045
Error: 4.440892098500626E-16
After 21 iterations
Computed value: 2.718281828459045
Real value: 2.718281828459045
Error: 0.0
</pre>
|