Fixed problem in Pi calculation algorithm
This commit is contained in:
parent
06c134ca91
commit
2770f47f27
6 changed files with 17 additions and 19 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
------------------------------------
|
||||
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue