自动类型转换(类型转换)如何在Fortran中工作? [英] How does automatic typecasting (type conversion) work in Fortran?

查看:474
本文介绍了自动类型转换(类型转换)如何在Fortran中工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用gfortran编译器。另请告诉我,在执行自动类型转换(类型转换)时,gfortran是否使用Fortran标准以外的其他类型。解析方案

Fortran 2008第7.2节。值得注意的是Cl。 7.2.1.3第8段:


对于变量为数值类型的内部赋值语句,expr可能有不同的数值
type或kind type参数,在这种情况下,根据表7.9的规则,将expr的值转换为变量的类型和种类类型参数


$ b $表7.9:数字转换和赋值语句

 变量类型值赋值
整数INT(expr ,KIND = KIND(变量))
实数REAL(expr,KIND = KIND(变量))
复数CMPLX(expr,KIND = KIND(变量))


这意味着任何表达式( expr )都会隐式转换为它被分配给的变量的类型和种类。对于字符类型,派生类型和其他任何内容,请参阅标准。



还要注意,Fortran只在赋值和初始化时执行此类转换,而不是像过程调用一样执行上下文。例如,考虑这个过程:

pre $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
print *,a
结束子程序

类型整数。例如,你不能这样做:

  call sub1(1.d0)

,因为这会导致实际和虚拟参数之间的类型不匹配。



您但是,可以这样做:

  integer :: a 
a = 1.d0!隐式解释为:a = INT(1.d0,kind = kind(a))
调用sub1(a)

因为隐式转换是为赋值定义的。






gfortran中隐式类型转换标准的唯一记录扩展(5.1.0)在赋值期间介于逻辑类型和整数类型之间。



请参阅: https://gcc.gnu.org/onlinedocs/gcc-5.1.0 /gfortran/Implicitly-convert-LOGICAL-and-INTEGER-values.html#Implicitly-convert-LOGICAL-and-INTEGER-values




  • 逻辑 .true。转换为整数 1

  • 逻辑 .false。转换为整数 0

  • 整数 0 转换为 .false。

  • 其他任何整数转换为 .true。






<请注意,如果您可以不使用旧版扩展程序,那么请不要使用它们。使用它们意味着你的程序不是标准的Fortran,因此任何编译器都可以自由地拒绝它,因为它是不正确的。此扩展旨在允许使用现代编译器编译旧代码,而不是用于新代码。


I am using gfortran compiler. Also tell me if gfortran uses something other than the Fortran standard while performing automatic typecasting (type conversion).

解决方案

Assignment is defined by Fortran 2008 Section 7.2. Of note is Cl. 7.2.1.3 paragraph 8:

For an intrinsic assignment statement where the variable is of numeric type, the expr may have a different numeric type or kind type parameter, in which case the value of expr is converted to the type and kind type parameter of the variable according to the rules of Table 7.9.

Table 7.9: Numeric conversion and the assignment statement

Type of variable     Value Assigned
integer              INT(expr , KIND = KIND (variable))
real                 REAL(expr , KIND = KIND (variable))
complex              CMPLX(expr , KIND = KIND (variable))

This means that any expression (expr) will be implicitly converted to the type and kind of the variable it is being assigned to. For character types, derived types and anything else, please see the standard.

Also note that Fortran only performs conversions like this during assignment and initialization but not contexts like procedure calls. For example, consider this procedure:

subroutine sub1(a)
 implicit none
 integer :: a     
 print *, a  
end subroutine

This procedure has a dummy argument of type integer. You cannot, for example, do this:

call sub1(1.d0)

because this results in a mismatch of type between the actual and dummy arguments.

You can, however, do this:

integer :: a
a = 1.d0     !implicitly interpreted as: a = INT(1.d0, kind=kind(a))
call sub1(a)

because the implicit conversion is defined for the assignment.


The only documented extension to the standard for implicit type conversion in gfortran (5.1.0) is between logical and integer types during assignment.

See: https://gcc.gnu.org/onlinedocs/gcc-5.1.0/gfortran/Implicitly-convert-LOGICAL-and-INTEGER-values.html#Implicitly-convert-LOGICAL-and-INTEGER-values

  • Logical .true. is converted to integer 1
  • Logical .false. is converted to integer 0
  • Integer 0 is converted to .false.
  • Any other integer is converted to .true.

Do note that if you can do without legacy extensions then don't use them. Using them means your program is not standard Fortran and thus any compiler is free to reject it for being incorrect. This extensions is meant to allow legacy code to compile with modern compilers and not for use in new code.

这篇关于自动类型转换(类型转换)如何在Fortran中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆