【发布时间】:2018-11-19 05:36:08
【问题描述】:
我正在查看documentation for FMZM here,从第 8.(c) 节中,我了解到:
(c) Subroutine FM_FORM does similar formatting, but we supply a character string for
the formatted result. After declaring the strings at the top of the routine, as with
CHARACTER(80) :: ST1,ST2
the WRITE above could become
CALL FM_FORM('F15.6',H,ST1)
CALL FM_FORM('E15.7',T,ST2)
WRITE (*,"(' Step size = ',A,' tolerance = ',A)") TRIM(ST1),TRIM(ST2)
FM_FORM must be used instead of FM_FORMAT when there are more than 200 characters
in the formatted string. These longer numbers usually need to be broken into several
lines.
我需要使用IM_FORM 函数来显示超过 200 个字符的大整数。在我的例子中,用 IM_FORM 代替上面的 FM_FORM。
在this example 之后,我看到了以下声明:
character(200) :: str
还有一些巧妙的格式:
str = IM_format( 'i200', result ) !<----- convert BigInt to string
print *, n, trim( adjustl(str) ) !<----- print huge integers
当我知道我的输出长度少于 200 个字符时,这很棒。但是,由于我使用的是任意精度库,因此我的数字很有可能会大得多。
所以工作形式类似于:
character(2000) :: str
res = mygetlargenumberfunction(n)
call im_form('i2000', res, str)
如何声明我的character(?) :: str 变量和我的IM_FORM 格式,以便可以容纳编译时未知的可能更大的输出?我是不是只能猜测一个非常大的数字?
更新寻址 cmets
我在 FMZM 任意精度库的上下文中处理分配和格式字符串,这与它被标记为重复的问题无关。
变化
character(2000) :: str
到
character (len=:), allocatable :: str
所有其他条件相同,产生
Program received signal SIGSEGV: Segmentation fault - invalid memory reference.
...
Segmentation fault (core dumped)
所以这个建议似乎与 FMZM 不兼容。
使用gfortran -std=f2008 myprogram.F90 和
GNU Fortran (Ubuntu 7.3.0-27ubuntu1~18.04) 7.3.0
【问题讨论】:
-
Allocatable character variables in Fortran 的可能重复项。具体来说,检查this 答案,因为它显示了可分配字符变量的语法,并声明您甚至不需要在现代编译器中分配它之前的分配。
-
我认为在 CALL FM_FORMAT('XXXXX', res, str) 中指定格式字符串的要求有所不同,对吧?
-
在将其作为参数传递之前,您可能仍需要分配现在可分配的字符变量。
-
谢谢,@francescalus,但如果我知道字符串将被赋予 RESULT 调用的动态特性多长时间,我会在编译时指定。 FMZM 似乎没有让我反思如何问“如果它是一个字符串,这个大整数会有多长时间?”传递给分配步骤和格式字符串。
-
您可以在运行时而不是编译时确定延迟长度(可分配)变量。用以 10 为底的对数大致确定它。
标签: fortran gfortran fortran90 arbitrary-precision fmzm