This is the mail archive of the fortran@gcc.gnu.org mailing list for the GNU Fortran project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: cmplx and complex intrinsics


On Fri, May 04, 2007 at 03:21:22PM -0500, Brian Barnes wrote:
> This is mostly for Brooks, I guess, as it's a documentation thing.
> Does the documentation for the "cmplx" intrinsic mean to imply that the
> return value is of the same "KIND" as the arguments supplied?  If I supply
> double precision (real*8) arguments X and Y, does the documentation as
> written suggest the returned value should be a complex*16?
> 
> http://gcc.gnu.org/onlinedocs/gfortran/CMPLX.html
> 
> I kinda think it does, but in all honestly, I could be misreading it.

It may be that the documentation implies that, but insofar it does, it's
wrong.  I'll write up a patch to make that clearer -- this is a common
error, and we ought to be very explicit about it.  Unless a KIND
argument is supplied, CMPLX returns a result of the default complex
type, regardless of the kind of its arguments.  (This is required by the
Fortran standard, unfortunately -- I think the general consensus is
that it's unfortunate but at this point necessary for backwards
compatibility.)

> I was debugging a program this afternoon (I didn't write the
> offending code, honest), and found that the returned value was only
> complex*8, single precision, when I supplied double precision arguments.
> I solved the problem by using the "complex" intrinsic, which is currently
> undocumented for gfortran (!).  I could have used "dcmplx", but actually
> figured out what was going on by reading g77 docs and I just tried
> "complex" figuring that it would be present in gfortran, which it was.  It
> seems that cmplx and complex in gfortran function exactly as described in
> the g77 docs.
> 
> http://gcc.gnu.org/onlinedocs/gcc-3.4.6/g77/Cmplx-Intrinsic.html
> http://gcc.gnu.org/onlinedocs/gcc-3.4.6/g77/Complex-Intrinsic.html
> 
> I just thought I'd write the list and see what the story was on all this.

Weird.  I could have sworn we had all the intrinsics documented, but
somehow that one does seem to have gotten missed.  How embarassing!  But
thank you for catching it.

> My questions:
> Is "complex" deprecated or obsolete in gfortran?

It's not part of the Fortran standard, so it's not portable to other
compilers.  Thus, it's sort of deprecated for that reason.

> Should I be using dcmplx instead?

Probably not; I'd recommend CMPLX with the optional KIND argument (as
explained below).

> Should documentation for complex be added?

Absolutely.

> Should I expect cmplx to return a double precision value when supplied
> with double precision input, and possibly there is a bug here?

Merely a bug in the documentation, unless you're also using the KIND
argument.

What I would recommend is this idiom:

  c = CMPLX (x, y, KIND (x))

That does exactly what you want, regardless of the whether you give it
single- or double-precision arguments, and is entirely standard Fortran.

- Brooks


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]