Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Complex power with float exponent resulting in NaN

Status
Not open for further replies.

Gugh

Programmer
Sep 16, 2015
2
BR
GNU Fortran, MinGW 4.8.1

IDE: Code Blocks 13.12

File ext: .f90

Code:
    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
 
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.
 
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:
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
 
That seems to work

rk ( 0.00000000 , 0.00000000 )
rk ( 0.00000000 , 0.00000000 )
 
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
 
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 ;-)...doing other things, just swinging by here.

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

Code:
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top