Non-constant expression where constant expression required.

Quote:

> > Hi all,

> > I have a strange problem. I cannot assign a complex variable with the

> > usage of a real ones:

> > program p1

> > implicit none

> > complex(8) x

> > real(8) beta

> > beta = 3.0_8

> > x = (1.0_8, beta )

> > end program p1

> > Compiler writes:

> > PGF90-S-0087-Non-constant expression where constant expression required

> > Why it happens? What should I do if imaginary part of a complex number

> > depends on previous calculations?

> I'm too lazy right now to check the correct and allowed syntax (sorry), but this works:

> x = cmplx(1.0_8, beta )

> as in:

> program p1

> implicit none

> integer, parameter :: dp=kind(1.0d0)

> complex(dp) x

> real(dp) beta

> beta = 3.0_dp

> x = cmplx(1.0_dp, beta )

> print *, x

> end program p1

I suggest adding a KIND argument to cmplx, giving

x = cmplx(1.0_dp, beta,kind=dp)

since the kind type of the result of cmplx is that of the default real

type if the kind argument is absent.

Illustrating this, the output of the program

program p1

implicit none

integer, parameter :: dp = kind(1.0d0)

real(kind=dp) :: beta

beta = 3.0_dp

print*,kind(cmplx(1.0_dp,beta,kind=dp))

print*,kind(cmplx(1.0_dp,beta))

end program p1

is

8

4