gfortran - 未指定的十进制长度允许实际输出吗? [英] gfortran - Is unspecified decimal length allowed for real output?
问题描述
$ b $ <$> code> write(*,'(F)')num
...但是我明白这个用法是编译器特定的扩展。 Gfortran确实接受了符合标准的0宽度说明符,但在标准和gfortran的文档中找不到有关如何不指定小数部分的任何内容。显而易见的猜测是只使用0,但是这会产生不同的结果。例如,给定这个:
pre $ code $ real $ num $ b $ num = 3.14159
write(*,' (F0.0)')num
...输出只是 3。
。我知道我可以指定一个大于零的十进制数,但是我可能会打印出不需要的额外零。
我有什么选择?我有什么?
最佳解决方案:
事实证明,最简单的解决方案是使用 g
说明符(g代表广义编辑)。这接受 0
来表示未指定/处理器相关的宽度,这正是我想要的。这样做比保留整个格式( write(*,*)
)更好,因为您仍然可以控制输出的其他部分,例如:
real :: num
字符(len = 10):: word
num = 3.14159
word ='pi ='
write(*,'(a5,g0)')word,num
得到这个:
pi = 3.14159012
感谢 Vladimir F 的想法(见 我今天早上第一个想到在看到High Performance Mark的答案后,首先想到的是将实数编写成一个字符串,然后使用它: 相同的输出作为最好的解决方案,但它稍微复杂一点,它不提供相当多的控制,但它接近。 Is there a way to format a real number for output such that both the width and decimal parts are left unspecified? This is possible with ifort by just doing the following: ...but I understand that that usage is a compiler-specific extension. Gfortran does accept the standard-compliant 0-width specifier, but I can't find anything in the standard nor in gfortran's documentation about how to leave the decimal part unspecified. The obvious guess is to just use 0 for that as well, but that yields a different result. For example, given this: ...the output is just What are my options? Do I have any? Best solution: It turns out that the easiest solution is to use the yields this: Thanks to Vladimir F for the idea (seen here). Inferior alternative: My first thought this morning after seeing High Performance Mark's answer was to write the real number to a character string and then use that: It yields the same output as the best solution, but it is a little more complicated and it doesn't offer quite as much control, but it gets close. 这篇关于gfortran - 未指定的十进制长度允许实际输出吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
< ($,$),$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $' trim(adjustl(cnum))
write (*, '(F)') num
real :: num
num = 3.14159
write (*, '(F0.0)') num
3.
. I know I could specify a decimal value greater than zero, but then I am liable to have undesired extra zeros printed.g
specifier ("g" for "generalized editing"). That accepts a 0
to mean unspecified/processor-dependent width, which is exactly what I wanted. This is preferable to leaving the entire format unspecified (write(*,*)
) because you can still control other parts of the output, for example:real :: num
character(len=10) :: word
num = 3.14159
word = 'pi = '
write (*, '(a5,g0)') word, num
pi = 3.14159012
character(len=20) :: cnum
write (cnum, *), num
write (*, '(a5,a)') word, trim(adjustl(cnum))