Skip to content

Commit 9c677be

Browse files
authored
07-cond.md: Close Enough correction (#839)
`print(abs(a - b) < 0.1 * abs(b))` (and preceding Solution 2) leaves out the edge case where `a` is exactly 10% of `b`. This issue comes down to the interpretation of "within" 10%. Does a number within 10% of another number include the 10% value? Assume `b = 100` and `0.1 * b = 10`, then the value 10 is within 10% by definition since 10 is 10% of 100. Now set `a = 100 + 10 = 110` which is `b` plus 10% of `b`. Then `a - b = b + 0.1 * b - b = 0.1 * b`, which is within 10% of b. The correct test is `abs(a - b) <= 0.1 * abs(b)` to account for this edge case.
1 parent ebe849a commit 9c677be

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

_episodes/07-cond.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ freeing us from having to manually examine every plot for features we've seen be
313313
> > a = 5
314314
> > b = 5.1
315315
> >
316-
> > if abs(a - b) < 0.1 * abs(b):
316+
> > if abs(a - b) <= 0.1 * abs(b):
317317
> > print('True')
318318
> > else:
319319
> > print('False')
@@ -323,7 +323,7 @@ freeing us from having to manually examine every plot for features we've seen be
323323
>
324324
> > ## Solution 2
325325
> > ~~~
326-
> > print(abs(a - b) < 0.1 * abs(b))
326+
> > print(abs(a - b) <= 0.1 * abs(b))
327327
> > ~~~
328328
> > {: .language-python}
329329
> >

0 commit comments

Comments
 (0)