Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Problem computing complex number

FlyingHermes
New Contributor I
891 Views
Hi,
I have a NaN result trying to compute a complex number.
Here is a Minimal Working Example:

[fortran]program ProgComplex
  implicit none
  complex(8) :: x
  x  =  (5.0,8.0)
  write(6,"('ProgComplex: real(x) = ',f5.2,3x,'aimag(x) = ',f5.2)") real(x), aimag(x)
  x  =  CMPLX((-1.0)**(2.25))
  write(6,"('ProgComplex: real(x) = ',f5.2,3x,'aimag(x) = ',f5.2)") real(x), aimag(x)
end program ProgComplex[/fortran]
This program returns the following two lines:
[plain]ProgComplex: real(x) =  5.00   aimag(x) =  8.00
ProgComplex: real(x) =   NaN   aimag(x) =  0.00[/plain]

The 1st 'write' statement prints to the screen the real and imaginary parts of the complex number x = 5 + 8i.
The returned value are the ones expected.

The 2nd 'write' statement prints the real and imaginary parts of the complex number x = (-1)^(2.25).
Using Matlab, I found

x = (-1)^(2.25) = 0.7071 + 0.7071i

However, my fortran program cimpiled with ifort gives me a NaN value for the real part.

This problem is not compiler -dependent since the gfortran compiler generate that same error, giving a more explicit message :
[plain]Error: Raising a negative REAL at (1) to a REAL power is prohibited[/plain]
As a result, how can a compute the number x = (-1)^(2.25) ?
Thanks for your help
0 Kudos
1 Solution
mecej4
Honored Contributor III
891 Views
This problem can be encountered in even simpler situations, such as finding the cube-root of -1 or -8.

Convert the base to complex before raising it to the power:

(cmplx(-1.0))**(-2.25)

View solution in original post

0 Kudos
3 Replies
FlyingHermes
New Contributor I
891 Views
Just to confim that this issu is not comiled-dependent, looking at the Fortran 2003 Standard (paragraph 7.1.8.), I've found that "Raising a negative-valued primary of type real to a real power is prohibited."

This leads me to the following problem: How can I compute x = (-1)^(2.25) in Fortran ?
Since, the solution exist and is complex, it should be possible, isn't it.


0 Kudos
mecej4
Honored Contributor III
892 Views
This problem can be encountered in even simpler situations, such as finding the cube-root of -1 or -8.

Convert the base to complex before raising it to the power:

(cmplx(-1.0))**(-2.25)
0 Kudos
FlyingHermes
New Contributor I
891 Views
Ok, thanks.
I feel shameful, the solution is so simple.
I'm really not used to deal with complex number in fortran...
Anyway, your anwser solved my problem.
Thanks angain.
0 Kudos
Reply