复杂数字的fortran格式说明符 [英] fortran format specifier for complex number
问题描述
我可以在fortran中为复数指定格式说明符吗?我有一个简单的程序。
Can I specify a format specifier for a complex number in fortran? I have a simple program.
program complx1
implicit none
complex :: var1
var1 = (10,20)
write (*,*) var1
write (*,'(F0.0)') var1
write (*,'(F0.0,A,F0.0)') real(var1), ' + i ' , aimag(var1)
end program complx1
输出:
Output:
( 10.0000000 , 20.0000000 )
10.
20.
10. + i 20.
我想为<$ c $使用内置格式c> a + bi 加上一些格式说明符,而不是手动(程序的最后一行)。显然 F0.0
不起作用。任何想法?
I wanted to use inbuilt format for a+bi
with some format specifier, instead of one did manually (second last line of program). Obviously F0.0
did not work. Any ideas?
编辑:
我不认为这是重复的帖子:在fortran中编写复杂矩阵,它表示使用 REAL
和 AIMAG
函数。我已经使用过这些函数,并且想知道是否有内置的格式可以完成这项工作。
I don't think this is a duplicate of post: writing complex matrix in fortran, which says to use REAL
and AIMAG
functions. I already used those functions and wondering whether there is an inbuilt format that can do the work.
现有的,基本满意的答案。一个格式字符串,如
An addendum to @francescalus' existing, and mostly satisfactory, answer. A format string such as
fmt = '(F0.0,SP,F0.0,"i")'
应该会导致一个复数在真实和虚部之间显示正确的符号;无需摆弄字符串以在那里获得加号。
should result in a complex number being displayed with the correct sign between real and imaginary parts; no need to fiddle around with strings to get a plus sign in there.
这篇关于复杂数字的fortran格式说明符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!