TheAlgorithms/Python · error · ValueError

the value of both inputs must be positive

Error message

the value of both inputs must be positive

What it means

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.

Source

Thrown at bit_manipulation/binary_and_operator.py:36

    >>> binary_and(0, 255)
    '0b00000000'
    >>> binary_and(256, 256)
    '0b100000000'
    >>> binary_and(0, -1)
    Traceback (most recent call last):
        ...
    ValueError: the value of both inputs must be positive
    >>> binary_and(0, 1.1)
    Traceback (most recent call last):
        ...
    ValueError: Unknown format code 'b' for object of type 'float'
    >>> binary_and("0", "1")
    Traceback (most recent call last):
        ...
    TypeError: '<' not supported between instances of 'str' and 'int'
    """
    if a < 0 or b < 0:
        raise ValueError("the value of both inputs must be positive")

    a_binary = format(a, "b")
    b_binary = format(b, "b")

    max_len = max(len(a_binary), len(b_binary))

    return "0b" + "".join(
        str(int(char_a == "1" and char_b == "1"))
        for char_a, char_b in zip(a_binary.zfill(max_len), b_binary.zfill(max_len))
    )


if __name__ == "__main__":
    import doctest

    doctest.testmod()

View on GitHub (pinned to f5988cc097)

Solutions

  1. Validate radius > 0 before calling and fix the default/missing-value handling in your data source.
  2. Represent missing radii as None (and skip) instead of 0.
  3. Catch ZeroDivisionError at the data-loading boundary to reject corrupt records.

Example fix

# before
v = escape_velocity(mass=body['mass'], radius=body.get('radius', 0))

# after
v = escape_velocity(body['mass'], body['radius']) if body['radius'] > 0 else None
Defensive patterns

Strategy: validation

Validate before calling

if radius == 0:
    raise ValueError("radius must be nonzero; got 0 (missing data?)")
v = escape_velocity(mass, radius)

Type guard

def is_nonzero_radius(r: object) -> bool:
    return isinstance(r, (int, float)) and not isinstance(r, bool) and r != 0

Try / catch

try:
    v = escape_velocity(m, r)
except ZeroDivisionError:
    skip_record(record_id)  # reject corrupt row at load boundary

Prevention

When it happens

Trigger: 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.

Common situations: Config files with unset radius defaulting to 0; CSV/JSON data where 0 encodes 'missing'; unit tests passing 0 as a boundary probe.

Related errors


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