GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Customize and improve error messages in the math module. · Issue #101410 · python/cpython

Customize and improve error messages in the math module. #101410

New issue
Copy link
New issue
Copy link
Closed
Closed
Customize and improve error messages in the math module.#101410
Copy link
Labels
extension-modulesC modules in the Modules dirC modules in the Modules dirtype-featureA feature request or enhancementA feature request or enhancement

Description

@rhettinger
rhettinger
opened on Jan 29, 2023
Issue body actions

The default error messages on a several math functions are mostly opaque and and unhelpful.

>>> sqrt(-5)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: math domain error

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):

ValueError:  math.sqrt() expects a non-negative input.  See cmath.sqrt() for variation that supports complex numbers
ValueError:  math.log() requires a non-negative input.   

Linked PRs

  • gh-101410: Improve error messages in math module #101492
  • gh-101410: support custom messages for domain errors in the math module #124299
  • gh-101410: customize error messages for 1-arg math functions #129497
  • gh-101410: revert loghelper() change in 75f59bb for integer input #132625

Activity

  1. rhettinger
    added
    type-featureA feature request or enhancementA feature request or enhancement
    on Jan 29, 2023
  2. CharlieZhao95 commented on Feb 1, 2023

    @CharlieZhao95
    CharlieZhao95
    on Feb 1, 2023
    Contributor
    More actions

    I checked the error message of sqrt in 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 numbers
    

    It seems that we need to expand the macro FUNC1 and the function math_1_to_whatever to add the parameter error_msg to them. Is this a feasible solution? I want to try to work for it. :)

  3. CharlieZhao95
    added a commit that references this issue on Feb 6, 2023

    Merge remote-tracking branch 'upstream/main' into pythongh-101410/mat…

    611dedb
  4. iritkatriel
    added
    extension-modulesC modules in the Modules dirC modules in the Modules dir
    on Nov 27, 2023
  5. serhiy-storchaka
    added a commit that references this issue on Dec 5, 2023

    Merge branch 'main' into pythongh-101410/math_error_message

    ef68c24
  6. CharlieZhao95
    added a commit that references this issue on Jul 22, 2024

    Merge branch 'main' into pythongh-101410/math_error_message

    dc8e593
  7. skirpichev
    added a commit that references this issue on Sep 21, 2024

    Merge branch 'main' into pythongh-101410/math_error_message

    30be617
  8. skirpichev
    self-assigned this
    on Sep 21, 2024
  9. skirpichev
    added 2 commits that reference this issue on Sep 21, 2024

    pythongh-101410: support custom messages for domain errors in the mat…

    5aa7921

    pythongh-101410: support custom messages for domain errors in the mat…

    fb01518
  10. serhiy-storchaka commented on Sep 22, 2024

    @serhiy-storchaka
    serhiy-storchaka
    on Sep 22, 2024
    Member
    More actions

    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.

  11. skirpichev commented on Sep 22, 2024

    @skirpichev
    skirpichev
    on Sep 22, 2024
    Member
    More actions

    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.

  12. vstinner
    added a commit that references this issue on Jan 23, 2025

    gh-101410: support custom messages for domain errors in the math modu…

    75f59bb
  13. vstinner commented on Jan 23, 2025

    @vstinner
    vstinner
    on Jan 23, 2025
    Member
    More actions

    Issue implemented in the change 75f59bb.

  14. vstinner
    closed this as completedon Jan 23, 2025
  15. skirpichev commented on Jan 24, 2025

    @skirpichev
    skirpichev
    on Jan 24, 2025
    Member
    More actions

    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.

  16. skirpichev
    reopened this on Jan 24, 2025
  17. vstinner commented on Jan 24, 2025

    @vstinner
    vstinner
    on Jan 24, 2025
    Member
    More actions

    @serhiy-storchaka:

    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.

    @skirpichev:

    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 added repr(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 <[email protected]>
    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
  18. skirpichev
    added a commit that references this issue on Jan 31, 2025

    pythongh-101410: customize error messages for 1-arg math functions

    77a643c
  19. skirpichev
    removed their assignment
    on Feb 1, 2025
  20. skirpichev
    added a commit that references this issue on Apr 17, 2025

    pythongh-101410: revert loghelper() change in 75f59bb for integer input

    f2e0142
  21. serhiy-storchaka
    added a commit that references this issue on Apr 17, 2025

    gh-101410: Revert loghelper() change in 75f59bb for integer input (GH…

    0c356c8
  22. vstinner
    added a commit that references this issue on Apr 22, 2025

    gh-101410: Customize error messages for 1-arg math functions (#129497)

    d0660a9
  23. vstinner
    closed this as completedon Apr 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    extension-modulesC modules in the Modules dirC modules in the Modules dirtype-featureA feature request or enhancementA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.