{"record":{"id":"7ba711a2c0647b75","repo":"TheAlgorithms/Python","slug":"maclaurin-sin-requires-either-an-int-or-float-fo","errorCode":null,"errorMessage":"maclaurin_sin() requires either an int or float for theta","messagePattern":"maclaurin_sin\\(\\) requires either an int or float for theta","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/maclaurin_series.py","lineNumber":47,"sourceCode":"    Traceback (most recent call last):\r\n        ...\r\n    ValueError: maclaurin_sin() requires either an int or float for theta\r\n    >>> maclaurin_sin(10, -30)\r\n    Traceback (most recent call last):\r\n        ...\r\n    ValueError: maclaurin_sin() requires a positive int for accuracy\r\n    >>> maclaurin_sin(10, 30.5)\r\n    Traceback (most recent call last):\r\n        ...\r\n    ValueError: maclaurin_sin() requires a positive int for accuracy\r\n    >>> maclaurin_sin(10, \"30\")\r\n    Traceback (most recent call last):\r\n        ...\r\n    ValueError: maclaurin_sin() requires a positive int for accuracy\r\n    \"\"\"\r\n\r\n    if not isinstance(theta, (int, float)):\r\n        raise ValueError(\"maclaurin_sin() requires either an int or float for theta\")\r\n\r\n    if not isinstance(accuracy, int) or accuracy <= 0:\r\n        raise ValueError(\"maclaurin_sin() requires a positive int for accuracy\")\r\n\r\n    theta = float(theta)\r\n    div = theta // (2 * pi)\r\n    theta -= 2 * div * pi\r\n    return sum(\r\n        (-1) ** r * theta ** (2 * r + 1) / factorial(2 * r + 1) for r in range(accuracy)\r\n    )\r\n\r\n\r\ndef maclaurin_cos(theta: float, accuracy: int = 30) -> float:\r\n    \"\"\"\r\n    Finds the maclaurin approximation of cos\r\n\r\n    :param theta: the angle to which cos is found\r\n    :param accuracy: the degree of accuracy wanted\r","sourceCodeStart":29,"sourceCodeEnd":65,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/maclaurin_series.py#L29-L65","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"# before\nmaclaurin_sin(raw_angle)  # raw_angle = '1.5'\n\n# after\nmaclaurin_sin(float(raw_angle))","handlingStrategy":"type-guard","validationCode":"theta = float(theta) if not isinstance(theta, (int, float)) or isinstance(theta, bool) else theta","typeGuard":"def is_valid_theta(x) -> bool:\n    return isinstance(x, (int, float)) and not isinstance(x, bool)","tryCatchPattern":null,"preventionTips":["Convert external string input to float immediately after parsing.","Keep angle values as floats throughout your pipeline instead of raw strings."],"tags":["math","type-validation","valueerror","trigonometry"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}