BAEL-6226 Calculate Pi Java Program (#13693)
* BAEL-6226 Calculate Pi Java Program * BAEL-6226 Calculate Pi Java Program
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
package com.baeldung.pi;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.Random;
|
||||
import org.junit.Test;
|
||||
|
||||
public class PiProgramUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenPiCalculator_whenCalculatePiWithTenThousandPoints_thenEstimatedPiIsWithinTolerance() {
|
||||
int totalPoints = 10000;
|
||||
int insideCircle = 0;
|
||||
|
||||
Random random = new Random();
|
||||
|
||||
for (long i = 0; i < totalPoints; i++) {
|
||||
double x = random.nextDouble() * 2 - 1;
|
||||
double y = random.nextDouble() * 2 - 1;
|
||||
|
||||
if (x * x + y * y <= 1) {
|
||||
insideCircle++;
|
||||
}
|
||||
|
||||
}
|
||||
double pi = 4.0 * insideCircle / totalPoints;
|
||||
|
||||
assertEquals(Math.PI, pi, 0.01);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user