{"record":{"id":"207fe72caaa9b935","repo":"TheAlgorithms/Python","slug":"maclaurin-cos-requires-either-an-int-or-float-fo","errorCode":null,"errorMessage":"maclaurin_cos() requires either an int or float for theta","messagePattern":"maclaurin_cos\\(\\) requires either an int or float for theta","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/maclaurin_series.py","lineNumber":99,"sourceCode":"    Traceback (most recent call last):\r\n        ...\r\n    ValueError: maclaurin_cos() requires either an int or float for theta\r\n    >>> maclaurin_cos(10, -30)\r\n    Traceback (most recent call last):\r\n        ...\r\n    ValueError: maclaurin_cos() requires a positive int for accuracy\r\n    >>> maclaurin_cos(10, 30.5)\r\n    Traceback (most recent call last):\r\n        ...\r\n    ValueError: maclaurin_cos() requires a positive int for accuracy\r\n    >>> maclaurin_cos(10, \"30\")\r\n    Traceback (most recent call last):\r\n        ...\r\n    ValueError: maclaurin_cos() requires a positive int for accuracy\r\n    \"\"\"\r\n\r\n    if not isinstance(theta, (int, float)):\r\n        raise ValueError(\"maclaurin_cos() 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_cos() 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((-1) ** r * theta ** (2 * r) / factorial(2 * r) for r in range(accuracy))\r\n\r\n\r\nif __name__ == \"__main__\":\r\n    import doctest\r\n\r\n    doctest.testmod()\r\n\r\n    print(maclaurin_sin(10))\r\n    print(maclaurin_sin(-10))\r\n    print(maclaurin_sin(10, 15))\r","sourceCodeStart":81,"sourceCodeEnd":117,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/maclaurin_series.py#L81-L117","documentation":"Raised by maclaurin_cos() in maths/maclaurin_series.py when theta is not an int or float. The function approximates cos(theta) via its Maclaurin series after normalizing theta with `theta = float(theta)`, so only real numeric radians are accepted. Any other type fails this isinstance gate before computation.","triggerScenarios":"maclaurin_cos('0'), maclaurin_cos(None), maclaurin_cos(complex(1,2)), or passing a value straight from a text field/config without conversion.","commonSituations":"Angles from UI text inputs, CSV/JSON string fields, or accidentally passing the wrong positional argument (e.g. a label where the angle goes).","solutions":["Convert to float at the call site: maclaurin_cos(float(theta)).","Sanitize external inputs once at ingestion, then pass validated floats through your code.","Check argument order — this error often means a non-angle value was passed as the first parameter."],"exampleFix":"# before\nmaclaurin_cos(angle_str)  # angle_str = '1.57'\n\n# after\nmaclaurin_cos(float(angle_str))","handlingStrategy":"type-guard","validationCode":"theta = float(theta)  # raises your own TypeError early if theta is not numeric","typeGuard":"def is_valid_theta(x) -> bool:\n    return isinstance(x, (int, float)) and not isinstance(x, bool)","tryCatchPattern":null,"preventionTips":["Validate angles once at ingestion rather than at every math call.","Double-check positional argument order when calling."],"tags":["math","type-validation","valueerror","trigonometry"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}