(in)exactness of complex numbers

Bengt Richter bokr at accessone.com
Wed Aug 1 19:17:52 EDT 2001


On Wed, 01 Aug 2001 13:32:52 GMT, ullrich at math.okstate.edu (David C. Ullrich) wrote:

>On Wed, 01 Aug 2001 14:45:14 +1200, Greg Ewing
><greg at cosc.canterbury.ac.nz> wrote:
>
>>"David C. Ullrich" wrote:
>>> 
>>> But why should the real and imag of a complex be required to be
>>> floats in the first place? Why  shouldn't they be allowed to be
>>> floats or rationals or integers?
>>
>>A complex number, in the mathematical sense, is 
>>not a pair of other numbers -- that's just one 
>>way of *representing* a complex number.
>
>Well thanks for clarifying _that_. Duh.
>
>(Not that it makes any difference here, but
>since you undertstand the math so much better
>than I do: Exactly what definition of
>"complex number" do you have in mind here?
>The _standard_ definition _is_ "pair of
>real numbers".)
>
Well, he *is* trying to pay attention to representation issues,
which I think is good ;-) but it sounded like the horse wasn't
quite hitched right, the way I read it.
[...]

It looks to me like the problem is that the Python internal representation
in C of complex actually *is* two doubles, not two python numbers.

Probably because for using external libraries it makes it a lot faster if you
don't first have to convert Python's numeric objects and make a pair
of doubles -- especially since you probably always get doubles back.

I don't think there is there's any reason you *couldn't*
use general numbers (excluding complex ;-) as the real and imag parts.
I'm guessing it would just be a lot of mostly boring rework programming
and there's hotter things now for the internals people (of which I'm not one,
in case the thought occurred. Not that I wouldn't like to be at some point ;-).
The trick would be to preserve speed and still get generality. I think it
could be done, but is that where you want to allocate the people who both
can and are in a position to do that?

Anyway, the abstract aspects of things are more interesting.
At least until I'm hungry ;-)

from complexobject.h:

typedef struct {
    double real;
    double imag;
} Py_complex;
...
/*
PyComplexObject represents a complex number with double-precision
real and imaginary parts.
*/

typedef struct {
    PyObject_HEAD
    Py_complex cval;
} PyComplexObject;     





More information about the Python-list mailing list