How to ensure same precision from gfortran and ifort compiler? For complex functions, to be specific

82 Views Asked by At

I am using the following code (filename cf.f90) to write values of some complex functions in output file. I am getting different outputs from gfortran and ifort compiler (I use gfortran -O3 -o cf.exe cf.f90 and ifort -O3 cf.f90 -o cf.exe)

program complex_func
  IMPLICIT  NONE
  integer, parameter :: dp = selected_real_kind(15, 307)
Real(dp) x
complex(dp) CF

x = 0.7
call complex_example(x, CF)

end program complex_func


Subroutine complex_example(y, my_CF)

  Implicit None
  integer, parameter :: dp = selected_real_kind(15, 307)
  Real(dp) y
  Complex(dp) my_CF, my_CF2, val(3), sumval(3)
  complex(dp), parameter :: I = (0.0_dp, 1.0_dp)  ! sqrt(-1)
  my_CF = (I+1) * exp(I*y) / y

  val = [my_CF, my_CF, (0.0_dp, 0.0_dp)]
 
  write(*,*) 'adding complex array :'
  sumval = val + val
  write(*,*) sumval
  
  return
  
end Subroutine complex_example

The output from gfortran is

adding complex array :
              (0.34464148256663812,4.0258854202296295)              (0.34464148256663812,4.0258854202296295)               (0.0000000000000000,0.0000000000000000)

while from ifort is

adding complex array :
              (0.344641482566638,4.02588542022963)              (0.344641482566638,4.02588542022963)               (0.0000000000000000,0.0000000000000000)

From gfortran it's 16 decimal digits and from ifort I get round-off at 14th digit. How do I make sure I get same precision from ifort, too?

Extra information: I am using complex functions in a bigger code, and I get the expected result from that code from gfortran but not ifort.

0

There are 0 best solutions below