在Fortran中,do循环中的变量类型缺省为整数。但是,我需要将参数采用浮点值的函数的值制作成表格并绘制出来。但是我不知道如何写同样的代码。在C/C++中,for循环可以接受浮点型参数。
我需要绘制的函数如下:

这里,'a‘是一个参数,步长需要选择为0.0001。
发布于 2020-02-25 14:37:56
这里有几种可能的方法。请注意,仅仅因为您可以使用实数for循环变量并不意味着您应该使用它-这是Fortran语言中为数不多的要删除的内容之一,这是有充分理由的!这个例子说明了为什么,比较两种方法的输出
ian-standard@barleybarber ~
$ cat inc.f90
Program inc
Implicit None
Real :: a
Real :: a_lo, a_hi
Real :: da
Integer :: n_points
Integer :: i
Write( *, * ) 'a hi?'
Read ( *, * ) a_hi
Write( *, * ) 'a lo?'
Read ( *, * ) a_lo
Write( *, * ) 'da?'
Read ( *, * ) da
n_points = Int( ( a_hi - a_lo ) / da )
! First way - parallelisable
Write( *, * ) 'First way'
Do i = 0, n_points
a = a_lo + Real( i ) * da
Write( *, * ) a
End Do
! Second way - not parallelisable
Write( *, * ) 'Second way'
a = a_lo
Do While( a <= a_hi )
Write( *, * ) a
a = a + da
End Do
End Program inc
ian-standard@barleybarber ~
$ gfortran -std=f2003 -Wall -Wextra -fcheck=all -O -g inc.f90 -o inc
ian-standard@barleybarber ~
$ ./inc
a hi?
1
a lo?
0
da?
0.1
First way
0.00000000
0.100000001
0.200000003
0.300000012
0.400000006
0.500000000
0.600000024
0.699999988
0.800000012
0.900000036
1.00000000
Second way
0.00000000
0.100000001
0.200000003
0.300000012
0.400000006
0.500000000
0.600000024
0.700000048
0.800000072
0.900000095https://stackoverflow.com/questions/60387687
复制相似问题