Skip to content

Commit 2a8fda4

Browse files
committed
Fix: Added doctests that break the code, changed var name
1 parent dc74572 commit 2a8fda4

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

Diff for: maths/fast_inverse_sqrt.py

+19-7
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,41 @@
66
import struct
77

88

9-
def fastInvSqrt(x: float) -> float:
9+
def fast_inverse_sqrt(number: float) -> float:
1010
"""
1111
Calculate the fast inverse square root of a number.
1212
1313
This function computes the fast inverse square root of a floating-point number
1414
using the famous Quake III algorithm, originally developed by id Software.
1515
16-
:param float x: The input number for which to calculate the inverse square root.
16+
:param float number: The input number for which to calculate the inverse square root.
1717
:return float: The fast inverse square root of the input number.
1818
1919
Example:
20-
>>> fastInvSqrt(10)
20+
>>> fast_inverse_sqrt(10)
2121
0.3156857923527257
2222
23-
>>> fastInvSqrt(4)
23+
>>> fast_inverse_sqrt(4)
2424
0.49915357479239103
25+
26+
>>> fast_inverse_sqrt(0)
27+
Traceback (most recent call last):
28+
...
29+
ValueError: Input must be a positive number.
30+
31+
>>> fast_inverse_sqrt(-1)
32+
Traceback (most recent call last):
33+
...
34+
ValueError: Input must be a positive number.
2535
"""
26-
i = struct.unpack('>i', struct.pack('>f', x))[0]
36+
if number<=0:
37+
raise ValueError("Input must be a positive number.")
38+
i = struct.unpack('>i', struct.pack('>f', number))[0]
2739
i = 0x5f3759df - (i >> 1)
2840
y = struct.unpack('>f', struct.pack('>i', i))[0]
29-
return y * (1.5 - 0.5 * x * y * y)
41+
return y * (1.5 - 0.5 * number * y * y)
3042

3143
if __name__ == "__main__":
3244
import doctest
3345

34-
doctest.testmod(verbose=True)
46+
doctest.testmod(verbose=True)

0 commit comments

Comments
 (0)