Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Loops Fortran:使用实数类型参数执行循环_Loops_For Loop_Fortran - Fatal编程技术网

Loops Fortran:使用实数类型参数执行循环

Loops Fortran:使用实数类型参数执行循环,loops,for-loop,fortran,Loops,For Loop,Fortran,Fortran中do循环中的变量类型默认为整数。但是,我需要将参数采用浮点值的函数的值制成表格并绘制出来。但是我不知道如何为同样的代码编写代码。在C/C++中,for循环可以采用浮点型参数 我需要绘制的函数如下所示: 这里,“a”是一个参数,步长需要选择为0.0001。这里有几种可能的方法。请注意,仅仅因为您可以使用实for循环变量并不意味着您应该这样做-有很好的理由说明为什么这是从Fortran语言中删除的极少数内容之一!这个例子说明了为什么要比较这两种方法的输出 ian-standard@

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

我需要绘制的函数如下所示:


这里,“a”是一个参数,步长需要选择为0.0001。

这里有几种可能的方法。请注意,仅仅因为您可以使用实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.900000095
伊恩-standard@barleybarber ~ $cat公司f90 程序公司 隐式无 真的吗 真的:a_-lo,a_-hi 雷亚尔::达 整数::n_点 整数::i 写(*,*)“你好吗?” 读(*,*)a_hi 写(*,*)“一个lo?” 读(*,*)a_lo 写(*,*)“da?” 读(*,*)da n_点=Int((a_高-a_低)/da) ! 第一种方式-可并行 写(*,*)“第一条路” i=0吗,n_点 a=a_lo+实(i)*da 写(*,*)a 结束 ! 第二种方式-不可并行 写(*,*)“第二条路” a=a_-lo
Do While(a这里有几种可能的方法。请注意,仅仅因为您可以使用实数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.900000095
伊恩-standard@barleybarber ~ $cat公司f90 程序公司 隐式无 真的吗 真的:a_-lo,a_-hi 雷亚尔::达 整数::n_点 整数::i 写(*,*)“你好吗?” 读(*,*)a_hi 写(*,*)“一个lo?” 读(*,*)a_lo 写(*,*)“da?” 读(*,*)da n_点=Int((a_高-a_低)/da) !第一条路-可并行 写(*,*)“第一条路” i=0吗,n_点 a=a_lo+实(i)*da 写(*,*)a 结束 !第二条路-不可并行 写(*,*)“第二条路” a=a_-lo
做一个(a)你能不能只迭代多个步骤并在每个循环步骤中分配一个(i)?在一个浮点上迭代是一个坏主意,在C或C++中。它是从FORTRAN中删除的。原因是,你不能只迭代步骤的数量并分配一个(i)在每个循环步骤中,在一个浮点上迭代是一个坏主意,在C或C++中。它是从FORTRAN中删除的。原因是,对于一些附加信息,请参阅一些附加信息。