gfortran - 未指定的十进制长度允许实际输出吗? [英] gfortran - Is unspecified decimal length allowed for real output?

查看:200
本文介绍了gfortran - 未指定的十进制长度允许实际输出吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法来格式化输出的实数,使宽度和小数部分都没有指定?这是可能的ifort通过执行以下操作:
$ 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的答案后,首先想到的是将实数编写成一个字符串,然后使用它:

< ($,$),$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $' trim(adjustl(cnum))

相同的输出作为最好的解决方案,但它稍微复杂一点,它不提供相当多的控制,但它接近。


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:

write (*, '(F)') num

...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:

real :: num
num = 3.14159
write (*, '(F0.0)') num

...the output is just 3.. I know I could specify a decimal value greater than zero, but then I am liable to have undesired extra zeros printed.

What are my options? Do I have any?

解决方案

Best solution:

It turns out that the easiest solution is to use the 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

yields this:

pi = 3.14159012

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:

character(len=20) :: cnum
write (cnum, *), num
write (*, '(a5,a)') word, trim(adjustl(cnum))

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆