Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/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
Function 使用Iso#u Fortran_Env设置函数';s类价值_Function_Fortran_Fortran2008 - Fatal编程技术网

Function 使用Iso#u Fortran_Env设置函数';s类价值

Function 使用Iso#u Fortran_Env设置函数';s类价值,function,fortran,fortran2008,Function,Fortran,Fortran2008,如何使用ISO Fortran Env的内在函数以Fortran 2008惯用的方式设置函数的返回种类值 通常在主程序中,我可以使用ISO Fortran内部函数,如下所示: program name here use iso_fortran_env implicit none integer, parameter :: double=REAL64 real(kind=double) :: some_variable end program name here

如何使用ISO Fortran Env的内在函数以Fortran 2008惯用的方式设置函数的返回种类值

通常在主程序中,我可以使用ISO Fortran内部函数,如下所示:

program name here
    use iso_fortran_env
    implicit none
    integer, parameter :: double=REAL64
    real(kind=double) :: some_variable
end program name here
program name here
    use iso_fortran_env
    implicit none
    integer, parameter :: double=REAL64
    real(kind=double) :: some_function
    ! Do stuff
end program name here
real function some_function()
    ! Do stuff
end some_function
但是似乎没有一种方便的方式将这些内部函数用于外部函数,因为REAL64和double都只在上面的主函数中定义。尝试在main中定义函数的种类,如下所示:

program name here
    use iso_fortran_env
    implicit none
    integer, parameter :: double=REAL64
    real(kind=double) :: some_variable
end program name here
program name here
    use iso_fortran_env
    implicit none
    integer, parameter :: double=REAL64
    real(kind=double) :: some_function
    ! Do stuff
end program name here
real function some_function()
    ! Do stuff
end some_function
至少在我的系统上,抛出一个类型不匹配错误(在我的系统上,double get定义为KIND=8,默认real get定义为KIND=4)。我总是可以使用
real(kind=8)function some_function()
,但出于可移植性的考虑,我不希望这样做。另外,在一个地方使用来自iso_fortran_env的REAL64,而在另一个地方使用KIND=8,感觉很肮脏

有没有一种简单(或至少可读)的方法来实现这一点,比如下面

real(kind=REAL64) function some_function()

我通常在发帖前尝试一下,但我认为这应该可以:

function some_function()
    use iso_fortran_env, only: real64
    implicit none
    real(kind=real64) :: some_function
    some_function = 1.0_real64
end function some_function
或者在模块内部

module some_module
    use iso_fortran_env, only: real64
    implicit none
contains
    real(kind=real64) function some_function()
        some_function=1.0_real64
    end function some_function
end module some_module

扩展@chw21的答案:

您始终可以选择在函数的规范部分声明函数结果的类型,并通过其中的主机关联从模块访问参数

编辑:«正如@Vladimir F所指出的,您还可以通过主机关联从函数体中声明的模块访问变量。»

事实上,这是将属性应用于函数结果的唯一方法,如指针、可分配、维度等

此外,还可以通过
result
后缀为函数结果声明新名称

pure function some_other_function_with_a_long_name() result(out)
    use, intrinsic :: iso_fortran_env, only: rk => real64
    implicit none
    real(rk), dimension(5) :: out
    out = 1.0_rk
    ! (...)
end

首先,您的问题提供了一个解决方案,并且该解决方案运行良好。正如IanH指出的,标准措辞中存在一些歧义,但我认为这是允许的,编译器也接受这种语法:

fundef.f90:

real(kind=REAL64) function some_function()
  use iso_fortran_env
  some_function = 1._real64
end
汇编:

> gfortran -c funkind.f90 
> 
您可以使用在函数内部使用的模块中定义的类型。还使用英特尔Fortran和Oracle Studio进行了测试


在现代Fortran中,所有函数都应该定义在一个模块中,但如果您希望从一个只在函数内部使用的模块中获得一种函数,那么可能性就在这里。

Interp F90/0145解决了这一问题。这一问题尚未解决,20多年来一直如此。有鉴于此,我认为将其描述为“完全合法”可能有点争议。我对这个词不太确定,甚至认为我忽略了它,但显然不是。我积极避免使用这种方法,因为这种“争议”是如何表现出来的。即使编译器似乎接受语法,也不值得研究它是如何处理的。我打开了interp,但我认为它指的是更精细的细节,主要是非常量实体和作用于它们的查询函数。也就是说,这似乎与该
的正确性一致,如果主机或使用关联可以在函数内访问,则命名常量可以出现在函数语句的A中。
回答F90/0145中的A点),无论出于何种原因,interp响应被拒绝。关于interp的进一步工作可能会改变这个答案,或者它可能会被提出并通过,而不会改变——我们只是不知道。我可以理解为什么在使用interp时需要谨慎,因为语言的顺序要求已经够复杂了。同时,我采用了与francescalus相同的方法。我最初计划询问纯函数,但我想我会将其归结为一个简单的“脏”函数。我真的很高兴听到这仍然是可能的纯。