{"record":{"id":"6b85cd84415bb735","repo":"TheAlgorithms/Python","slug":"float-object-cannot-be-interpreted-as-an-integer-6b85cd","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_binary.py","lineNumber":31,"sourceCode":"    >>> decimal_to_binary_iterative(35)\n    '0b100011'\n    >>> # negatives work too\n    >>> decimal_to_binary_iterative(-2)\n    '-0b10'\n    >>> # other floats will error\n    >>> decimal_to_binary_iterative(16.16) # doctest: +ELLIPSIS\n    Traceback (most recent call last):\n        ...\n    TypeError: 'float' object cannot be interpreted as an integer\n    >>> # strings will error as well\n    >>> decimal_to_binary_iterative('0xfffff') # doctest: +ELLIPSIS\n    Traceback (most recent call last):\n        ...\n    TypeError: 'str' object cannot be interpreted as an integer\n    \"\"\"\n\n    if isinstance(num, float):\n        raise TypeError(\"'float' object cannot be interpreted as an integer\")\n    if isinstance(num, str):\n        raise TypeError(\"'str' object cannot be interpreted as an integer\")\n\n    if num == 0:\n        return \"0b0\"\n\n    negative = False\n\n    if num < 0:\n        negative = True\n        num = -num\n\n    binary: list[int] = []\n    while num > 0:\n        binary.insert(0, num % 2)\n        num >>= 1\n\n    if negative:","sourceCodeStart":13,"sourceCodeEnd":49,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/conversions/decimal_to_binary.py#L13-L49","documentation":"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.","triggerScenarios":"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 `/`.","commonSituations":"Averaging or dividing values before conversion; reading numbers from JSON/CSV where the parser produced floats; mixing this function with math module results.","solutions":["If the float is integral, convert first: decimal_to_binary_iterative(int(x))","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","Trace where the float came from and use integer arithmetic (// instead of /) upstream"],"exampleFix":"# before\ndecimal_to_binary_iterative(16.16)  # TypeError\n\n# after\ndecimal_to_binary_iterative(int(16.16))  # '0b10000'","handlingStrategy":"type-guard","validationCode":"if not isinstance(num, int) or isinstance(num, bool):\n    num = int(num)  # or raise your own error for non-integral floats","typeGuard":"def is_plain_int(v: object) -> TypeGuard[int]:\n    return isinstance(v, int) and not isinstance(v, bool)","tryCatchPattern":"try:\n    decimal_to_binary_iterative(num)\nexcept TypeError:\n    num = int(num)\n    result = decimal_to_binary_iterative(num)","preventionTips":["Keep numeric pipelines in int domain: use // and integer-friendly functions","Convert floats at the boundary (int/round) with an explicit policy for fractional parts","Type-annotate your wrappers as (num: int) so mypy flags float callers"],"tags":["python","type-error","binary","input-validation"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}