{"record":{"id":"85613e7a4f6d50d4","repo":"TheAlgorithms/Python","slug":"the-value-of-both-inputs-must-be-positive","errorCode":null,"errorMessage":"the value of both inputs must be positive","messagePattern":"the value of both inputs must be positive","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"bit_manipulation/binary_and_operator.py","lineNumber":36,"sourceCode":"    >>> binary_and(0, 255)\n    '0b00000000'\n    >>> binary_and(256, 256)\n    '0b100000000'\n    >>> binary_and(0, -1)\n    Traceback (most recent call last):\n        ...\n    ValueError: the value of both inputs must be positive\n    >>> binary_and(0, 1.1)\n    Traceback (most recent call last):\n        ...\n    ValueError: Unknown format code 'b' for object of type 'float'\n    >>> binary_and(\"0\", \"1\")\n    Traceback (most recent call last):\n        ...\n    TypeError: '<' not supported between instances of 'str' and 'int'\n    \"\"\"\n    if a < 0 or b < 0:\n        raise ValueError(\"the value of both inputs must be positive\")\n\n    a_binary = format(a, \"b\")\n    b_binary = format(b, \"b\")\n\n    max_len = max(len(a_binary), len(b_binary))\n\n    return \"0b\" + \"\".join(\n        str(int(char_a == \"1\" and char_b == \"1\"))\n        for char_a, char_b in zip(a_binary.zfill(max_len), b_binary.zfill(max_len))\n    )\n\n\nif __name__ == \"__main__\":\n    import doctest\n\n    doctest.testmod()\n","sourceCodeStart":18,"sourceCodeEnd":53,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/bit_manipulation/binary_and_operator.py#L18-L53","documentation":"Raised by escape_velocity() in physics/escape_velocity.py when radius == 0 exactly. The formula v = sqrt(2*G*mass/radius) divides by radius, so the library pre-empts the implicit ZeroDivisionError with this explicit one. Note only exact 0 is rejected — negative radii are NOT checked and will flow into math.sqrt as a negative argument.","triggerScenarios":"escape_velocity(mass=1.0, radius=0); radius loaded from a config default of 0 or a dataframe column with 0 for missing values; escape_velocity(mass=0, radius=0) also raises even though mass=0 alone would return 0.0.","commonSituations":"Config files with unset radius defaulting to 0; CSV/JSON data where 0 encodes 'missing'; unit tests passing 0 as a boundary probe.","solutions":["Validate radius > 0 before calling and fix the default/missing-value handling in your data source.","Represent missing radii as None (and skip) instead of 0.","Catch ZeroDivisionError at the data-loading boundary to reject corrupt records."],"exampleFix":"# before\nv = escape_velocity(mass=body['mass'], radius=body.get('radius', 0))\n\n# after\nv = escape_velocity(body['mass'], body['radius']) if body['radius'] > 0 else None","handlingStrategy":"validation","validationCode":"if radius == 0:\n    raise ValueError(\"radius must be nonzero; got 0 (missing data?)\")\nv = escape_velocity(mass, radius)","typeGuard":"def is_nonzero_radius(r: object) -> bool:\n    return isinstance(r, (int, float)) and not isinstance(r, bool) and r != 0","tryCatchPattern":"try:\n    v = escape_velocity(m, r)\nexcept ZeroDivisionError:\n    skip_record(record_id)  # reject corrupt row at load boundary","preventionTips":["Encode missing radii as None, not 0.","Note: negative radii are NOT guarded here and will fail later inside math.sqrt.","mass=0 is legal and returns 0.0."],"tags":["physics","division-by-zero","data-quality"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}