{"record":{"id":"60d9cee0860a8e6f","repo":"TheAlgorithms/Python","slug":"float-object-cannot-be-interpreted-as-an-integer","errorCode":null,"errorMessage":"'float' object cannot be interpreted as an integer","messagePattern":"'float' object cannot be interpreted as an integer","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"conversions/decimal_to_any.py","lineNumber":67,"sourceCode":"    >>> # a base less than 2 will error\n    >>> decimal_to_any(7, 0) # doctest: +ELLIPSIS\n    Traceback (most recent call last):\n        ...\n    ValueError: base must be >= 2\n    >>> # a base greater than 36 will error\n    >>> decimal_to_any(34, 37) # doctest: +ELLIPSIS\n    Traceback (most recent call last):\n        ...\n    ValueError: base must be <= 36\n    \"\"\"\n    if isinstance(num, float):\n        raise TypeError(\"int() can't convert non-string with explicit base\")\n    if num < 0:\n        raise ValueError(\"parameter must be positive int\")\n    if isinstance(base, str):\n        raise TypeError(\"'str' object cannot be interpreted as an integer\")\n    if isinstance(base, float):\n        raise TypeError(\"'float' object cannot be interpreted as an integer\")\n    if base in (0, 1):\n        raise ValueError(\"base must be >= 2\")\n    if base > 36:\n        raise ValueError(\"base must be <= 36\")\n    new_value = \"\"\n    mod = 0\n    div = 0\n    while div != 1:\n        div, mod = divmod(num, base)\n        if base >= 11 and 9 < mod < 36:\n            actual_value = ALPHABET_VALUES[str(mod)]\n        else:\n            actual_value = str(mod)\n        new_value += actual_value\n        div = num // base\n        num = div\n        if div == 0:\n            return str(new_value[::-1])","sourceCodeStart":49,"sourceCodeEnd":85,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/conversions/decimal_to_any.py#L49-L85","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"# before\ndecimal_to_any(58, float(cfg[\"base\"]))  # TypeError\n\n# after\ndecimal_to_any(58, int(cfg[\"base\"]))","handlingStrategy":"type-guard","validationCode":"def valid_base(base) -> bool:\n    return isinstance(base, int) and not isinstance(base, bool) and 2 <= base <= 36","typeGuard":"def is_int_base(base: object) -> TypeGuard[int]:\n    return isinstance(base, int) and not isinstance(base, bool)","tryCatchPattern":null,"preventionTips":["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"],"tags":["python","type-error","radix","input-validation"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}