Non-constant expression where constant expression required. 
Author Message
 Non-constant expression where constant expression required.

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

Quote:
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?

Thank you in advance.



Tue, 10 Mar 2009 03:26:44 GMT  
 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

lnx:scratch : pgf95 testit.f90
lnx:scratch : a.out
  (1.000000000000000,3.000000000000000)

cheers,

paulv

p.s. lnx:scratch : pgf95 -V
      pgf95 6.0-5 32-bit target on x86 Linux

--
Paul van Delst             Ride lots.

Ph: (301)763-8000 x7748
Fax:(301)763-8545



Tue, 10 Mar 2009 03:40:19 GMT  
 Non-constant expression where constant expression required.

Quote:
>      x = cmplx(1.0_dp, beta )

but ..... you've to be very careful not to get the kind wrong

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

is really what you want to use..

Joost



Tue, 10 Mar 2009 03:43:44 GMT  
 Non-constant expression where constant expression required.

Quote:
>      x = cmplx(1.0_dp, beta )

but ..... you've to be very careful not to get the kind wrong

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

is really what you want to use..

Joost



Tue, 10 Mar 2009 03:43:51 GMT  
 Non-constant expression where constant expression required.

Quote:

>>      x = cmplx(1.0_dp, beta )

> but ..... you've to be very careful not to get the kind wrong

>   x = cmplx(1.0_dp, beta, KIND=dp)

> is really what you want to use..

> Joost

...which I count as the biggest gotcha in the intrinsic routines. Not at all
obvious, this one.


Tue, 10 Mar 2009 03:46:13 GMT  
 Non-constant expression where constant expression required.

Quote:

>>      x = cmplx(1.0_dp, beta )

> but ..... you've to be very careful not to get the kind wrong

>   x = cmplx(1.0_dp, beta, KIND=dp)

> is really what you want to use..

Yes, you're correct. I did recall something about this hence my "I'm too lazy" caveat.

To the OP, do what Joost says. :o)

cheers,

paulv

--
Paul van Delst             Ride lots.

Ph: (301)763-8000 x7748
Fax:(301)763-8545



Tue, 10 Mar 2009 03:49:31 GMT  
 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



Tue, 10 Mar 2009 03:51:30 GMT  
 Non-constant expression where constant expression required.

Quote:

> I have a strange problem. I cannot assign a complex variable with the
> usage of a real ones:
...
>     x = (1.0_8, beta )

Others have given you the solution. Let me explain the problem. The
syntax you are using above is the syntax for a complex constant. You
can't have a constant built up from a variable. The (x,y) thing is not
some kind of general constructor for complex values; that's what the
cmplx fucntion is. (x,y) is only for constants. And yes, there would be
problems with making it general; I don't even recall what they are
off-hand, but there are places where it would not work.

--
Richard Maine                     | Good judgment comes from experience;
email: my first.last at org.domain| experience comes from bad judgment.
org: nasa, domain: gov            |       -- Mark Twain



Tue, 10 Mar 2009 04:30:55 GMT  
 Non-constant expression where constant expression required.
...

Quote:
>> but ..... you've to be very careful not to get the kind wrong

>>   x = cmplx(1.0_dp, beta, KIND=dp)

>> is really what you want to use..
...
> ...which I count as the biggest gotcha in the intrinsic routines. Not
> at all obvious, this one.

This is why I don't even use the CMPLX intrinsic anymore.
I create a defined prefix operator (called .i.) that's generic to
both single and double precision (and any other real KIND
the compiler supports).  It's meaning is to multiply its operand
by the square root of negative one and it returns a COMPLEX
result of the same KIND as its REAL operand.

With this, the above would be written:

   x = 1.0_dp + .i. beta

--
J. Giles

"I conclude that there are two ways of constructing a software
design: One way is to make it so simple that there are obviously
no deficiencies and the other way is to make it so complicated
that there are no obvious deficiencies."   --  C. A. R. Hoare



Wed, 11 Mar 2009 02:33:29 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. PGF90-S-0087-Non-constant expression where constant expression required

2. PGF90-S-0087-Non-constant expression where constant expression required

3. Lint on a non-constant expression - harmful?

4. optimizing evaluation of constant expressions

5. constant expressions, parameters, `define

6. constant expressions, parameters, `define

7. '87 LRM - constant expression on port

8. Salford compiler: Assigning a value to a constant expression is invalid

9. constants (literal, parameter, expression) in F77

10. complex constants and expression syntax extension a la Mathematica

11. constant expressions & compilers

12. Constant expressions and mathematical functions

 

 
Powered by phpBB® Forum Software