BAEL-6226 Calculate Pi Java Program (#13693)

* BAEL-6226 Calculate Pi Java Program

* BAEL-6226 Calculate Pi Java Program
This commit is contained in:
Michael Olayemi
2023-04-04 03:06:12 +01:00
committed by GitHub
parent d186dc6997
commit 3cdc10d6bb
4 changed files with 63 additions and 0 deletions

View File

@@ -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);
}
}