-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
Pass return value on ValueError exceptions in the cmath/math modules #133895
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
Comments
CC @tim-one, @mdickinson |
CC @serhiy-storchaka, does this make sense for you? |
Now this information is available as the "value" attribute of the ValueError exception object: ```pycon >>> import cmath >>> try: ... cmath.log(complex(-0.0, 0)) ... except ValueError as exc: ... print(exc.value) ... (-inf+3.141592653589793j) ``` Also uses the AC magic for return value of the cmath.rect().
Now this information is available as the "value" attribute of the ValueError exception object: ```pycon >>> import math >>> try: ... math.gamma(-0.0) ... except ValueError as exc: ... print(exc.value) ... -inf ```
I don't know how useful this feature would be. On the one hand, it adds overhead in case an exception was raised. It is unfortunate because the EAFP principle is commonly used in Python. On other hand, if the user needs such information, there is no way to get it in other way. And the cost of raising and catching an exception is already high, so relative overhead is not so great. But I do not know if anyone really need it. This needs a wider discussion. Should the value be added also for |
@serhiy-storchaka, my major motivation was the mpmath's fp context (fixed precision). It uses Python's floats and math/cmath functions. But other mpmath contexts have (just like gmpy2) a different defaults: exceptions are not raised. Unfortunately, it's not easy to fix the fp context to match this behavior: essentially this require to re-implement handling of special values in all stdlib's functions. With proposed solution I could just wrap such functions with try-except block.
Yes, I think you are right, i.e.: >>> gmpy2.mpfr(1)/gmpy2.mpfr(0)
mpfr('inf')
Maybe. Alternative solution might be some context notion for floating-point math, just like for the decimal module. With this we can open door to more features of the platform's floating-point arithmetic, e.g. support different rounding modes. (This is a kinda possible with ctypes, but FE_DOWNWARD, FE_TONEAREST, FE_TOWARDZERO and FE_UPWARD values are system-specific.) And it's essentially for free. What do you think? |
BTW, as I expected, computed values in exceptional cases (special values, overflows) might be incorrect in the cmath module. I found two examples in our test suite so far, fixed in #134995. |
Uh oh!
There was an error while loading. Please reload this page.
Feature or enhancement
Proposal:
Currently the error handling happens this way for the cmath module (taken from module comments):
The ValueError raised for EDOM and the OverflowError - for ERANGE, but the Annex G result is hidden from the pure-Python world. Though, it might be helpful for applications. E.g.
clog(-0+0i)
returns-∞+πi
andclog(+0+0i)
returns-∞+0i
- correct one-sided limits in the pole of log(). (BTW, something like PoleError could be better here, but that's another story.)The mpmath and the gmpy2 (per default, if
trap_divzero
and/ortrap_invalid
context options aren't enabled) rather return special values per the C standard, not raise exceptions. And the mpmath also uses builtin float's and math/cmath functions for the fp context. Thus, to override current behavior of the stdlib - we need to catch ValueError from the called function and then process function arguments to return special values, i.e. essentially re-implement handling of special values. But they already are computed in cmath/math functions, so why not return this information with an exception? An example:Initial patch, working for most functions, not using math_error().
Similar happens in the math module and fix looks simple as well. I didn't check all cases, but it seems that most functions in the cmath/math modules actually compute correct (per C standard and Annex G) answers for special values.
Alternative approach: some global flag to turn on the gmpy2-like behavior (i.e. raise no exceptions, but return special values instead).
Has this already been discussed elsewhere?
This is a minor feature, which does not need previous discussion elsewhere
Links to previous discussion of this feature:
No response
Linked PRs
The text was updated successfully, but these errors were encountered: