Fortran类型转换 [英] Fortran type conversions
问题描述
我有以下命令来设置我的数组
使用,Intrinsic :: iso_fortran_env
整数(Int8) ,Allocatable :: iu(:)
分配(iu(4))
iu = [4,3,2,1]
如何停止编译器给我
分配(IU(4)); iu = [4,3,2,1]
1
警告:转换
从INTEGER(4)到INTEGER(1)可能在(1)处发生更改[-Wconversion]
高性能Mark的解答可解决您的问题。但是,假设 int8
不是默认类型(错误消息支持),那么在该答案中给出的数组构造函数中的每个元素应该具有相同的类型善良(他们没有)参数。所以:
iu = [4_int8,3_int8,2_int8,1_int8]
是一个不应该涉及转换的有效构造函数。
这样做有点乏味,特别是有很多元素,所以值得注意的是(如Fortran 2008 4.8中所述)可以在数组构造函数中使用类型规范来指定数组的类型和类型参数。然后,你可以写出:
pre code> iu = [integer(Int8):: 4,3,2,1]
其中值现在只需符合整数(Int8)
。
gfortran是否抱怨转换似乎取决于编译器版本。使用旧版本进行测试仍然存在警告,4.9.0版本没有。
I have the following command to set my array
Use, Intrinsic :: iso_fortran_env
Integer (Int8), Allocatable :: iu(:)
Allocate (iu(4))
iu = [4,3,2,1]
How can I stop the compiler giving me
Allocate (iu(4)); iu = [4,3,2,1]
1
Warning: Possible change of value in conversion
from INTEGER(4) to INTEGER(1) at (1) [-Wconversion]
High Performance Mark's answer about solves your problem. However, assuming int8
isn't the default kind (which the error messages supports) each element in the array constructor given in that answer should have the same type (they have) and kind (they haven't) parameter. So:
iu = [4_int8,3_int8,2_int8,1_int8]
is a valid constructor which shouldn't involve conversion.
It's a bit tedious to do that, especially with many elements, so it's worth noting that (as described in Fortran 2008 4.8) it's possible to use a type specification in the array constructor to specify the type and type parameters of the array. You can, then, write
iu = [integer(Int8) :: 4, 3, 2, 1]
where the values need now only be conformable with integer(Int8)
.
Whether gfortran complains about conversion seems to depend on the compiler version. Testing with an old version there was still a warning, with 4.9.0 there wasn't.
这篇关于Fortran类型转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!