Hi all,
I wrote a fortran module called arithmetic_mod in order to handle different type of input values such as integer, real and complex as follows:
module arithmetic_mod
implicit none
private
public :: adder
interface adder
procedure int_add, real_add, complex_add
end interface adder
contains
! Adder Functions
integer function int_add(n_1, n_2)
integer, intent(in) :: n_1, n_2
integer :: sum
sum = n_1 + n_2
end function int_add
real function real_add(n_1, n_2)
real, intent(in) :: n_1, n_2
real :: sum
sum = n_1 + n_2
end function real_add
complex function complex_add(n_1, n_2)
complex, intent(in) :: n_1, n_2
complex :: sum
sum = n_1 + n_2
end function complex_add
end module arithmetic_mod
I tried to test the module in a separate file as follows:
program test
use arithmetic_mod
implicit none
integer :: i_num_1 = 2, i_num_2 = 3
real :: r_num_1 = 25.0, r_num_2 = 35.0
complex :: com_num_1 = (7, 8), com_num_2 = (5, -7)
print "(a8,i1)", "2 + 3 = ", adder(i_num_1, i_num_2)
print "(a14,f4.1)", "25.0 + 35.0 = ", adder(r_num_1, r_num_2)
print*, adder(com_num_1, com_num_2)
end program test
and i compiled these files using commands below:
gfortran -c arithmetic_mod.f90 test_arithmetic.f90
gfortan arithmetic_mod.o test_arithmetic.f90
But when i ran ./a.out , i got output below:
2 + 3 = 5
25.0 + 35.0 = 60.0
(NaN, NaN)
The questions is why complex adder returns NaN instead of correct output(12.000000, 1.000000)?