英特尔Fortran表达式返回错误的结果

时间:2015-09-29 01:18:24

标签: fortran intel-fortran

我的代码如下:

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

,输入文件为

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

然后我用

编译它
ifort -O3 -autodouble t1.f90 -o out

但是当我执行它时,na得到447这是不正确的。正确答案是448。

1 个答案:

答案 0 :(得分:2)

通过调查数字达到完全精确度,可以理解这个问题。

单精度0.077.0000000298023224E-02,精度为7.0000000000000007E-02。数字800可以在两种模型中完全表示。

但是,这些数字的乘积分别约为56.00000023841857956.000000000000006。由于可用的精度,第一个向56(单精度)舍入,第二个向56.000000000000007舍入(双精度)。

因此,单精度计算通过在“右”方向上舍入来“获得”一些精度。

关于@casey提到的ifort 16的不同行为,我想重新排列等式或者可能使用过度精度来表示中间结果。虽然,这只是猜测。

相关问题