我do在我正在使用的旧代码中遇到了嵌套结构,并希望理解和现代化。它使用相同的标记动作语句来终止do循环,以及go to语句。这是一个简化版本,它通过一些其他微不足道的操作说明了原始代码的逻辑:
subroutine original(lim)
k=0
do 10 i=1,4
do 10 j=1,3
k=k-2
if (i>lim) go to 10
k=k+i*j
10 k=k+1
write(*,*) k
return
end
Run Code Online (Sandbox Code Playgroud)
在查看此站点上的其他问题
(和外部资源)后,这是我重写原始代码逻辑的最大努力,没有过时的功能(和):go to
subroutine modern(lim)
integer, intent(in) :: lim
integer :: i, j, k
k=0
outer: do i=1,4
inner: do j=1,3
k=k-2
if (i>lim) then
k=k+1
cycle inner
end if
k=k+i*j
k=k+1
end do inner
end do outer
write(*,*) k
end subroutine modern
Run Code Online (Sandbox Code Playgroud)
我使用以下程序测试了代码,包括触发/不触发go to …
我有一个与几年前在英特尔开发人员论坛上提出的有关就地重构阵列的问题相关的问题。
简而言之,答案是可以分配特定等级的数组,并创建一个指向相同内存位置(即就地)但具有不同等级的指针,例如:
use, intrinsic :: ISO_C_BINDING
integer, allocatable, target :: rank1_array(:)
integer, pointer :: rank3_array(:,:,:)
integer :: i
! Allocate rank1_array
allocate(rank1_array(24))
! Created rank3_pointer to rank1_array
call C_F_POINTER (C_LOC(rank1_array), rank3_array, [3,2,4])
! Now rank3_array is the same data as rank1_array, but a 3-dimension array with bounds (3,2,4)
Run Code Online (Sandbox Code Playgroud)
我现在的问题是,如果我deallocate是原始数组rank1_array,为什么指针rank3_array仍然关联,并且可以毫无问题地使用(似乎)。因此,如果我附加上面的代码段:
! initialise the allocated array
rank1_array = [(i, i=1,24)]
! then deallocate it
deallocate(rank1_array)
! now do stuff with the pointer
print *, …Run Code Online (Sandbox Code Playgroud) 我正在学习 Fortran,需要我的程序来查找数组中的特定值。一个简单的程序如下:
program hello
implicit none
integer :: x
x = findloc([4,9,0,2,-9,9,1],9)
end program hello
Run Code Online (Sandbox Code Playgroud)
给我以下错误:
Error: Function 'findloc' at (1) has no IMPLICIT type
Run Code Online (Sandbox Code Playgroud)
我正在 macbook 上使用 gfortran 编译它。如果我能获得有关 findloc 的帮助,我将非常感激
为了与C程序实现互操作性,Fortran标准的2003年修订版引入了一个模块iso_c_binding,该模块允许编写以下内容:
USE iso_c_binding, ONLY: c_int, c_float
INTEGER(KIND=c_int) :: x
REAL(KIND=c_float) :: y
Run Code Online (Sandbox Code Playgroud)
保证可以在C端精确映射到a int x和a float y。现在对于整数,这是至关重要的,因为默认INTEGER类型(取决于编译器和平台)可以很好地包装为64位整数类型,即使int是32位。
但是,对于浮点类型,以下映射似乎是不可避免的:
REAL*4 (or) REAL -> float
REAL*8 (or) DOUBLE PRECISION -> double
Run Code Online (Sandbox Code Playgroud)
因此,这里的问题:是否有任何实际的平台或编译器在哪里,这是没有满足,例如,在那里sizeof(REAL*4) != sizeof(float)?