Fortran Coder

标题: 小数部位该为0却不是0 [打印本页]

作者: NeilYo    时间: 2018-2-1 15:30
标题: 小数部位该为0却不是0
程序中的aa和C1数组均为实型,其中,C1数组在后续组成线性方程组的系数矩阵。问题出现在:Fortran中输出的aa数组其小数位都为0,而C1数组其小数位不为0;用matlab得到C1数组的结果小数位是等于0的。是不是跟实数的标准形式有关,该如何解决?在采用Fortran来求解线性方程组时,这一现象会不会影响计算结果(尝试了一下,结果跟预期吻合很不好)?谢谢

1517468789(1).jpg (53.7 KB, 下载次数: 309)

1517468789(1).jpg

作者: NeilYo    时间: 2018-2-1 15:43
下面是相应地代码:
[Fortran] 纯文本查看 复制代码
program Compute
    implicit none
    integer,parameter::M=1,N=2
    real,dimension(:,:),allocatable::C1
    integer i,j,k,m1,n1,a1,a2,ETL(M*N,5)
    real aa(4,4)
    allocate(C1((M+1)*(N+1),(M+1)*(N+1)))
    open(unit=15,file='storett3',access='sequential',action='write')
    open(unit=14,file='storeaa3',access='sequential',action='write')
    data aa/4,2,1,2,2,4,2,1,1,2,4,2,2,1,2,4/
    k=0
    do j=1,M
       do i=1,N
          k=k+1
          ETL(k,1)=k
          m1=mod(i,N)
          if(m1==0) then
             n1=j*(N+1)-1
          else
             n1=(j-1)*(N+1)+m1
          end if
          ETL(k,2)=n1
          ETL(k,3)=n1+1
          ETL(k,4)=n1+N+2
          ETL(k,5)=n1+N+1
        end do
    end do   
    do k=1,M*N
       do i=1,4
          do j=1,4
             a1=ETL(k,i+1)
             a2=ETL(k,j+1)
             C1(a1,a2)=C1(a1,a2)+aa(i,j)
          end do
       end do
    end do
    write(14,"(<4>('',f,''))")((aa(i,j),i=1,4),j=1,4)
    write(15,"(<6>('',f,''))")((C1(i,j),i=1,6),j=1,6)
end

作者: pasuka    时间: 2018-2-1 16:43
C1数组没有赋初值,与aa结果不一致又有啥奇怪呢?
-finit-local-zero
-finit-derived
-finit-integer=n
-finit-real=<zero|inf|-inf|nan|snan>
-finit-logical=<true|false>
-finit-character=n
The -finit-local-zero option instructs the compiler to initialize local INTEGER, REAL, and COMPLEX variables to zero, LOGICAL variables to false, and CHARACTER variables to a string of null bytes. Finer-grained initialization options are provided by the -finit-integer=n, -finit-real=<zero|inf|-inf|nan|snan> (which also initializes the real and imaginary parts of local COMPLEX variables), -finit-logical=<true|false>, and -finit-character=n (where n is an ASCII character value) options. Components of derived type variables will be initialized according to these flags only with -finit-derived. These options do not initialize

allocatable arrays
variables that appear in an EQUIVALENCE statement.
(These limitations may be removed in future releases).

Note that the -finit-real=nan option initializes REAL and COMPLEX variables with a quiet NaN. For a signalling NaN use -finit-real=snan; note, however, that compile-time optimizations may convert them into quiet NaN and that trapping needs to be enabled (e.g. via -ffpe-trap).

Finally, note that enabling any of the -finit-* options will silence warnings that would have been emitted by -Wuninitialized for the affected local variables.







欢迎光临 Fortran Coder (http://bbs.fcode.cn/) Powered by Discuz! X3.2