Remove complex and imaginary types?

bearophile bearophileHUGS at lycos.com
Mon Jan 7 01:56:06 PST 2008


Bill Baxter:
> but for a programming language it's bad practice to
> have a lot of magic literals in your code anyway.  I.e.
> 
>     creal rot45 = creal(.707, 707);
>     creal xr = rot45 * x;
> 
> is better than
>     creal xr = (.707 + 0.707i) * x;
> 
> in my opinion.  As long as complex constants can still be put in static 
> arrays, it should be ok.

Python has the literals too:

>>> complex(5)
(5+0j)
>>> (6 + 2j - 1 + 3j) / 3j
(1.6666666666666665-1.6666666666666665j)
>>> (complex(6, 2) - complex(1, 3)) / complex(0,3)
(-0.33333333333333331-1.6666666666666665j)


But maybe imaginary literals aren't necessary:

C++
complex<long double> a = 5;
complex<long double> b(0,7);
c = a + b + complex<long double>(0,7);

Current D:
creal a = 5;
ireal b = 7i;
c = a + b + 7i;

Alternative:
auto a = creal(5); // == creal(5,0)
auto b = ireal(7);
c = a + b + ireal(7);


C++:
c = (complex<double>(6,2) + complex<double>(-1,3)) / complex<double>(0,3);

Current D:
c = (6 + 2i - 1 + 3i) / 3i;

Alternative:
c = (creal(6,2) + cdouble(-1,3)) / idouble(3);

Bye,
bearophile



More information about the Digitalmars-d mailing list