I am never going to complain about Python again

Cameron Simpson cs at zip.com.au
Thu Oct 10 17:52:43 EDT 2013


On 10Oct2013 17:48, Neil Cerutti <neilc at norwich.edu> wrote:
> On 2013-10-10, MRAB <python at mrabarnett.plus.com> wrote:
> > 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.

I must say that I read the footnote [2] as a directive to the
programmer. "If you need to do this, a good way is to compare
magnitudes is appropriate."

Cheers,
-- 
Cameron Simpson <cs at zip.com.au>

My life is a never ending battle for the forces of good.
Unfortunately, unlike many other crusaders for righteousness, in my
system of morality, the right thing to do is very often to sit around
reading the paper or watching TV.       - Tim_Mefford <tim at physics.orst.edu>



More information about the Python-list mailing list