Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!news-1.dfn.de!news.dfn.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Neil Cerutti Newsgroups: comp.lang.python Subject: Re: I am never going to complain about Python again Date: 10 Oct 2013 17:48:16 GMT Organization: Norwich University Lines: 52 Message-ID: References: <52562ee3$0$2931$c3e8da3$76491128@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit X-Trace: individual.net X7lTMjBab0BLVL7oqWjDkQSdiHIv7Yrzo3Ef4LjP+lvXxNk96M Cancel-Lock: sha1:eHjzv8cKmBeaqKNpuwqMyLBHvCo= User-Agent: slrn/0.9.9p1/mm/ao (Win32) Xref: csiph.com comp.lang.python:56593 On 2013-10-10, MRAB wrote: > On 10/10/2013 16:57, Rotwang wrote: >> On 10/10/2013 16:51, Neil Cerutti wrote: >>> [...] >>> >>> Mixed arithmetic always promotes to the wider type (except in >>> the case of complex numbers (Ha!)). >>> >>> r == c is equivalent to r == abs(c), which returns the magintude >>> of the complex number. >> >> What? >> >> >>> -1 == -1 + 0j >> True >> >>> -1 == abs(-1 + 0j) >> False >> >>> 1 == 0 + 1j >> False >> >>> 1 == abs(0 + 1j) >> True >> > Indeed. > > If r is real (float) and c is complex: > > r == c means r == c.real and c.imag == 0.0 Woah. I thought I was going by what the docs say: Python fully supports mixed arithmetic: when a binary arithmetic operator has operands of different numeric types, the operand with the “narrower” type is widened to that of the other, where integer is narrower than floating point, which is narrower than complex. Comparisons between numbers of mixed type use the same rule. [2] The constructors int(), float(), and complex() can be used to produce numbers of a specific type. [...] [2] Not for complex numbers. Instead convert to floats using abs() if appropriate. I guess the "if appropriate" part eluded my eye. When *is* it appropriate? Apparently not during an equal test. >>> 5.0 == abs(3 + 4j) False -- Neil Cerutti