- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
I have a NaN result trying to compute a complex number.
Here is a Minimal Working Example:
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 :
Thanks for your help
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
1 Solution
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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)
Convert the base to complex before raising it to the power:
(cmplx(-1.0))**(-2.25)
Link Copied
3 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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)
Convert the base to complex before raising it to the power:
(cmplx(-1.0))**(-2.25)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
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.

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page