program hello
real(kind=8) :: x
x=1.000001234567890
Write(*,'(F10.11)') X
end program Hellowrite语句到底是如何工作的?我试过几种组合,但都搞不明白。如果我不知道一个变量有多少个小数呢?我怎么才能把所有的数字打印到最后一个小数点呢?
发布于 2016-09-03 01:51:24
下面的程序将把浮点数打印到标准输出,而不会损失任何精度。
program main
use ISO_Fortran_env, only: &
stdout => OUTPUT_UNIT, &
compiler_version, &
compiler_options
! Explicit typing only
implicit none
! Variable declarations
integer, parameter :: SP = selected_real_kind(p=6, r=37)
integer, parameter :: DP = selected_real_kind(p=15, r=307)
real (SP) :: single
real (DP) :: double
single = 1.000001234567890_SP
double = 1.000001234567890_DP
write( stdout, '(e13.6e2)') single
write( stdout, '(e23.15e3)') double
write( stdout, '(/4a/)') &
' This file was compiled using ', compiler_version(), &
' using the options ', compiler_options()
end program main请注意如何使用种类参数SP和DP以可移植的方式控制精度。此程序产生以下结果:
0.100000E+01
0.100000123456789E+001
This file was compiled using GCC version 6.1.1 20160802 using the options -mtune=generic -march=x86-64 -std=f2008ts发布于 2016-09-03 18:05:44
你的主要问题是在'X.Y‘中,X是总长度,Y是小数点右边的长度。所以X> Y,在你的例子中是2...应该是"13.11“。
PROGRAM Hello
IMPLICIT NONE
REAL(kind=8) :: x !Some suggest DOUBLE
x=1.000001234567890
WRITE(*,5) x
5 FORMAT('x=',F14.11)
WRITE(*,6) x !This make be better for you...
6 FORMAT('x=',0PE22.11) !This make be better for you...
END PROGRAM Hellohttps://stackoverflow.com/questions/39297096
复制相似问题