Skip to content

maths/largest_of_very_large_numbers: validate positive input as docstring documents#14933

Open
thejesh23 wants to merge 1 commit into
TheAlgorithms:masterfrom
thejesh23:fix/largest-numbers-validate-positive
Open

maths/largest_of_very_large_numbers: validate positive input as docstring documents#14933
thejesh23 wants to merge 1 commit into
TheAlgorithms:masterfrom
thejesh23:fix/largest-numbers-validate-positive

Conversation

@thejesh23

Copy link
Copy Markdown

Describe your change:

Fixes #14930

The docstring of res() in maths/largest_of_very_large_numbers.py already documents:

```

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

but the corresponding check was never actually written. Calling res(-1, 5) reaches math.log10(-1) and raises ValueError: math domain error instead of the documented message, so the doctest fails under python -m doctest maths/largest_of_very_large_numbers.py on current master.

This PR adds the missing two-line guard so the code matches its documented contract:

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

After the change, all four doctests in the file pass. The doctest itself was not changed — it already described the intended behaviour.

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Add or change doctests? -- Note: Please avoid changing both code and tests in a single pull request.
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues then the description above includes the issue number(s) with a closing keyword: "Fixes #ISSUE-NUMBER".

The docstring already documents that res(-1, 5) should raise
`ValueError: expected a positive input`, but no such validation
exists. Calling res() with a negative x currently reaches
`math.log10(x)` and raises `ValueError: math domain error` instead,
so the documented doctest fails.

Add the missing check at the top of the function so it matches
the documented contract.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant