TheAlgorithms/Python · error · TypeError
'float' object cannot be interpreted as an integer
Error message
'float' object cannot be interpreted as an integer
What it means
Raised by decimal_to_any(num, base) in conversions/decimal_to_any.py:67 when the `base` argument is a Python float. The function only accepts an integer radix; a float base would make divmod(num, base) produce fractional remainders and break digit computation, so the library rejects it up front with a TypeError. The message deliberately mirrors CPython's own error text for the same situation in int()/divmod contexts.
Source
Thrown at conversions/decimal_to_any.py:67
>>> # a base less than 2 will error
>>> decimal_to_any(7, 0) # doctest: +ELLIPSIS
Traceback (most recent call last):
...
ValueError: base must be >= 2
>>> # a base greater than 36 will error
>>> decimal_to_any(34, 37) # doctest: +ELLIPSIS
Traceback (most recent call last):
...
ValueError: base must be <= 36
"""
if isinstance(num, float):
raise TypeError("int() can't convert non-string with explicit base")
if num < 0:
raise ValueError("parameter must be positive int")
if isinstance(base, str):
raise TypeError("'str' object cannot be interpreted as an integer")
if isinstance(base, float):
raise TypeError("'float' object cannot be interpreted as an integer")
if base in (0, 1):
raise ValueError("base must be >= 2")
if base > 36:
raise ValueError("base must be <= 36")
new_value = ""
mod = 0
div = 0
while div != 1:
div, mod = divmod(num, base)
if base >= 11 and 9 < mod < 36:
actual_value = ALPHABET_VALUES[str(mod)]
else:
actual_value = str(mod)
new_value += actual_value
div = num // base
num = div
if div == 0:
return str(new_value[::-1])View on GitHub (pinned to f5988cc097)
Solutions
- Coerce the base to int before calling: decimal_to_any(34, int(base)) — safe only when the float is integral (16.0 -> 16); 2.5 still needs a real fix
- Use integer division when computing the base dynamically: base = total_bits // group_size instead of /
- Validate base with isinstance(base, int) at your API boundary and reject non-integral floats with your own error before calling the library
Example fix
# before decimal_to_any(58, float(cfg["base"])) # TypeError # after decimal_to_any(58, int(cfg["base"]))
Defensive patterns
Strategy: type-guard
Validate before calling
def valid_base(base) -> bool:
return isinstance(base, int) and not isinstance(base, bool) and 2 <= base <= 36 Type guard
def is_int_base(base: object) -> TypeGuard[int]:
return isinstance(base, int) and not isinstance(base, bool) Prevention
- Always produce the radix with integer arithmetic (// not /)
- int() any base coming from JSON/config before calling decimal_to_any
- Range-check 2..36 at your own API boundary so bad values never reach the library
When it happens
Trigger: Calling decimal_to_any(5, 2.5), decimal_to_any(10, 16.0), or any invocation where the base arrives from JSON parsing, config files, or division (e.g. base=32/2) and is therefore a float, even if it is integral-valued like 16.0.
Common situations: Bases read from JSON/YAML/TOML configs often deserialize as floats ("base": 16.0); computing a base dynamically with `/` instead of `//`; wrapping user input with float() before passing it as the radix.
Related errors
- base must be >= 2
- 'float' object cannot be interpreted as an integer
- iterations must be defined as integers
- All weights must be integers but got weight of type {type(wt
- numbers must be an iterable of integers
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/60d9cee0860a8e6f.
Report an issue: GitHub.