classification
Title: Strange behaviour when multiplying imaginary inf by 1
Type: behavior Stage: resolved
Components: Versions:
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Alex Monk, mark.dickinson, steven.daprano
Priority: normal Keywords:

Created on 2015-06-12 13:22 by Alex Monk, last changed 2015-06-12 17:38 by mark.dickinson. This issue is now closed.

Messages (4)
msg245247 - (view) Author: Alex Monk (Alex Monk) Date: 2015-06-12 13:22
alex@alex-laptop:~$ python3
Python 3.4.3 (default, Mar 26 2015, 22:03:40) 
[GCC 4.9.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> infj = complex(0, float("inf"))
>>> infj
infj
>>> infj*1
(nan+infj)
>>> infj*1*1
(nan+nanj)
msg245257 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2015-06-12 14:48
I've found the same behaviour going back to Python 1.5.

I think what happens here is that (0+∞j)*1 evaluates the multiplication by implicitly coercing the int to a complex:

    (0+∞j)*(1+0j)
    => (0*1 + ∞j*1 + 0*0j + ∞j*0j)
    => (0-NAN + ∞j+0j)
    => (NAN + ∞j)

rather than using the "simple" way (1*0 + 1*∞j) => (0+∞j).

The problem here is that while there is no mathematical difference between multiplying by 1 or (1+0j), once you involve NANs and INFs, it does. So even though they give different answers, both methods are correct, for some value of "correct".

I don't see that this is necessarily a bug to be fixed, it might count as a change in behaviour needing to go through a deprecation period first. Perhaps it should be discussed on python-ideas first?

My personal feeling is that Python should multiply a complex by a non-complex in the "simple" way, rather than implicitly converting the int to a complex. Anyone who wants the old behaviour can easily get it by explicitly converting 1 to a complex first.

So I guess this is a +1 to "fixing" this.

(Oh, the same applies to the / operator.)
msg245265 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2015-06-12 17:34
There's no bug here:  as Steven explained, this is simply the result of the usual arithmetic conversions when performing a mixed-type operation.

> Python should multiply a complex by a non-complex in the "simple" way

I think this would just be adding unnecessary complication.  -1 from me.
msg245266 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2015-06-12 17:38
Closing as not a bug;  for the suggestion of changing the mixed-type arithmetic behaviour, I think that's something that should be discussed on the python-ideas mailing list.
History
Date User Action Args
2015-06-12 17:38:52mark.dickinsonsetstatus: open -> closed
resolution: not a bug
messages: + msg245266

stage: resolved
2015-06-12 17:34:24mark.dickinsonsetmessages: + msg245265
2015-06-12 14:48:54steven.dapranosetnosy: + steven.daprano
messages: + msg245257
2015-06-12 13:27:39r.david.murraysetnosy: + mark.dickinson
2015-06-12 13:22:58Alex Monkcreate