-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
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
str.format() wrongly formats complex() numbers (Py30a2) #45929
Comments
>>> x = complex(1, 2/3)
>>> "{0} {0:.5}".format(x)
'(1+0.666666666667j) (1+0.' The complex number is being formatted as if it were a string and simply |
This really is a feature request -- in Python 2.x there is no formatting I agree it would be neat to have control over complex numbers using the >>> "{0.real:.5}+{0.imag:.5}j".format(z)
'1+0.66667j' |
Maybe this would be a good GHOP task? |
On 2007-12-11, Guido van Rossum wrote:
I thought Python 3 was meant to be an _improvement_:-)
Good point, I'll use that. Thanks! |
On 2007-12-11, Guido van Rossum wrote:
> Guido van Rossum added the comment:
>
> This really is a feature request -- in Python 2.x there is no formatting
> code for complex numbers at all, and "%.5s" % complex(...) does the same
> thing.
>
> I agree it would be neat to have control over complex numbers using the
> same formatting language used for floats; but I note that it's easy
> enough to do this manually, e.g.
>
> >>> "{0.real:.5}+{0.imag:.5}j".format(z)
>
> '1+0.66667j' That's not quite right because it doesn't always handle the sign '1.00000+0.66667j'
>>> "{0.real:.5f}{0.imag:+.5f}j".format(complex(1, -2/3))
'1.00000-0.66667j' |
That's why I didn't close the issue but reclassified it. Or did you expect me to implement it overnight? :-) |
Confirmed in py3k at rev71995. |
I agree this is a feature request. It comes down to: What should the format specifier mini-language for complex numbers look Should it look like the existing mini-language for floats, but have the I'm sure python-ideas could argue over it for ages, but I don't see any |
This sounds clumsy to me. I'd guess that in most uses you'd want the
That doesn't sound unreasonable. But there might need to be some It seems simplest just to tell people to format the real and imaginary |
Mark Dickinson wrote:
I agree, and mostly I was just trying to spark some discussion and show
How about this:
When we document the above approach, we note the way to get full control I guess we should put the docs in with string formatting (since that's |
This sounds good to me. I assume a '+' would still affect
I don't see any problem with dealing with width, alignment For the bits that are disabled (e.g., zero padding), should |
You're correct. It's just zero padding that would be disabled.
I think a ValueError would be best. That way if we decide to give it some |
More specifically, how about allowing widths, and the I suppose that thousands separators should be permitted |
Forgot to reply to this part. Yes, a '+', '-', or ' ' would still affect the real part, but the |
Agreed. It also fits with the way that other non-numeric types seem to >>> format("boris", "030s")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: '=' alignment not allowed in string format specifier |
That sounds correct.
That was my thinking, too. |
I'm also going to disallow the '%' format code. I don't think it makes |
Sounds good to me.
Well, I think it's clear what the numbers would be (just scale both real Just being difficult: I completely agree that '%' should be disallowed |
Two things that haven't come up so far: (1) What about parentheses? The current complex repr and str have I'd suggest leaving them out altogether; except that I have (2) What about zeros? The current repr and str leave out the real |
Mark Dickinson wrote:
The rule is that if that x.__format__('') is equivalent to str(x). All I couldn't find a case with any built-in objects where this really makes format(1+1j, '') -> '(1+1j)' Although I guess if we wanted to, we could say that the empty
I agree. Again, we could say that the empty presentation type is |
It bothers me a little. I see '' as a special case of the empty
This works for me. [about suppressing real zeros...]
Makes sense. Does treating the empty presentation type as special this |
Mark Dickinson wrote:
Me, too.
No. I'm basically finished with it. Before I check it in, I'll attach a |
See the attached patch. Comments welcome. I'm not sure I'm doing the right thing with 'g' and appending zeros:
>>> format(3+4j, '.2')
'(3+4j)'
>>> format(3+4j, '.2g')
'3.0+4.0j'
>>> format(3+0j, '.2')
'(3+0j)'
>>> format(3+0j, '.2g')
'3.0+0.0j'
>>> format(1j, '.2')
'(1j)'
>>> format(1j, '.2g')
'0.0+1.0j' |
Mark Dickinson wrote:
I don't have a problem with trunk's complex.__format__ not agreeing with |
Here's a patch against py3k, with one slight change with non-empty |
With your patch, I'm getting quite strange results when using alignment >>> z = 123+4j
>>> format(z, '=20')
'( 123+ 4j)'
>>> format(z, '^20')
'( 123 +4 j)'
>>> format(z, '<20')
'(123 +4 j)'
>>> len(format(z, '<20'))
43 Is this intentional? I was expecting to get strings of length 20, |
...
No, not intentional. I'll fix it and add some tests. Thanks. |
This is a patch against py3k, including tests in test_complex.py. It |
I also propose to disallow the '=' alignment flag. It means put the Remember, by using .real and .imag, you can achieve this level of |
I think these patches are complete. One for py3k, one for trunk. If no |
With these patches, all tests pass for me both for py3k and trunk. |
I haven't done as thorough a review as I'd like, but both |
Thanks, Mark. I'm not so worried about the code, but more so the tests. As far as the But of course, another set of eyes to review it is always welcome. If Committed in trunk in r72137, and in py3k in r72140. |
One comment on the new complex formatting. I now get (in py3k) >>> from math import pi, e
>>> format(complex(pi,e), '<')
'(3.14159+2.71828j)'
>>> format(complex(pi,e), '')
'(3.14159265359+2.71828182846j)' I understand why this is happening, but again I think that alignment |
Is this suggestion for all types, or just complex? Because float has the >>> format(pi, '')
'3.14159265359'
[38243 refs]
>>> format(pi, '>')
'3.14159' |
Hmm. That also seems wrong to me. So I guess it's a suggestion |
Yes, this is a separate issue. It comes from PEP-3101's specification of |
Thanks. See bpo-5920. |
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: