Fortran Coder

查看: 7853|回复: 2

[数值问题] 小数部位该为0却不是0

[复制链接]

2

帖子

1

主题

0

精华

新人

F 币
18 元
贡献
11 点
发表于 2018-2-1 15:30:05 | 显示全部楼层 |阅读模式
程序中的aa和C1数组均为实型,其中,C1数组在后续组成线性方程组的系数矩阵。问题出现在:Fortran中输出的aa数组其小数位都为0,而C1数组其小数位不为0;用matlab得到C1数组的结果小数位是等于0的。是不是跟实数的标准形式有关,该如何解决?在采用Fortran来求解线性方程组时,这一现象会不会影响计算结果(尝试了一下,结果跟预期吻合很不好)?谢谢
1517468789(1).jpg

2

帖子

1

主题

0

精华

新人

F 币
18 元
贡献
11 点
 楼主| 发表于 2018-2-1 15:43:23 | 显示全部楼层
下面是相应地代码:
[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

490

帖子

4

主题

0

精华

大宗师

F 币
3298 元
贡献
1948 点

水王勋章元老勋章热心勋章

发表于 2018-2-1 16:43:16 | 显示全部楼层
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.


您需要登录后才可以回帖 登录 | 极速注册

本版积分规则

捐赠本站|Archiver|关于我们 About Us|小黑屋|Fcode ( 京ICP备18005632-2号 )

GMT+8, 2024-3-29 00:55

Powered by Tencent X3.4

© 2013-2024 Tencent

快速回复 返回顶部 返回列表