我写了一个利用fortran程序读取文件数据的程序,发现数据的实数小数点后的数据总是不对呢?请大家帮忙看看啊,先谢谢了!
程序如下:
[Fortran] 纯文本查看 复制代码
PROGRAM t1
IMPLICIT NONE
INTEGER :: number_nodes
REAL, DIMENSION(6) :: info_nodes
INTEGER :: ierror
INTEGER :: i,j
! open the file
OPEN (UNIT=8, FILE = 'nodes1.dat', STATUS='OLD', ACTION = 'READ', IOSTAT=ierror)
! read integers
READ (8,*) number_nodes
! read nodes
DO i = 1,number_nodes
READ(8,*) info_nodes(i)
END DO
WRITE(*,*) 'number of nodes is', number_nodes
DO i = 1,number_nodes
WRITE(*,*) 'node ', i , 'is ', info_nodes(i)
END DO
CLOSE(UNIT=8)
END PROGRAM t1
其中文件 nodes1.dat的格式为:
6
0.0
0.2
0.4
0.6
0.8
1.0
利用ubuntu下的gfortran编译,得到的结果为:
number of nodes is 6
node 1 is 0.00000000
node 2 is 0.200000003
node 3 is 0.400000006
node 4 is 0.600000024
node 5 is 0.800000012
node 6 is 1.00000000
其中node2---nodes5后面为什么总有些莫名奇妙小数呢?
刚学fortran不就,请大家不吝赐教.
|