TheAlgorithms/Python · error · ValueError
maclaurin_cos() requires a positive int for accuracy
Error message
maclaurin_cos() requires a positive int for accuracy
What it means
Raised by maclaurin_cos() in maths/maclaurin_series.py when accuracy is not an int or is <= 0. accuracy is the number of series terms summed via range(accuracy); floats such as 30.5 and strings such as '30' fail the `isinstance(accuracy, int) or accuracy <= 0` check and are rejected with this ValueError.
Source
Thrown at maths/maclaurin_series.py:102
>>> maclaurin_cos(10, -30)
Traceback (most recent call last):
...
ValueError: maclaurin_cos() requires a positive int for accuracy
>>> maclaurin_cos(10, 30.5)
Traceback (most recent call last):
...
ValueError: maclaurin_cos() requires a positive int for accuracy
>>> maclaurin_cos(10, "30")
Traceback (most recent call last):
...
ValueError: maclaurin_cos() requires a positive int for accuracy
"""
if not isinstance(theta, (int, float)):
raise ValueError("maclaurin_cos() requires either an int or float for theta")
if not isinstance(accuracy, int) or accuracy <= 0:
raise ValueError("maclaurin_cos() requires a positive int for accuracy")
theta = float(theta)
div = theta // (2 * pi)
theta -= 2 * div * pi
return sum((-1) ** r * theta ** (2 * r) / factorial(2 * r) for r in range(accuracy))
if __name__ == "__main__":
import doctest
doctest.testmod()
print(maclaurin_sin(10))
print(maclaurin_sin(-10))
print(maclaurin_sin(10, 15))
print(maclaurin_sin(-10, 15))
print(maclaurin_cos(5))
View on GitHub (pinned to f5988cc097)
Solutions
- Pass a positive int literal, e.g. maclaurin_cos(theta, 30).
- Round/cast floats: maclaurin_cos(theta, int(round(accuracy))).
- Declare CLI options with type=int and validate positivity before calling.
Example fix
# before maclaurin_cos(0.5, 30.5) # after maclaurin_cos(0.5, 30)
Defensive patterns
Strategy: validation
Validate before calling
accuracy = int(accuracy) if isinstance(accuracy, (int, float)) else 30
if accuracy <= 0:
raise ValueError('accuracy must be a positive int') Type guard
def is_valid_accuracy(a) -> bool:
return isinstance(a, int) and not isinstance(a, bool) and a > 0 Prevention
- Never pass a float term count; round and cast first.
- Declare config schemas with integer types for term/iteration counts.
When it happens
Trigger: maclaurin_cos(10, 30.5), maclaurin_cos(10, '30'), maclaurin_cos(10, 0), or maclaurin_cos(10, 2.0) (a float even with integral value).
Common situations: Accuracy computed as a float (e.g. n_terms = total/10), read from JSON where it parsed as float, or supplied via CLI without type=int.
Related errors
- maclaurin_sin() 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/4e9762df67168b27.
Report an issue: GitHub.