在Fortran FMZM多精度库中使用IM_格式显示任意长度的格式化字符串

在Fortran FMZM多精度库中使用IM_格式显示任意长度的格式化字符串,fortran,gfortran,fortran90,arbitrary-precision,fmzm,Fortran,Gfortran,Fortran90,Arbitrary Precision,Fmzm,我正在查看第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 co

我正在查看第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\u FORM
函数来显示超过200个字符的大整数。在我的情况下,用IM形式代替上面的FM形式

下面,我看到一份声明:

character(200) :: str
还有一些巧妙的格式:

str = IM_format( 'i200', result )   !<----- convert BigInt to string
print *, n, trim( adjustl(str) )    !<----- print huge integers
我如何声明我的
字符(?):str
变量和我的
IM\u表单
格式,以便我能够容纳编译时未知的可能大得多的输出?让我去猜一个很大的数字吗

更新地址评论 我在FMZM任意精度库的上下文中处理分配和格式字符串,这与标记为重复的问题无关

改变

   character(2000)       :: 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

通过@francescalus的轻推,我以如下方式解决了问题:

   ! 
   character (len=:), allocatable :: str
   character(len=1024) :: fmat
   !
   res = mygetlargenumberfunction(n)      ! call the function
   lenr = log10(TO_FM(res))+1             ! size the string
   allocate(character(len=lenr) :: str)   ! now allocate the string itself
   write (fmat, "(A5,I0)") "i", lenr      ! create the format string
   call im_form(fmat, res, str)           ! do the call
   print*, trim( adjustl(str))

可能的副本。具体来说,检查答案,因为它显示了可分配字符变量的语法,并说明在现代编译器中,您甚至不需要在赋值之前分配它。我认为在调用FM_格式('XXXXX',res,str)中指定格式字符串的要求使这有所不同,对吗?在将其作为参数传递之前,您可能仍然需要分配现在可分配的字符变量。谢谢,@francescalus,但是如果我知道字符串将被赋予结果调用的动态特性,我将在编译时指定它。FMZM似乎没有让我反省如何询问“如果这个大整数是字符串,它会有多长?”以传递给分配步骤和格式字符串。您可以在运行时而不是在编译时使用延迟长度(allocatable)变量来确定。以10为基数的对数近似确定它。
GNU Fortran (Ubuntu 7.3.0-27ubuntu1~18.04) 7.3.0
   ! 
   character (len=:), allocatable :: str
   character(len=1024) :: fmat
   !
   res = mygetlargenumberfunction(n)      ! call the function
   lenr = log10(TO_FM(res))+1             ! size the string
   allocate(character(len=lenr) :: str)   ! now allocate the string itself
   write (fmat, "(A5,I0)") "i", lenr      ! create the format string
   call im_form(fmat, res, str)           ! do the call
   print*, trim( adjustl(str))