Hiren
Hiren

Reputation: 301

Fortran get complex number from a real number.

I am working on writing a Fortran which has to solve square root and that results in getting a complex number, but Fortran doesn't print it or passes that to another variable. It gives (NaN, 0.000).

This is a dummy code to represent the problem that i am having with the actual code i am working on. If you guys can give me any information that would be helpful. Thank You.

   program test
       IMPLICIT NONE
       COMPLEX X
       REAL a, b, c
       a = 1
       b = 1
       c = 1
       X = sqrt(b - 4*a*c)
       print *, REAL(X), ' - j',-AIMAG(X)
   end program test

Upvotes: 0

Views: 1972

Answers (1)

M. S. B.
M. S. B.

Reputation: 29401

Since a, b and c are all reals, the expression on the right-hand side of the assignment will be calculated in real arithmetic. Assigning it to a complex variable on the left-hand side doesn't change that. If you want the calculation done as a complex value, the easiest way is to declare a, b, and c as complex.

Upvotes: 2

Related Questions