TheAlgorithms/Python · error · ValueError
maclaurin_sin() requires a positive int for accuracy
Error message
maclaurin_sin() requires a positive int for accuracy
What it means
Raised by maclaurin_sin() in maths/maclaurin_series.py when the accuracy argument is not an int or is <= 0. accuracy controls how many terms of the Maclaurin series are summed (range(accuracy)), so it must be a positive Python int. Floats like 30.5 and numeric strings are rejected even if they 'look' numeric.
Source
Thrown at maths/maclaurin_series.py:50
>>> maclaurin_sin(10, -30)
Traceback (most recent call last):
...
ValueError: maclaurin_sin() requires a positive int for accuracy
>>> maclaurin_sin(10, 30.5)
Traceback (most recent call last):
...
ValueError: maclaurin_sin() requires a positive int for accuracy
>>> maclaurin_sin(10, "30")
Traceback (most recent call last):
...
ValueError: maclaurin_sin() requires a positive int for accuracy
"""
if not isinstance(theta, (int, float)):
raise ValueError("maclaurin_sin() requires either an int or float for theta")
if not isinstance(accuracy, int) or accuracy <= 0:
raise ValueError("maclaurin_sin() requires a positive int for accuracy")
theta = float(theta)
div = theta // (2 * pi)
theta -= 2 * div * pi
return sum(
(-1) ** r * theta ** (2 * r + 1) / factorial(2 * r + 1) for r in range(accuracy)
)
def maclaurin_cos(theta: float, accuracy: int = 30) -> float:
"""
Finds the maclaurin approximation of cos
:param theta: the angle to which cos is found
:param accuracy: the degree of accuracy wanted
:return: the value of cosine in radians
View on GitHub (pinned to f5988cc097)
Solutions
- Pass a literal positive int: maclaurin_sin(theta, 30).
- Coerce explicitly: maclaurin_sin(theta, int(accuracy)) when accuracy arrives as a float or numeric string.
- With argparse, declare the option with type=int so you never receive a string.
Example fix
# before maclaurin_sin(1.2, accuracy='30') # after maclaurin_sin(1.2, accuracy=30)
Defensive patterns
Strategy: validation
Validate before calling
if not isinstance(accuracy, int) or isinstance(accuracy, bool) or accuracy <= 0:
accuracy = 30 # or raise your own error with context Type guard
def is_valid_accuracy(a) -> bool:
return isinstance(a, int) and not isinstance(a, bool) and a > 0 Prevention
- Use argparse type=int for numeric CLI options.
- Cast computed term counts with int() before passing.
When it happens
Trigger: maclaurin_sin(10, 30.5), maclaurin_sin(10, '30'), maclaurin_sin(10, 0), or maclaurin_sin(10, -3). Note maclaurin_sin(10, True) passes because bool is a subclass of int and True == 1.
Common situations: Reading accuracy from CLI args (always strings with argparse unless type=int is set), from JSON floats, or dividing/computing the term count and passing a float result.
Related errors
- maclaurin_cos() requires a positive int for accuracy
- maclaurin_sin() requires either an int or float for theta
- maclaurin_cos() requires either an int or float for theta
- The order must be greater than or equal to 1.
- The final value of x must be greater than the initial values
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/466d44743c320d40.
Report an issue: GitHub.