Fixing the imaginary/complex mess
Lars T. Kyllingstad
public at kyllingen.NOSPAMnet
Mon Jun 13 05:00:03 PDT 2011
On Mon, 13 Jun 2011 12:36:27 +0100, Robert Clipsham wrote:
> On 12/06/2011 23:37, bearophile wrote:
>> Andrei:
>>
>>> Don, instead of this fix, could be at best phase everything built-in
>>> about complex out?
>>
>> Two usages of complex numbers in the RosettaCode site.
>>
>> Acommon place where you find complex numbers is to plot
>> Mandelbrot/Julia sets: http://rosettacode.org/wiki/Mandelbrot_set#D
>>
>> import std.stdio, std.math;
>>
>> void main() { enum maxIter = 1000; foreach (y; -39 .. 39) { foreach (x;
>> -39 .. 39) { auto c = y/40.0 - 0.5 + x/40.0i, z = 0.0 + 0.0i, i = 0;
>> for (; i< maxIter&& abs(z)< 4; i++) z = z ^^ 2 + c; write(i ==
>> maxIter ? '#' : ' '); } writeln(); } }
>>
>>
>>
>> Version using std.complex:
>>
>> import std.stdio, std.complex;
>>
>> void main() { enum maxIter = 1000; foreach (y; -39 .. 39) { foreach (x;
>> -39 .. 39) { auto c = Complex!double(y/40.0 - 0.5, x/40.0), z =
>> Complex!double(0, 0), i = 0; for (; i< maxIter&& z.abs()< 4; i++) z
>> = z ^^ 2 + c; write(i == maxIter ? '#' : ' '); } writeln(); } }
>>
>>
>> I think it's worth adding to std.complex module few examples of usage,
>> plus a complex() function so you are allowed to write (the imaginary
>> part defaults to zero if the real part is set to zero): auto z =
>> complex(5);
>
> I seemed to think the plan for complex numbers was to do what happened
> with associative arrays, that is, keep the language syntax, but have the
> feature implemented in the library. Is this not the case?
That was my understanding as well, which is why I never added a complex()
helper function.
It is, however, my humble opinion that we should just get rid of the
complex literals. (Otherwise, std.complex would have to be moved into
druntime.)
-Lars
More information about the Digitalmars-d
mailing list