complex representation

Robert Kern robert.kern at gmail.com
Mon Jul 7 19:59:48 EDT 2008


DSM wrote:
>  From the nothing-is-so-trivial-it's-not-worth-a-usenet-post file:
> 
> Shouldn't the default representation of complex numbers be like that of
> floats? That is, have a decimal point?
> 
>  >>> 1
>  1
>  >>> 1.0
>  1.0
>  >>> 1j
>  1j
>  >>> 1.0j
>  1j
>  >>> 1.0+1.0j
>  (1+1j)
> 
> 
> In the relevant bit of floatobject.c, there's a comment explaining that
> 1.0 isn't accidental:
> 
>         /* Subroutine for float_repr and float_print.
>            We want float numbers to be recognizable as such,
>            i.e., they should contain a decimal point or an exponent.
>            However, %g may print the number as an integer;
>            in such cases, we append ".0" to the string. */
> 
> ISTM the same reasoning applies equally to complex numbers. 

Not necessarily. The reason that having floats always have a decimal point is 
desirable is to ensure roundtripping through eval(repr(x)). The j is sufficient 
for complexes. There are no complex integers to confuse it with.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco




More information about the Python-list mailing list