INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Complex power with float exponent resulting in NaN

Complex power with float exponent resulting in NaN

Complex power with float exponent resulting in NaN

(OP)
GNU Fortran, MinGW 4.8.1

IDE: Code Blocks 13.12

File ext: .f90

CODE --> fortran

program hello
    implicit none
    COMPLEX rk
    rk=2*0
    rk = rk **4 ! Ok
    rk = rk **4. ! NaN
    end program 

How to make powers of complex with float exponent without resulting in NaN?

Compiled with mingw32-gfortran.exe 4.8.1, with flags for no line length limit (free/fixed forms).

Why? I'm refactoring a fortran code to use it from a c# program, and, although the power with the float works on the environment the code was created (visual studio, not sure about compiler, intel maybe), it does not work on my environment (code blocks, mingw). So, even if in this case i can just use an integer, it might not be so in the future.

Thanks

RE: Complex power with float exponent resulting in NaN

Seems to work OK on gfortran 4.8.0.

If you're using C# with DLLImport, the Fortran functions need to use the stdcall calling convention.

RE: Complex power with float exponent resulting in NaN

(OP)
This code is run on code blocks, as a fortran application project. Although I intend to use it as a dll to a C# program, currently they are completely unrelated.
Also, the code runs fine, the error shows on PRINT only. Try this, and check the output:

CODE --> fortran

program hello
    implicit none
COMPLEX rk

rk=2*0
rk = rk**4
print *, 'rk',rk
rk = rk **4.
print *, 'rk',rk

end program 

Compiler call (with gfortran.exe):

CODE -->

gfortran.exe -Jobj\Debug\ -Wall  -g  -ffixed-form -ffree-form    -c "C:\Users\cpuo\Documents\Atividades\2015\InGamma\Projetos CBlocks\Complex_teste\main.f90" -o obj\Debug\main.o 

RE: Complex power with float exponent resulting in NaN

That seems to work

rk ( 0.00000000 , 0.00000000 )
rk ( 0.00000000 , 0.00000000 )

RE: Complex power with float exponent resulting in NaN

Apparently, raising a complex number to a decimal power is not a trivial thing. See this and this.

RE: Complex power with float exponent resulting in NaN

I think that the problem is just related 0**val . If val is an integer value greater that 0, then the result is 0 of course. But if val is a real value, then the compiler may interpret this expression as exp(val*log(0)) which is NaN.

In addition I find really strange the instruction "rk=2*0". It this a mistake ? When one wants to set a variable to zero, one simply writes "rk=0". As rk is declared complex, then the OP wanted perhaps to write something like "rk=(2,0)" (but here also "rk=2" is simpler).

Fran├žois Jacq

RE: Complex power with float exponent resulting in NaN

FJacq: Good observations, both...the log(0) and rk=2*0...even rk=(2,0), does this fly? I know it does when reading from file, but in the program, I thought one must use rk=complx(2,0)...I guess I could write a short program and test it...but I won't winky smile...doing other things, just swinging by here.

gsal

RE: Complex power with float exponent resulting in NaN

@salgerman : the syntax rk=(2,0) is correct

CODE --> fortran

program t
  complex r
  r=(2,0)
  write(*,*) r
end program 

root@pctoutou:/media/sda6/test # gfortran t.f90
root@pctoutou:/media/sda6/test # ./a.out
( 2.00000000 , 0.00000000 )

Fran├žois Jacq

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close