August 06, 2010
Hi,

I'm investigating if D would be useful to me as a numerical programming language to replace my current mix of fortran and python.

I'm stuck with a problem which seems odd to me:

    cdouble c1;
    c1 = 2.0;

complains:

    Error: cannot implicitly convert expression (2) of type double to cdouble

The only way I can find to solve this is by doing:

    c1 = cast(cdouble)2.0;

This will drive me crazy, many numeric codes need to multiply complex numbers, or assign to them, with real numbers. This problem also occurs with imaginary numbers.

Is there a simple solution to this without covering all my code with casts?

Thanks for any help!

Regards,
John

August 06, 2010
John Travers:
>     c1 = cast(cdouble)2.0;

Casts are bad, better to avoid them when possible.
You can do this:

void main() {
  cdouble c1;
  c1 = 2.0 + 0i;
}

But complex numbers will be removed from D, they will become partially library ones (imaginary ones will probably just removed). I think they will hopefully keep the same good enough syntax.

Bye,
bearophile
August 07, 2010
John Travers wrote:
> Hi,
> 
> I'm investigating if D would be useful to me as a numerical programming
> language to replace my current mix of fortran and python.

Welcome! There are quite a few of us who are numerical programmers here, and it's one of D's target areas.

> 
> I'm stuck with a problem which seems odd to me:
> 
>     cdouble c1;
>     c1 = 2.0;
> 
> complains:
> 
>     Error: cannot implicitly convert expression (2) of type double to cdouble
> 
> The only way I can find to solve this is by doing:
> 
>     c1 = cast(cdouble)2.0;
> 
> This will drive me crazy, many numeric codes need to multiply complex numbers,
> or assign to them, with real numbers. This problem also occurs with imaginary
> numbers.

Unfortunately we had to disable implicit casts double->cdouble, because it causes problems with function overloading.

So you need to explicitly add the imaginary part.
c1 = 2.0 + 0.0i;
Top | Discussion index | About this forum | D home