Fortran 函数返回意外的类型和值 [英] Fortran functions returning unexpected types and values
问题描述
我正在从事一个需要在 Fortran 中实现一些数值方法的项目.为此,我需要编写一些递归函数.这是我的代码.
I'm working on a project that needs to implement few numerical methods in Fortran. For this, I need to write some recursive functions. Here is my code.
!
! File: main.F95
!
RECURSIVE FUNCTION integrate(n) RESULT(rv)
IMPLICIT NONE
DOUBLE PRECISION :: rv
INTEGER, INTENT(IN) :: n
DOUBLE PRECISION, PARAMETER :: minusone = -1.0
IF (n == 1) THEN
rv = 10 !exp(minusone)
RETURN
ELSE
rv = 1 - (n * integrate(n - 1))
RETURN
END IF
END FUNCTION integrate
RECURSIVE FUNCTION factorial(n) RESULT(res)
INTEGER res, n
IF (n .EQ. 0) THEN
res = 1
ELSE
res = n * factorial(n - 1)
END IF
END
PROGRAM main
DOUBLE PRECISION :: rv1
PRINT *, factorial(5)
PRINT *, integrate(2)
!READ *, rv1
END PROGRAM main
对于这个程序,输出是:
For this program the output is:
NaN
1
如果我更改打印语句的顺序(第 30 行和第 31 行),输出将是:
If I change the order of the print statements (line 30 & 31), the output will be:
1
-19.000000
输出应该是(对于原始打印语句顺序):
Output should be (for the original print statement order):
120
-19
我从维基百科Fortran 95 语言特性页面中获取了阶乘函数.
I took the factorial function from the Wikipedia Fortran 95 language features page.
- 编译器:带有 Cygwin 的 gfortran 4.5.3
- IDE:Netbeans 7.0.1
- 平台:Windows 7
推荐答案
您的函数编写正确.问题出在主程序中,您没有显式声明 integrate
和 factorial
函数的类型,因此您具有隐式类型,在这种情况下 factorial
假定为 REAL
并且 integrate
假定为 INTEGER
.出于某种原因,您的编译器没有警告您有关类型不匹配的信息.我的:
Your functions are written correctly. The problem is in the main program, where you do not explicitly declare the type of integrate
and factorial
functions, so you have implicit typing, in which case factorial
is assumed REAL
and integrate
is assumed INTEGER
. For some reason, your compiler did not warn you about type mismatch. Mine did:
$ gfortran recurs.f90
recurs.f90:26.22:
PRINT *, integrate(2)
1
Error: Return type mismatch of function 'integrate' at (1) (INTEGER(4)/REAL(8))
recurs.f90:27.22:
PRINT *, factorial(5)
1
Error: Return type mismatch of function 'factorial' at (1) (REAL(4)/INTEGER(4))
您应该将主程序更改为:
You should change your main program to:
PROGRAM main
IMPLICIT NONE
DOUBLE PRECISION, EXTERNAL :: integrate
INTEGER, EXTERNAL :: factorial
PRINT *, factorial(5)
PRINT *, integrate(2)
END PROGRAM main
注意 IMPLICIT NONE
行.此声明语句将禁用任何隐式类型,如果不是所有变量和函数都显式声明,编译器将抛出错误.这是每个 Fortran 程序中非常重要的一行,如果您拥有它,您就会自己解决问题,因为它会迫使您显式声明程序中的所有内容.
Notice the IMPLICIT NONE
line. This declaration statement will disable any implicit typing, and the compiler would throw an error if not all variables and functions are explicitly declared. This is a very important line in every Fortran program, and if you had it, you would've figured out your problem yourself, because it would force you to explicitly declare everything in your program.
现在的输出是:
120
-19.0000000000000
正如预期的那样.
作为旁注,DOUBLE PRECISION
类型声明不如使用 REAL
和指定的 KIND
参数灵活,例如一个REAL(KIND=myRealKind)
.请参阅有关如何正确使用 KIND
的问题的答案:Fortran 90 kind 参数.
As a side note, the DOUBLE PRECISION
type declaration is not as flexible as using REAL
with KIND
parameter specified instead, e.g. anREAL(KIND=myRealKind)
. See answers to this question about how to use KIND
properly: Fortran 90 kind parameter.
这篇关于Fortran 函数返回意外的类型和值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!