Fixed problem in Pi calculation algorithm

This commit is contained in:
Jonas Bonér 2011-04-23 11:55:54 +02:00
parent 06c134ca91
commit 2770f47f27
6 changed files with 17 additions and 19 deletions

View file

@ -36,7 +36,7 @@ object Pi extends App {
def calculatePiFor(start: Int, nrOfElements: Int): Double = {
var acc = 0.0
for (i <- start until (start + nrOfElements))
acc += 4 * math.pow(-1, i) / (2 * i + 1)
acc += 4.0 * math.pow(-1, i) / (2 * i + 1)
acc
}
//#calculate-pi

View file

@ -282,7 +282,7 @@ The only thing missing in our ``Worker`` actor is the implementation on the ``ca
private double calculatePiFor(int start, int nrOfElements) {
double acc = 0.0;
for (int i = start * nrOfElements; i <= ((start + 1) * nrOfElements - 1); i++) {
acc += 4 * (1 - (i % 2) * 2) / (2 * i + 1);
acc += 4.0 * (1 - (i % 2) * 2) / (2 * i + 1);
}
return acc;
}
@ -550,7 +550,7 @@ Before we package it up and run it, let's take a look at the full code now, with
private double calculatePiFor(int start, int nrOfElements) {
double acc = 0.0;
for (int i = start * nrOfElements; i <= ((start + 1) * nrOfElements - 1); i++) {
acc += 4 * (1 - (i % 2) * 2) / (2 * i + 1);
acc += 4.0 * (1 - (i % 2) * 2) / (2 * i + 1);
}
return acc;
}

View file

@ -245,7 +245,7 @@ The only thing missing in our ``Worker`` actor is the implementation on the ``ca
def calculatePiFor(start: Int, nrOfElements: Int): Double = {
var acc = 0.0
for (i <- start until (start + nrOfElements))
acc += 4 * (1 - (i % 2) * 2) / (2 * i + 1)
acc += 4.0 * (1 - (i % 2) * 2) / (2 * i + 1)
acc
}

View file

@ -266,8 +266,6 @@ But before we package it up and run it, let's take a look at the full code now,
.. includecode:: examples/Pi.scala
Run it as a command line application
------------------------------------

View file

@ -87,7 +87,7 @@ public class Pi {
private double calculatePiFor(int start, int nrOfElements) {
double acc = 0.0;
for (int i = start * nrOfElements; i <= ((start + 1) * nrOfElements - 1); i++) {
acc += 4 * (1 - (i % 2) * 2) / (2 * i + 1);
acc += 4.0 * (1 - (i % 2) * 2) / (2 * i + 1);
}
return acc;
}

View file

@ -59,7 +59,7 @@ object Pi extends App {
def calculatePiFor(start: Int, nrOfElements: Int): Double = {
var acc = 0.0
for (i <- start until (start + nrOfElements))
acc += 4 * (1 - (i % 2) * 2) / (2 * i + 1)
acc += 4.0 * (1 - (i % 2) * 2) / (2 * i + 1)
acc
}