{"record":{"id":"daf451eee39defe5","repo":"TheAlgorithms/Python","slug":"differentiate-requires-a-float-as-input-for-posi","errorCode":null,"errorMessage":"differentiate() requires a float as input for position","messagePattern":"differentiate\\(\\) requires a float as input for position","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/dual_number_automatic_differentiation.py","lineNumber":121,"sourceCode":"    >>> differentiate(lambda y: y ** 2, 4, 3)\r\n    0\r\n    >>> 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","sourceCodeStart":103,"sourceCodeEnd":139,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/dual_number_automatic_differentiation.py#L103-L139","documentation":"Raised by differentiate() in maths/dual_number_automatic_differentiation.py when position is neither float nor int. The evaluation point becomes the real part of the seed Dual(position, 1), so it must be a real number; strings and other types fail the isinstance(position, (float, int)) check and raise ValueError with this message (note the message says 'float' even though ints are accepted).","triggerScenarios":"Calling differentiate(lambda x: x**2, '', 1), differentiate(f, None, 1), or differentiate(f, '3.0', 1). Note differentiate(lambda x: x**2, 3, 1) is fine — int passes.","commonSituations":"Position read as a string from argv, input(), or a config file; None from an optional parameter default; Decimal or Fraction position values that are not float/int subclasses.","solutions":["Convert before calling: differentiate(f, float(position), 1).","Validate numeric input at your boundary (reject empty strings, parse with float()).","Cast Decimal/Fraction positions to float explicitly, accepting the precision change."],"exampleFix":"# before\ndifferentiate(lambda x: x ** 2, input('x: '), 1)  # str -> ValueError\n\n# after\nx0 = float(input('x: '))\ndifferentiate(lambda x: x ** 2, x0, 1)","handlingStrategy":"type-guard","validationCode":"if not isinstance(position, (float, int)) or isinstance(position, bool):\n    position = float(position)  # after your own parse validation","typeGuard":"def is_real_number(v) -> bool:\n    return isinstance(v, (int, float)) and not isinstance(v, bool)","tryCatchPattern":"try:\n    d = differentiate(func, position, order)\nexcept ValueError as e:\n    if 'float as input for position' in str(e):\n        d = differentiate(func, float(position), order)\n    else:\n        raise","preventionTips":["Convert argv/input() values with float() at the boundary.","Cast Decimal/Fraction positions to float explicitly before calling."],"tags":["maths","autodiff","validation","valueerror"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}