Skip to content

Commit

Permalink
Let prob_cleanup raise exception if inaccurate.
Browse files Browse the repository at this point in the history
Fixes #203.
  • Loading branch information
jgosmann committed Aug 27, 2018
1 parent ed4a3ae commit 2da500a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
6 changes: 5 additions & 1 deletion nengo_spa/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@ def prob_cleanup(similarity, dimensions, vocab_size):
Requires SciPy.
"""
return CosineSimilarity(dimensions).cdf(similarity) ** vocab_size
p = CosineSimilarity(dimensions).cdf(similarity)
if similarity < 1. and p == 1.:
raise ArithmeticError(
"Insufficient floating point precision to compute value.")
return p ** vocab_size
5 changes: 5 additions & 0 deletions nengo_spa/tests/test_math.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import numpy as np
import pytest

from nengo_spa.math import prob_cleanup
Expand All @@ -13,3 +14,7 @@ def test_prob_cleanup(rng):
assert 0.999 > prob_cleanup(0.4, 128, 1000) > 0.997
assert 0.99 > prob_cleanup(0.4, 128, 10000) > 0.97
assert 0.9 > prob_cleanup(0.4, 128, 100000) > 0.8
assert prob_cleanup(0.4, 128, np.inf) == 0.

with pytest.raises(ArithmeticError):
prob_cleanup(0.2, 2025, np.inf)

0 comments on commit 2da500a

Please sign in to comment.