Search
Do I have to cast double to cdouble everywhere?
Aug 06, 2010
John Travers
Aug 06, 2010
bearophile
Aug 07, 2010
Don
```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

```
```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
```
```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.