Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-101410: Improve error messages in math module#101492

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Conversation

@CharlieZhao95
Copy link
Contributor

@CharlieZhao95CharlieZhao95 commentedFeb 1, 2023
edited by bedevere-bot
Loading

Make several math functions support custom error messages.
math.sqrt() andmath.log() now provide more helpful error messages.

>>>importmath# before>>>math.sqrt(-1)Traceback (mostrecentcalllast):File"<stdin>",line1,in<module>ValueError:mathdomainerror# now>>>math.sqrt(-1)Traceback (mostrecentcalllast):File"<stdin>",line1,in<module>ValueError:math.sqrt()expectsanon-negativeinput,got'-1'.Seecmath.sqrt()forvariationthatsupportscomplexnumbers>>>math.log(0)Traceback (mostrecentcalllast):File"<stdin>",line1,in<module>ValueError:math.log()expectsapositiveinput,got'0'.

"Return the square root of x.")
"Return the square root of x.",
"math.sqrt() expects a non-negative input, got '%R'.\n"
"See cmath.sqrt() for variation that supports complex numbers")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Perhaps ana beforevariation would be more correct? Or

See cmath.sqrt(), which also supports negative inputs. I don't know what's better - just a suggestion (:

mdickinson reacted with thumbs up emoji
Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Perhaps ana beforevariation would be more correct? Or

See cmath.sqrt(), which also supports negative inputs. I don't know what's better - just a suggestion (:

Thanks! It seems reasonable to me to adda. Let's wait for others to reply. :)

OTheDev reacted with thumbs up emoji
@CharlieZhao95
Copy link
ContributorAuthor

I guess similar functions and macros can have the same changes (egFUNC1A,FUNC2). I will continue my work after the initial review. :)

@CharlieZhao95CharlieZhao95 marked this pull request as ready for reviewFebruary 3, 2023 02:19
if (Py_IS_NAN(r)&& !Py_IS_NAN(x)) {
PyErr_SetString(PyExc_ValueError,
"math domain error");/* invalid arg */
PyErr_Format(PyExc_ValueError,err_msg,arg);/* invalid arg */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Shouldn't same message be used below on the line 1076 as well (infinite r for finite input)? atanh(-1) is an example.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Shouldn't same message be used below on the line 1076 as well (infinite r for finite input)? atanh(-1) is an example.

I was wondering if there is a function that can have both input and output( NAN r for non-NAN x and infinite r for finite x).

If such a function exists, it may lead to confusing error messages, because one error message corresponds to two scenarios. I haven't thought of such a function yet, please suggest if it exists. :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Same atanh :)

atanh(2) - for the 1st scenario and atanh(1) - for the second. I doubt it will be confusing if you raise instead something like ValueError("atanh expects an argument from (-1, -1), got %R"). I don't think it does make sense to have a different descriptions for pole errors (like atanh(1)).@mdickinson ?

CharlieZhao95 reacted with thumbs up emoji
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Copy link
Member

@serhiy-storchakaserhiy-storchaka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Would not be better to output the converted C double valuex instead of the original Python objectarg? The repr of the Python object can be arbitrary long, unlike to the string representation of C double. Also, there may be some errors introduced at converting Python object to C double, not visible from its repr. Although it is much more cumbersome.

CharlieZhao95 reacted with thumbs up emoji
@serhiy-storchaka
Copy link
Member

serhiy-storchaka commentedSep 23, 2023
edited
Loading

For example,math.log10(Fraction(1, 10**400)) raises an exception, even if the argument is positive. On other handmath.sqrt(Fraction(-1, 10**400)) returns -0.0 even if the argument is negative. BTW,math.sqrt() returning negative zero can cause issues in formatted output.

@CharlieZhao95
Copy link
ContributorAuthor

Would not be better to output the converted C double valuex instead of the original Python objectarg?

Thanks for your suggestion! I forgot this PR while waiting for others to review it, and it’s time to pick it up :)

IMO, it is better to use the C double variable. The only drawback is that we will lose some floating point precision in error message. Does this confuse others? Perhaps this minor flaw is acceptable.

# Using double(%f), the default value is six decimal places.>>>math.sqrt(-math.pi)Traceback (mostrecentcalllast):File"<stdin>",line1,in<module>ValueError:math.sqrt()expectsanonnegativeinput,got'-3.141593'.Seecmath.sqrt()foravariationthatsupportscomplexnumbers>>>math.pi3.141592653589793>>>math.sqrt(-0.00000001)Traceback (mostrecentcalllast):File"<stdin>",line1,in<module>ValueError:math.sqrt()expectsanonnegativeinput,got'-0.000000'.Seecmath.sqrt()foravariationthatsupportscomplexnumbers

@AA-Turner
Copy link
Member

AA-Turner commentedSep 26, 2023
edited
Loading

ValueError:math.sqrt() expects a nonnegative input, got '-3.141593'.

Perhaps just append ' (rounded)' to the end of the line?

A

@skirpichev
Copy link
Contributor

In most cases functions in the module are 1-arg. I think that if we show the function domain (e.g.math.sqrt() expects a non-negative input.) - this will be more than enough.

AA-Turner reacted with thumbs up emoji

@serhiy-storchaka
Copy link
Member

Of course, the output should be more precise and consistent with Python representation, so do not usesnprintf() or like. There are several ways to do this:

  1. Create a Python float object withPyFloat_FromDouble, then use %R format.
  2. UsePyOS_double_to_string() (as in the implementation offloat.__repr__), then use %s format to insert it in the error message.

Before writing code, wait to see if other core developers (in particular@mdickinson and@tim-one) have something to say about this idea.

CharlieZhao95 reacted with thumbs up emoji

@skirpichev
Copy link
Contributor

FYI,@CharlieZhao95, this branch has unresolved conflicts. Are you planing to finish this? If not, I'll open a pr, based on this work.

CharlieZhao95 reacted with thumbs up emoji

@CharlieZhao95
Copy link
ContributorAuthor

FYI,@CharlieZhao95, this branch has unresolved conflicts. Are you planing to finish this? If not, I'll open a pr, based on this work.

Thanks for reminding! BTW, is it time to continue this PR? Maybe we can make this PR simpler (removegot '%R' from the error message first)

Copy link
Member

@picnixzpicnixz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Can you add tests for checking the new messages?

returnNULL;

num=loghelper(args[0],m_log);
num=loghelper(args[0],m_log,"math.log() expects a positive input, got '%R'.");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I don't think using quotes around numbers is relevant. For instance, if it's a custom class of a float that overrides__repr__, then you don't necessarily want to surround the__repr__ by quotes.

Suggested change
num=loghelper(args[0],m_log,"math.log() expects a positive input, got'%R'.");
num=loghelper(args[0],m_log,"math.log() expects a positive input, got%R.");

/*[clinic end generated code: output=5425899a4d5d6acb input=08321262bae4f39b]*/
{
returnloghelper(x,m_log2);
returnloghelper(x,m_log2,"math.log2() expects a positive input, got '%R'.");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Ditto.

/*[clinic end generated code: output=be72a64617df9c6f input=b2469d02c6469e53]*/
{
returnloghelper(x,m_log10);
returnloghelper(x,m_log10,"math.log10() expects a positive input, got '%R'.");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Ditto

CharlieZhao95and others added3 commitsJuly 22, 2024 15:43
…t2aQE.rstCo-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
@CharlieZhao95
Copy link
ContributorAuthor

Can you add tests for checking the new messages?

Sure :) I'm waiting for other core developers to reply. I will add tests after they finalize the solution.

picnixz and skirpichev reacted with thumbs up emoji

@skirpichev
Copy link
Contributor

This work continued in#124299

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@skirpichevskirpichevskirpichev left review comments

@serhiy-storchakaserhiy-storchakaserhiy-storchaka left review comments

@picnixzpicnixzpicnixz left review comments

+2 more reviewers

@mdickinsonmdickinsonmdickinson left review comments

@OTheDevOTheDevOTheDev left review comments

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

8 participants

@CharlieZhao95@serhiy-storchaka@AA-Turner@skirpichev@mdickinson@picnixz@OTheDev@bedevere-bot

[8]ページ先頭

©2009-2025 Movatter.jp