{"record":{"id":"0cddebb6f1137c52","repo":"TheAlgorithms/Python","slug":"differentiate-requires-an-int-as-input-for-order","errorCode":null,"errorMessage":"differentiate() requires an int as input for order","messagePattern":"differentiate\\(\\) requires an int as input for order","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/dual_number_automatic_differentiation.py","lineNumber":123,"sourceCode":"    >>> differentiate(8, 8, 8)\r\n    Traceback (most recent call last):\r\n        ...\r\n    ValueError: differentiate() requires a function as input for func\r\n    >>> differentiate(lambda x: x **2, \"\", 1)\r\n    Traceback (most recent call last):\r\n        ...\r\n    ValueError: differentiate() requires a float as input for position\r\n    >>> differentiate(lambda x: x**2, 3, \"\")\r\n    Traceback (most recent call last):\r\n        ...\r\n    ValueError: differentiate() requires an int as input for order\r\n    \"\"\"\r\n    if not callable(func):\r\n        raise ValueError(\"differentiate() requires a function as input for func\")\r\n    if not isinstance(position, (float, int)):\r\n        raise ValueError(\"differentiate() requires a float as input for position\")\r\n    if not isinstance(order, int):\r\n        raise ValueError(\"differentiate() requires an int as input for order\")\r\n    d = Dual(position, 1)\r\n    result = func(d)\r\n    if order == 0:\r\n        return result.real\r\n    return result.duals[order - 1] * factorial(order)\r\n\r\n\r\nif __name__ == \"__main__\":\r\n    import doctest\r\n\r\n    doctest.testmod()\r\n\r\n    def f(y):\r\n        return y**2 * y**4\r\n\r\n    print(differentiate(f, 9, 2))\r\n","sourceCodeStart":105,"sourceCodeEnd":140,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/dual_number_automatic_differentiation.py#L105-L140","documentation":"Raised by differentiate() in maths/dual_number_automatic_differentiation.py when the order argument is not an int. The derivative order selects which dual part to read (result.duals[order - 1] * factorial(order)) and only makes sense as a non-negative integer; strings, floats, and None fail isinstance(order, int) and raise ValueError.","triggerScenarios":"Calling differentiate(lambda x: x**2, 3, '') or differentiate(f, 3.0, 1.0) — any non-int order. Note there is no >= 0 check: negative ints pass this guard and then fail later at duals[order - 1] with IndexError.","commonSituations":"Order parsed from CLI/config as a string ('2' instead of 2); float order 1.0 from arithmetic; forgetting that the third positional argument is the order when calling positionally.","solutions":["Pass an int order: differentiate(f, x, 1) or int(order).","Additionally validate order >= 0 yourself, since the library does not check negativity.","Use keyword arguments (order=2) to avoid positional mix-ups with position."],"exampleFix":"# before\ndifferentiate(lambda x: x ** 2, 3.0, '1')  # ValueError\n\n# after\norder = int('1')\ndifferentiate(lambda x: x ** 2, 3.0, order, )  # 6.0; also ensure order >= 0","handlingStrategy":"type-guard","validationCode":"if not isinstance(order, int) or isinstance(order, bool) or order < 0:\n    raise ValueError(f'order must be a non-negative int, got {order!r}')","typeGuard":"def is_valid_order(o) -> bool:\n    return isinstance(o, int) and not isinstance(o, bool) and o >= 0","tryCatchPattern":"try:\n    d = differentiate(func, position, order)\nexcept ValueError as e:\n    if 'int as input for order' in str(e):\n        d = differentiate(func, position, int(order))\n    else:\n        raise","preventionTips":["Use keyword args (order=2) to avoid positional swaps with position.","Also enforce order >= 0 yourself — negative ints pass the library's check and crash later with IndexError."],"tags":["maths","autodiff","validation","valueerror"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}