Skip to content

Commit 67f564e

Browse files
philippeitisJoseph Weston
authored andcommitted
Add note about fast_norm overflow.
It appears that for vectors with large values, it is possible to induce an integer overflow in dot(x, x).
1 parent 571850c commit 67f564e

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

adaptive/learner/triangulation.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,20 @@
1111

1212

1313
def fast_norm(v):
14+
""" Manually take the vector norm for len 2, 3 vectors. Defaults to a square root of the dot product
15+
for larger vectors.
16+
17+
Note that for large vectors, it is possible for integer overflow to occur.
18+
For instance:
19+
vec = [49024, 59454, 12599, -63721, 18517, 27961]
20+
dot(vec, vec) = -1602973744
21+
22+
"""
23+
len_v = len(v)
1424
# notice this method can be even more optimised
15-
if len(v) == 2:
25+
if len_v == 2:
1626
return sqrt(v[0] * v[0] + v[1] * v[1])
17-
if len(v) == 3:
27+
if len_v == 3:
1828
return sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2])
1929
return sqrt(dot(v, v))
2030

0 commit comments

Comments
 (0)