Repository navigation
Customize and improve error messages in the math module. #101410
Description
Activity
- addedtype-featureA feature request or enhancementA feature request or enhancement
on Jan 29, 2023 I checked the error message of
sqrtin Julia and here is the result:julia> sqrt(-5) ERROR: DomainError with -5.0: sqrt will only return a complex result if called with a complex argument. Try sqrt(Complex(x)).Maybe we can also consider adding error messages for input parameters, such as
>>> sqrt(-5) Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: math.sqrt() expects a non-negative input, got '-5.0'. See cmath.sqrt() for variation that supports complex numbersIt seems that we need to expand the macro
FUNC1and the functionmath_1_to_whateverto add the parametererror_msgto them. Is this a feasible solution? I want to try to work for it. :)- addedextension-modulesC modules in the Modules dirC modules in the Modules dir
on Nov 27, 2023 - added a commit that references this issue
on Sep 21, 2024 Should the error message contain the value? There is a difference between
sqrt()taking -5 and -2.220446049250313e-16. Knowing this difference can help to find an error. Especially if the function definition range is limited from two sides.On other side, formatting the repr takes a time, and if the exception is common (it is the result of limited precision in other computations), this can harm the average performance. And for
math.log()the input can be an arbitrary large integer. Its decimal representation can be arbitrary long and take arbitrary long time for calculation.And for math.log() the input can be an arbitrary large integer.
math.log() is a special snowflake. I think here we should keep here just a basic message like "expected a positive input" in the branch, that handle large integers.
- added a commit that references this issue
on Jan 23, 2025 Issue implemented in the change 75f59bb.
No, that just add some infrastructure to support this with few example. Most functions aren't converted yet, though it should be mostly mechanical.
BTW, latest review change 8d28fd8 should be, probably, reverted - see discussion in this issue right above.
On other side, formatting the repr takes a time, and if the exception is common (it is the result of limited precision in other computations), this can harm the average performance. And for math.log() the input can be an arbitrary large integer. Its decimal representation can be arbitrary long and take arbitrary long time for calculation.
BTW, latest review change 8d28fd8 should be, probably, reverted - see discussion in this issue right above.
Oh.
str(int)can produce long output for large integers, I forgot that.If the change is reverted, please add a comment to explain why the error message doesn't include the argument.
It reminds me a
list.remove(x)change which addedrepr(x)to the error message: the change introduced a loop on some special objects which produced an infinite output leading to a crash (out of memory):commit f6cdc6b4a191b75027de342aa8b5d344fb31313e Author: Victor Stinner <vstinner@python.org> Date: Mon Mar 18 14:54:45 2024 +0100 Revert "gh-96844: Improve error message of list.remove (gh-106455)" (#116956) This reverts commit 217f47d6e5e56bca78b8556e910cd00890f6f84a.Reacted by Sergey B Kirpichev- added a commit that references this issue
on Apr 17, 2025
The default error messages on a several math functions are mostly opaque and and unhelpful.
It would require some effort, but the code could be refactored to give a customized and helpful response, a least in common cases (falling back to the C error code message when the cause is not known):
Linked PRs