www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - Implicit conversion from complex to real

reply Nick <Nick_member pathlink.com> writes:
Should this really be allowed?

creal c = 3 + 5i;
double d = c;    // d is now 3

Besides being a lossy conversion, it can make it difficult to write a complex
math library, and lead to some silent bugs if there ever comes to BE a library.

For example:
creal exp(creal);
real exp(real);
..
cdouble d = 3 + 4i;
exp(d); // The compiler chokes on this one, so we can't overload names

Another example: If we rename the complex functions (as done in C99) we get
real exp(real);
creal cexp(creal);
..
creal d = 2i;
exp(d); // Typo, should be cexp, but the compiler accepts and exp returns the
wrong answer.

Nick
Aug 01 2004
parent "Matthew" <admin stlsoft.dot.dot.dot.dot.org> writes:
"Nick" <Nick_member pathlink.com> wrote in message
news:cejsno$2g2s$1 digitaldaemon.com...
 Should this really be allowed?

 creal c = 3 + 5i;
 double d = c;    // d is now 3

 Besides being a lossy conversion, it can make it difficult to write a complex
 math library, and lead to some silent bugs if there ever comes to BE a library.
Should require explicit cast IMO
 For example:
 creal exp(creal);
 real exp(real);
 ..
 cdouble d = 3 + 4i;
 exp(d); // The compiler chokes on this one, so we can't overload names

 Another example: If we rename the complex functions (as done in C99) we get
 real exp(real);
 creal cexp(creal);
 ..
 creal d = 2i;
 exp(d); // Typo, should be cexp, but the compiler accepts and exp returns the
 wrong answer.

 Nick
Aug 01 2004