Skip to content

Commit e752a58

Browse files
authored
Fix typos in nth_root.py documentation and code
1 parent 3535f7a commit e752a58

1 file changed

Lines changed: 27 additions & 27 deletions

File tree

‎maths/numerical_analysis/nth_root.py‎

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
Approximate the nth root of a real number using the Newton's Method.
2+
Approximate the nth root of a real number using Newton's Method.
33
44
The nth root of a real number R can be computed with Newton's method,
55
which starts with an initial guess x_0 and then iterates using the
@@ -21,7 +21,7 @@
2121
USA. Addison-Wesley Publishing Company.
2222
"""
2323

24-
from math import pow
24+
from math import pow # noqa: A004
2525

2626

2727
def nth_root(radicand: float, index: int, tolerance: float = 0.0001) -> float:
@@ -34,15 +34,15 @@ def nth_root(radicand: float, index: int, tolerance: float = 0.0001) -> float:
3434
tolerance: positive real number that establishes the stopping criterion
3535
3636
Returns:
37-
new_aproximation: approximation of the nth root of the radicand for the
37+
new_approximation: approximation of the nth root of the radicand for the
3838
given index
3939
4040
Raises:
41-
TypeError: radicand is not real number
42-
TypeError: index is not integer
43-
ValueError: index is not positive integer
44-
TypeError: tolerance is not real number
45-
ValueError: tolerance is not positive real number
41+
TypeError: radicand is not a real number
42+
TypeError: index is not an integer
43+
ValueError: index is not a positive integer
44+
TypeError: tolerance is not a real number
45+
ValueError: tolerance is not a positive real number
4646
ValueError: math domain error
4747
4848
>>> round(nth_root(9, 2),1)
@@ -75,51 +75,51 @@ def nth_root(radicand: float, index: int, tolerance: float = 0.0001) -> float:
7575
>>> nth_root('invalid input', 3, 0.0001)
7676
Traceback (most recent call last):
7777
...
78-
TypeError: radicand must be real number, not str
78+
TypeError: radicand must be a real number, not a str
7979
8080
>>> nth_root(4, 0.5, 0.0001)
8181
Traceback (most recent call last):
8282
...
83-
TypeError: index must be integer, not float
83+
TypeError: index must be an integer, not a float
8484
8585
>>> nth_root(16, -4, 0.001)
8686
Traceback (most recent call last):
8787
...
88-
ValueError: index must be positive integer, -4 <= 0
88+
ValueError: index must be a positive integer, -4 <= 0
8989
9090
>>> nth_root(4, 2, '0.000001')
9191
Traceback (most recent call last):
9292
...
93-
TypeError: tolerance must be real number, not str
93+
TypeError: tolerance must be a real number, not str
9494
9595
>>> nth_root(9, 2, -0.01)
9696
Traceback (most recent call last):
9797
...
98-
ValueError: tolerance must be positive real number, -0.01 <= 0
98+
ValueError: tolerance must be a positive real number, -0.01 <= 0
9999
100100
>>> nth_root(-256, 4, 0.0001)
101101
Traceback (most recent call last):
102102
...
103103
ValueError: math domain error, radicand must be nonnegative for even index
104104
"""
105105
if not isinstance(radicand, (int, float)):
106-
error_message = f"radicand must be real number, not {type(radicand).__name__}"
106+
error_message = f"radicand must be a real number, not a {type(radicand).__name__}"
107107
raise TypeError(error_message)
108108

109109
if not isinstance(index, int):
110-
error_message = f"index must be integer, not {type(index).__name__}"
110+
error_message = f"index must be an integer, not a {type(index).__name__}"
111111
raise TypeError(error_message)
112112

113113
if index <= 0:
114-
error_message = f"index must be positive integer, {index} <= 0"
114+
error_message = f"index must be a positive integer, {index} <= 0"
115115
raise ValueError(error_message)
116116

117117
if not isinstance(tolerance, (int, float)):
118-
error_message = f"tolerance must be real number, not {type(tolerance).__name__}"
118+
error_message = f"tolerance must be a real number, not {type(tolerance).__name__}"
119119
raise TypeError(error_message)
120120

121121
if tolerance <= 0:
122-
error_message = f"tolerance must be positive real number, {tolerance} <= 0"
122+
error_message = f"tolerance must be a positive real number, {tolerance} <= 0"
123123
raise ValueError(error_message)
124124

125125
if radicand < 0 and index % 2 == 0:
@@ -130,19 +130,19 @@ def nth_root(radicand: float, index: int, tolerance: float = 0.0001) -> float:
130130
return 0.0
131131

132132
# Set initial guess
133-
new_aproximation = radicand
134-
# Set old_aproximation to enter the loop
135-
old_aproximation = new_aproximation + tolerance + 0.1
133+
new_approximation = radicand
134+
# Set old_approximation to enter the loop
135+
old_approximation = new_approximation + tolerance + 0.1
136136

137137
# Iterate as long as the stop criterion is not satisfied
138-
while tolerance <= abs(old_aproximation - new_aproximation):
139-
old_aproximation = new_aproximation
138+
while tolerance <= abs(old_approximation - new_approximation):
139+
old_approximation = new_approximation
140140
# Compute new_approximation with the recurrence relation described above
141-
first_summand = (index - 1) / index * old_aproximation
142-
second_summand = radicand / (index * pow(old_aproximation, index - 1))
143-
new_aproximation = first_summand + second_summand
141+
first_summand = (index - 1) / index * old_approximation
142+
second_summand = radicand / (index * pow(old_approximation, index - 1))
143+
new_approximation = first_summand + second_summand
144144

145-
return new_aproximation
145+
return new_approximation
146146

147147

148148
if __name__ == "__main__":

0 commit comments

Comments
 (0)