TheAlgorithms/Python · error · TypeError

Input value must be a 'int' type

Error message

Input value must be a 'int' type

What it means

Raised by largest_pow_of_two_le_num when the argument is a float. The function doubles a result bit until it exceeds the number, requiring an integer; floats are explicitly rejected. Non-float non-ints (e.g., strings) are not guarded and fail with a comparison TypeError at `number <= 0`. Non-positive numbers return 0 instead of raising.

Source

Thrown at bit_manipulation/largest_pow_of_two_le_num.py:48

    >>> largest_pow_of_two_le_num(-1)
    0
    >>> largest_pow_of_two_le_num(3)
    2
    >>> largest_pow_of_two_le_num(15)
    8
    >>> largest_pow_of_two_le_num(99)
    64
    >>> largest_pow_of_two_le_num(178)
    128
    >>> largest_pow_of_two_le_num(999999)
    524288
    >>> largest_pow_of_two_le_num(99.9)
    Traceback (most recent call last):
        ...
    TypeError: Input value must be a 'int' type
    """
    if isinstance(number, float):
        raise TypeError("Input value must be a 'int' type")
    if number <= 0:
        return 0
    res = 1
    while (res << 1) <= number:
        res <<= 1
    return res


if __name__ == "__main__":
    import doctest

    doctest.testmod()

View on GitHub (pinned to f5988cc097)

Solutions

  1. Floor the input: largest_pow_of_two_le_num(int(x)) or math.floor(x) for floats.
  2. Use bit_length for a closed form on positive ints: 1 << (n.bit_length() - 1) when n >= 1.
  3. Validate capacity/size inputs as positive ints at config-load time.

Example fix

# before
largest_pow_of_two_le_num(99.9)  # TypeError

# after
largest_pow_of_two_le_num(int(99.9))  # 64
Defensive patterns

Strategy: type-guard

Validate before calling

import math
number = int(math.floor(number)) if isinstance(number, float) else number

Type guard

def is_int(value: object) -> bool:
    return isinstance(value, int)

Prevention

When it happens

Trigger: Calling largest_pow_of_two_le_num(99.9) or any float. largest_pow_of_two_le_num(-5) does not raise — it returns 0.

Common situations: Sizing hash-table buckets or buffer blocks from computed loads that are floats; passing ratios or normalized values where an integer quantity was intended.

Related errors


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