TheAlgorithms/Python · error · ValueError

differentiate() requires an int as input for order

Error message

differentiate() requires an int as input for order

What it means

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.

Source

Thrown at maths/dual_number_automatic_differentiation.py:123

    >>> differentiate(8, 8, 8)
    Traceback (most recent call last):
        ...
    ValueError: differentiate() requires a function as input for func
    >>> differentiate(lambda x: x **2, "", 1)
    Traceback (most recent call last):
        ...
    ValueError: differentiate() requires a float as input for position
    >>> differentiate(lambda x: x**2, 3, "")
    Traceback (most recent call last):
        ...
    ValueError: differentiate() requires an int as input for order
    """
    if not callable(func):
        raise ValueError("differentiate() requires a function as input for func")
    if not isinstance(position, (float, int)):
        raise ValueError("differentiate() requires a float as input for position")
    if not isinstance(order, int):
        raise ValueError("differentiate() requires an int as input for order")
    d = Dual(position, 1)
    result = func(d)
    if order == 0:
        return result.real
    return result.duals[order - 1] * factorial(order)


if __name__ == "__main__":
    import doctest

    doctest.testmod()

    def f(y):
        return y**2 * y**4

    print(differentiate(f, 9, 2))

View on GitHub (pinned to f5988cc097)

Solutions

  1. Pass an int order: differentiate(f, x, 1) or int(order).
  2. Additionally validate order >= 0 yourself, since the library does not check negativity.
  3. Use keyword arguments (order=2) to avoid positional mix-ups with position.

Example fix

# before
differentiate(lambda x: x ** 2, 3.0, '1')  # ValueError

# after
order = int('1')
differentiate(lambda x: x ** 2, 3.0, order, )  # 6.0; also ensure order >= 0
Defensive patterns

Strategy: type-guard

Validate before calling

if not isinstance(order, int) or isinstance(order, bool) or order < 0:
    raise ValueError(f'order must be a non-negative int, got {order!r}')

Type guard

def is_valid_order(o) -> bool:
    return isinstance(o, int) and not isinstance(o, bool) and o >= 0

Try / catch

try:
    d = differentiate(func, position, order)
except ValueError as e:
    if 'int as input for order' in str(e):
        d = differentiate(func, position, int(order))
    else:
        raise

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14). Data as JSON: /api/errors/0cddebb6f1137c52. Report an issue: GitHub.