TheAlgorithms/Python · error · ValueError
differentiate() requires a float as input for position
Error message
differentiate() requires a float as input for position
What it means
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).
Source
Thrown at maths/dual_number_automatic_differentiation.py:121
>>> differentiate(lambda y: y ** 2, 4, 3)
0
>>> 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
- 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.
Example fix
# before
differentiate(lambda x: x ** 2, input('x: '), 1) # str -> ValueError
# after
x0 = float(input('x: '))
differentiate(lambda x: x ** 2, x0, 1) Defensive patterns
Strategy: type-guard
Validate before calling
if not isinstance(position, (float, int)) or isinstance(position, bool):
position = float(position) # after your own parse validation Type guard
def is_real_number(v) -> bool:
return isinstance(v, (int, float)) and not isinstance(v, bool) Try / catch
try:
d = differentiate(func, position, order)
except ValueError as e:
if 'float as input for position' in str(e):
d = differentiate(func, float(position), order)
else:
raise Prevention
- Convert argv/input() values with float() at the boundary.
- Cast Decimal/Fraction positions to float explicitly before calling.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- differentiate() requires a function as input for func
- differentiate() requires an int as input for order
- Both points must have the same dimension.
- Monogons and Digons are not polygons in the Euclidean space
- All values must be greater than 0
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/daf451eee39defe5.
Report an issue: GitHub.