Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix nan handling in fp.mag() and hyper() #688

Merged
merged 2 commits into from
Apr 28, 2023

Conversation

skirpichev
Copy link
Collaborator

Closes #485
Closes #479
Closes #507
Closes #489
Closes #488
Closes #478

mpmath/ctx_fp.py Outdated
@@ -200,7 +203,7 @@ def hypsum(ctx, p, q, types, coeffs, z, maxterms=6000, **kwargs):
for i in num: t *= (coeffs[i]+k)
for i in den: t /= (coeffs[i]+k)
k += 1; t /= k; t *= z; s += t
if abs(t) < tol:
if not abs(t) >= tol:
Copy link
Collaborator

@dimpase dimpase Apr 20, 2023

Choose a reason for hiding this comment

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

Why is this needed? To account for a possible NaN from abs(t)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

BTW, maybe we should explicitly test for nan's (as b0b5446 does), not as here or in 0d97f93. Or redo the while statement like 064e940.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Please check for NaN explicitly, for code readability.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ok, a quick check for nan was added to the hyper(), everything else was reverted.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@dimpase, does it make sense for you now?

Copy link
Collaborator

Choose a reason for hiding this comment

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

it's still puzzling how it's possible to get to the NaN check without getting an overflow (and thus an uncaught error). I.e., why is this check needed, at all?

Also, you can get an inf rather than NaN, and inf is perfectly possible to meaningfully compare with non-infs.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

it's still puzzling how it's possible to get to the NaN check without getting an overflow (and thus an uncaught error). I.e., why is this check needed, at all?

The idea was to use double negation (< -> not >=) to make a quick exit for nan's.

I agree, this is much more clear in form like while abs(t) >= tol: ...

Also, you can get an inf rather than NaN, and inf is perfectly possible to meaningfully compare with non-infs.

I think this piece of code can't handle inf's correctly, so nothing was broken.

Anyway, I've reverted this stuff: an explicit check for nan's in hyper() does the job. The second commit is optional now, but I think it's a good idea to have same output for fp.mag() and mp.mag(). Right now we have 0 and nan for nan's, respectively.

@skirpichev skirpichev requested a review from dimpase April 21, 2023 04:59
@skirpichev skirpichev changed the title Fix nan handling in fp.mag(), hypsum(), _hyp2f1_gosper() and _hyp2f1() Fix nan handling in fp.mag() and hyper() Apr 21, 2023
@skirpichev
Copy link
Collaborator Author

Ok, given the changes are trivial now, I'm going to merge this tomorrow.

@skirpichev skirpichev merged commit d691324 into mpmath:master Apr 28, 2023
6 checks passed
@skirpichev skirpichev deleted the fix-nan branch April 28, 2023 04:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants