Skip to content

Commit 7733bef

Browse files
committed
Fix test_estimate_pi, duplicate into files for later lessons
1 parent b51f2ed commit 7733bef

File tree

8 files changed

+70
-7
lines changed

8 files changed

+70
-7
lines changed

episodes/06-floating-point-data.Rmd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,8 @@ if either of the conditions are satisfied.
151151
:::::::::::::::::::::::: solution
152152

153153
```python
154-
import random
155154
import math
155+
import random
156156

157157
from estimate_pi import estimate_pi
158158

@@ -162,7 +162,7 @@ def test_estimate_pi():
162162
actual = estimate_pi(iterations=10000)
163163
atol = 1e-2
164164
rtol = 5e-3
165-
assert math.isclose(actual, expected, abs_tol=atol rel_tol=rtol)
165+
assert math.isclose(actual, expected, abs_tol=atol, rel_tol=rtol)
166166
```
167167

168168
:::::::::::::::::::::::::::::::::
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1+
import math
12
import random
2-
from math import fabs
33

44
from estimate_pi import estimate_pi
55

66
def test_estimate_pi():
77
random.seed(0)
88
expected = 3.141592654
99
actual = estimate_pi(iterations=10000)
10-
# Test absolute tolerance
1110
atol = 1e-2
12-
assert fabs(actual - expected) < atol
13-
# Test relative tolerance
1411
rtol = 5e-3
15-
assert fabs((actual - expected) / expected) < rtol
12+
assert math.isclose(actual, expected, abs_tol=atol, rel_tol=rtol)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import random
2+
3+
def estimate_pi(iterations):
4+
num_inside = 0
5+
for _ in range(iterations):
6+
x = random.random()
7+
y = random.random()
8+
if x**2 + y**2 < 1:
9+
num_inside += 1
10+
return 4 * num_inside / iterations
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import math
2+
import random
3+
4+
from estimate_pi import estimate_pi
5+
6+
def test_estimate_pi():
7+
random.seed(0)
8+
expected = 3.141592654
9+
actual = estimate_pi(iterations=10000)
10+
atol = 1e-2
11+
rtol = 5e-3
12+
assert math.isclose(actual, expected, abs_tol=atol, rel_tol=rtol)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import random
2+
3+
def estimate_pi(iterations):
4+
num_inside = 0
5+
for _ in range(iterations):
6+
x = random.random()
7+
y = random.random()
8+
if x**2 + y**2 < 1:
9+
num_inside += 1
10+
return 4 * num_inside / iterations
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import math
2+
import random
3+
4+
from estimate_pi import estimate_pi
5+
6+
def test_estimate_pi():
7+
random.seed(0)
8+
expected = 3.141592654
9+
actual = estimate_pi(iterations=10000)
10+
atol = 1e-2
11+
rtol = 5e-3
12+
assert math.isclose(actual, expected, abs_tol=atol, rel_tol=rtol)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import random
2+
3+
def estimate_pi(iterations):
4+
num_inside = 0
5+
for _ in range(iterations):
6+
x = random.random()
7+
y = random.random()
8+
if x**2 + y**2 < 1:
9+
num_inside += 1
10+
return 4 * num_inside / iterations
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import math
2+
import random
3+
4+
from estimate_pi import estimate_pi
5+
6+
def test_estimate_pi():
7+
random.seed(0)
8+
expected = 3.141592654
9+
actual = estimate_pi(iterations=10000)
10+
atol = 1e-2
11+
rtol = 5e-3
12+
assert math.isclose(actual, expected, abs_tol=atol, rel_tol=rtol)

0 commit comments

Comments
 (0)