Fortran数字格式

BCa*_*olo -1 double fortran loops rounding

我使用的是英特尔®VisualFortran编译器专业版11.1,当我运行此代码时

program Console1
implicit none

real(8), parameter:: iterations = 1000.d0
real(8), parameter:: maximum = 0.02d0

integer, parameter:: outfile=1

real(8) force, dforce
integer i

dforce = maximum/iterations
force = 0.d0

open (unit=outfile,file="results.txt",action="write",status="replace")
do i=0,int(iterations)
    write(outfile, *) force
    force = force+dforce
enddo
close(outfile)

endprogram
Run Code Online (Sandbox Code Playgroud)

文件results.txt是乱七八糟的,最终的数字是1.999999999999952E-002而不是2.d-2

我做错了什么?谢谢!

Wil*_*cat 9

不是了!浮点数不是实数的精确表示.期.

每个计算机科学家应该知道的浮点运算.