TheAlgorithms/Python · error · ValueError
maclaurin_sin() requires either an int or float for theta
Error message
maclaurin_sin() requires either an int or float for theta
What it means
Raised by maclaurin_sin() in maths/maclaurin_series.py when the theta argument is not an int or float. The function computes a Maclaurin series approximation of sin(theta), which requires a real numeric angle in radians. Any string, list, None, or other type is rejected before the series is evaluated.
Source
Thrown at maths/maclaurin_series.py:47
Traceback (most recent call last):
...
ValueError: maclaurin_sin() requires either an int or float for theta
>>> 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
View on GitHub (pinned to f5988cc097)
Solutions
- Convert the argument before calling: maclaurin_sin(float(theta)).
- Validate input at the boundary of your program (e.g. after argparse or JSON parsing) and coerce to float.
- If you need Decimal/Fraction support, convert explicitly via float() since the check only accepts built-in int/float.
Example fix
# before maclaurin_sin(raw_angle) # raw_angle = '1.5' # after maclaurin_sin(float(raw_angle))
Defensive patterns
Strategy: type-guard
Validate before calling
theta = float(theta) if not isinstance(theta, (int, float)) or isinstance(theta, bool) else theta
Type guard
def is_valid_theta(x) -> bool:
return isinstance(x, (int, float)) and not isinstance(x, bool) Prevention
- Convert external string input to float immediately after parsing.
- Keep angle values as floats throughout your pipeline instead of raw strings.
When it happens
Trigger: Calling maclaurin_sin('1.5'), maclaurin_sin(None), maclaurin_sin([0.5]), or maclaurin_sin(Decimal('0.5')). Note that bool passes, since bool is a subclass of int in Python.
Common situations: Parsing angles from user input or config files as strings ('30' instead of 30), passing a numpy scalar or Decimal where a plain float is expected, or forwarding unvalidated data from JSON.
Related errors
- maclaurin_cos() requires either an int or float for theta
- maclaurin_sin() requires a positive int for accuracy
- maclaurin_cos() requires a positive int for accuracy
- Input must be an integer
- surface_area_cube() only accepts non-negative values
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/7ba711a2c0647b75.
Report an issue: GitHub.