amin bk
amin bk

Reputation: 206

Intel Fortran expression returns incorrect result

I have a code as following:

program th
  implicit none

  integer N1
  integer maxi,ei,Nc,ns,na
  real CH1,CH2



  OPEN(unit=1,file='input_file',status="old")
  read(1,*) ns                      !!!
  read(1,*) ei                         !!!!!!!!!!!!!!! 
  read(1,*) maxi!!!!!!!!!!!!!!!!!!!    
  read(1,*) N1!!!!!!!!!!!!!!!!  
  close(unit=1)


  CH1 = 0.07
  CH2 = -0.35


  Na = INT(abs(2.*((N1/2)*CH1 + (N1/2)*CH2)))
  write(*,*) Na,abs(2.*((real(N1)/2.)*CH1 + (real(N1)/2.)*CH2));stop
end program  th

and the input file is

1                      !!!!!!!!!!!
1                          !!!!!!!!!! 
1                   !!!   
1600 

Then I compile it with

ifort -O3 -autodouble t1.f90 -o out

but when I execute it I get 447 for na which is not correct. The correct answer is 448.

Upvotes: 0

Views: 157

Answers (1)

Stefan
Stefan

Reputation: 2518

This problem can be understood by investigating the numbers upto their full precision.

The number 0.07 is 7.0000000298023224E-02 in single precision and 7.0000000000000007E-02 in double precision. The number 800 can be represented exactly in both models.

However, the product of these numbers is approximately 56.000000238418579 and 56.000000000000006, respectively. Due to the available precision, the first gets rounded towards 56 (in single precision) and the second towards 56.000000000000007 (in double precision).

As a result, the calculation in single precision "gains" some precision by rounding in the "right" direction.

Concerning the different behaviour of ifort 16 mentioned by @casey, I guess there are some differences in rearranging the equation or maybe use of excess-precision for intermediate results. Though, this is only a guess.

Upvotes: 2

Related Questions