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
add inf, nan, infj, nanj to cmath module #67418
Comments
As pointed out by Guido in bpo-23185, the constants |
What about cmath.nanj? |
Guido also says: """ ... and the same comments would apply to "infj". |
Oh, there is also infj: >>> complex(0, float("inf"))
infj
>>> complex("infj")
infj
>>> complex(0, float("nan"))
nanj
>>> complex("nanj")
nanj |
@Haypo: I'm not keen on either of infj or nanj, on a YAGNI basis. I expect they'd be used almost never, and for the few times that they're really needed, complex(0, inf) and complex(0, nan) seem like good enough spellings. |
Note: following the precedent of cmath.e and cmath.pi, cmath.nan and cmath.inf should have type *float*. Let's not get into the business of deciding *which* complex infinities and nans to represent. |
There are other names which exist only in math, but not in cmath. >>> sorted(set(dir(math)) - set(dir(cmath)))
['atan2', 'ceil', 'copysign', 'degrees', 'erf', 'erfc', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'hypot', 'inf', 'ldexp', 'lgamma', 'log1p', 'log2', 'modf', 'nan', 'pow', 'radians', 'trunc'] May be complex equivalents of all functions should be added for the same reasons? |
(1) This is off-topic for this issue; please open a separate one. (2) Many of those functions simply don't make sense for complex numbers (for example floor, degrees, etc.), or it's not obvious how to extend them. For those that do make sense (erf, for example), implementing and testing a library-quality version would take a lot of time and effort, and no-one would be interested in the result. It's not worth it. |
Agreed. |
Note, the reason I proposed nanj (and infj) is that these are produced by On Tue, Jan 13, 2015 at 8:46 AM, Guido van Rossum <report@bugs.python.org>
|
Another solution would be to change repr() of complex if imaginary component is not finite number to the form complex(x, y). |
Okay, makes sense. One of the reasons I'm a bit unhappy with the idea of adding infj and nanj is that it seems like an encouragement to expect "eval(repr(z))" to work (i.e., recover the original value of z), and because Python doesn't have strict imaginary numbers (i.e., numbers with no real part at all), that fails: >>> from math import inf, nan
>>> infj = complex(0.0, inf)
>>> nanj = complex(0.0, nan)
>>> z = complex(0.0, -inf)
>>> w = eval(repr(z))
>>> z
-infj
>>> w # real part has the "wrong" sign
(-0-infj) But that's a pretty hollow objection, because this really has nothing to do with inf and nan; it's a problem with finite values, too: >>> z = complex(0.0, -3.4)
>>> w = eval(repr(z))
>>> z
-3.4j
>>> w
(-0-3.4j) So I'll add infj and nanj if the consensus is that they're useful. |
That wouldn't help with the str(), though, unless you're proposing to change that, too. |
I don't understand why w ends up having -0 as the real part. For floats, at On Tue, Jan 13, 2015 at 9:27 AM, Mark Dickinson <report@bugs.python.org>
|
Because "-3.4j" is interpreted as "-complex(0, 3.4)". |
Unfortunately not: something like this is fairly inescapable. The problem is that when you do (for example) 5 - 6j you're in effect subtracting complex(0.0, 6.0) from complex(5.0, 0.0): you've invented a real part of 0.0 for the second term and an imaginary part of 0.0 for the first term. And since 0.0 is *not* an identity for addition (-0.0 + 0.0 is 0.0, not -0.0, under the usual rounding modes), signs of zeros tend to get lost. So the safe way to construct a complex number is to avoid any actual arithmetic by using complex(real_part, imag_part) rather than real_part + imag_part*1j. I rather like Serhiy's idea of making the complex repr have this form, except that the change would probably break existing code. (And the str should really stay in the current expected human-readable format, so the problems with infj and nanj don't go away.) Another possible "fix" is to introduce a new 'imaginary' type, such that the type of an imaginary literal is now 'imaginary' rather than 'complex', and arithmetic operations like addition can special-case the addition of a float to an 'imaginary' instance to produce a complex number with exactly the right bits. The C standardisation folks already tried this: C99 introduces optional "imaginary" types and a new _Imaginary keyword, but last time I looked almost none of the popular compilers supported those types. (I think Intel's icc might be an exception.) While this works as a technical solution, IMO the cure is worse than the disease; I don't want to think about the user-confusion that would result from having separate "complex" and "imaginary" types. |
This type should exist only at compile time. Peephole optimizer should replace it with complex after folding constants. Or may be repr() (and str()) should keep zero real part if imaginary part is negative and output period if real part is zero. For now: >>> z = complex(0.0, 3.4); z; eval(repr(z))
3.4j
3.4j
>>> z = complex(0.0, -3.4); z; eval(repr(z))
-3.4j
(-0-3.4j)
>>> z = complex(-0.0, 3.4); z; eval(repr(z))
(-0+3.4j)
3.4j
>>> z = complex(-0.0, -3.4); z; eval(repr(z))
(-0-3.4j)
-3.4j But all this perhaps is offtopic here. |
OK, let's not try to resolve that issue, we can just note it in the docs. On Tue, Jan 13, 2015 at 11:38 AM, Serhiy Storchaka <report@bugs.python.org>
|
But then after doing "x = 5j", "-0.0 + 5j" and "-0.0 + x" would have different values. Yuck!
Sorry, I don't see how this helps. What do you want the repr of (for example) "complex(-0.0, 5.0)" to be, and why? What about the cases with 0 in the imaginary part? |
Okay, fair enough. |
Ah, it doesn't help in this case. It helps only when the imaginary part is negative. >>> eval('(-0.0-5j)')
(-0-5j)
>>> eval('(-0-5j)')
-5j |
Here's a patch that adds inf, infj, nan and nanj. |
New changeset 4b25da63d1d0 by Mark Dickinson in branch 'default': |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: