Skip to content

maths/largest_of_very_large_numbers.py: res() missing documented ValueError for negative input #14930

Description

@thejesh23

Repository commit

c0db072 (master)

Python version (python --version)

Python 3.12.13

Dependencies version (pip freeze)

Standard library only (math).

Expected behavior

The docstring of maths.largest_of_very_large_numbers.res documents the following doctest for a negative x:

>>> res(-1, 5)
Traceback (most recent call last):
...
ValueError: expected a positive input

So res(-1, 5) should raise ValueError with the message expected a positive input, and this doctest should pass under pytest --doctest-modules.

Actual behavior

There is no input validation in res(). When x < 0 and y != 0 the function reaches math.log10(x), which raises ValueError: math domain error (wrong message) instead of the documented expected a positive input. The doctest as written therefore fails:

$ python3 -m doctest maths/largest_of_very_large_numbers.py -v
...
Failed example:
    res(-1, 5)
Expected:
    Traceback (most recent call last):
    ...
    ValueError: expected a positive input
Got:
    Traceback (most recent call last):
        ...
        return y * math.log10(x)
                   ^^^^^^^^^^^^^
    ValueError: math domain error
1 items had failures:
   1 of   4 in maths.largest_of_very_large_numbers.res
***Test Failed*** 1 failures.

Suggested fix

Add the missing check at the top of res():

if x < 0:
    raise ValueError("expected a positive input")

This makes the code match the documented contract, and turns the currently-failing doctest into a passing one. A PR is being opened.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions