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_binary_iterative(num) in conversions/decimal_to_binary.py:31 when `num` is a float. The algorithm repeatedly applies num >>= 1 and num % 2, which are integer operations; floats (even integral-valued ones like 16.0) are rejected with a TypeError before any math runs. Negative integers are supported, but only true ints.

Source

Thrown at conversions/decimal_to_binary.py:31

    >>> decimal_to_binary_iterative(35)
    '0b100011'
    >>> # negatives work too
    >>> decimal_to_binary_iterative(-2)
    '-0b10'
    >>> # other floats will error
    >>> decimal_to_binary_iterative(16.16) # doctest: +ELLIPSIS
    Traceback (most recent call last):
        ...
    TypeError: 'float' object cannot be interpreted as an integer
    >>> # strings will error as well
    >>> decimal_to_binary_iterative('0xfffff') # doctest: +ELLIPSIS
    Traceback (most recent call last):
        ...
    TypeError: 'str' object cannot be interpreted as an integer
    """

    if isinstance(num, float):
        raise TypeError("'float' object cannot be interpreted as an integer")
    if isinstance(num, str):
        raise TypeError("'str' object cannot be interpreted as an integer")

    if num == 0:
        return "0b0"

    negative = False

    if num < 0:
        negative = True
        num = -num

    binary: list[int] = []
    while num > 0:
        binary.insert(0, num % 2)
        num >>= 1

    if negative:

View on GitHub (pinned to f5988cc097)

Solutions

  1. If the float is integral, convert first: decimal_to_binary_iterative(int(x))
  2. If the float has a fractional part, decide on truncation (int(x)), rounding (round(x)), or rejecting it in your own validation — the library will not guess
  3. Trace where the float came from and use integer arithmetic (// instead of /) upstream

Example fix

# before
decimal_to_binary_iterative(16.16)  # TypeError

# after
decimal_to_binary_iterative(int(16.16))  # '0b10000'
Defensive patterns

Strategy: type-guard

Validate before calling

if not isinstance(num, int) or isinstance(num, bool):
    num = int(num)  # or raise your own error for non-integral floats

Type guard

def is_plain_int(v: object) -> TypeGuard[int]:
    return isinstance(v, int) and not isinstance(v, bool)

Try / catch

try:
    decimal_to_binary_iterative(num)
except TypeError:
    num = int(num)
    result = decimal_to_binary_iterative(num)

Prevention

When it happens

Trigger: decimal_to_binary_iterative(16.16), decimal_to_binary_iterative(40.0), or passing a value that came from float parsing, statistics (mean/median), or division with `/`.

Common situations: Averaging or dividing values before conversion; reading numbers from JSON/CSV where the parser produced floats; mixing this function with math module results.

Related errors


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