std.complex

Lars T. Kyllingstad public at kyllingen.NOSPAMnet
Tue May 11 09:08:17 PDT 2010


On Tue, 11 May 2010 15:08:07 +0000, eles wrote:

> Maybe I am wrong, but my feeling is that if the complex numbers were a
> native type (like creal&co. are now), then it would have been possible
> to have a dedicated formatting (just like %f, %d and %s are) for
> writefln.
> 
> Putting the type into the library seems to forbid some very nice things:
> 
>  - initializing with "Complex!double z=1+2i" or "Complex!double z=1+2*i"

I don't think there will still be a special syntax for complex literals 
when the built-in types are deprecated.  I'd guess not.  It is, however, 
likely that creal etc. will be aliases for Complex!real etc.  And this 
doesn't look that bad:

  auto z = cdouble(1,2);


>  or or
> "Complex!double z=2.23*exp(1.107*i)" syntax (which is very practical) -

The following does just that, and IMO it looks a lot better:

  auto z = fromPolar(2.23, 1.107);


> even in C99 one could write "complex double z=1+2*I" (but "I" is a
> defined symbol) - instead of "Complex!double(2,3)"

If you want that, just define

  immutable I = Complex!float(0, 1);

Perhaps it would be an idea to put that in std.complex, but I'm not 
convinced.


>  - lack of support for formatting in writef (although, that should not
>  be
> limited to native types)
>  - formatting in writef/printf is quite versatile for regular use, and
>  could be
> immediately extended to complex numbers (e.g. if one formatting is
> specified, then both real and imaginary parts would be displayed with
> that format), although 2 different complex-number formats should be
> defined: real/imaginary and amplitude/phase

This is exactly what I'm proposing.  Complex numbers don't have to be 
built-in for that.

-Lars


More information about the Digitalmars-d-learn mailing list