You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Fortran 2003逻辑类型转数值类型编译错误修复求助

Fortran逻辑数组编译错误修复方案

错误场景

编译旧Fortran程序时触发类型不匹配错误,涉及mask_part和npsortie两个LOGICAL类型数组,因依赖限制只能使用gfortran-7编译器。

编译错误信息

if (sum(npsortie>0)) write(*,'(a,i8,a,i8,a,i8,a,i8,a)') &
      1
Error: 'array' argument of 'sum' intrinsic at (1) must have a numeric type
advect.f90:577:21:


       if(sum(int(mask_part))>0) then
                 1
Error: 'a' argument of 'int' intrinsic at (1) must have a numeric type
advect.f90:580:69:


         meanz=sum(ztra1(1:numpart),DIM=1,MASK=mask_part)/sum(int(mask_part))
                                                                 1
Error: 'a' argument of 'int' intrinsic at (1) must have a numeric type

当前编译参数

gfortran-7 -O2 -fopenmp -pedantic -std=f2003 -cpp -Wall -fconvert=swap -fall-intrinsics -fmax-errors=20 -I /usr/include -L /urs/include -c advect.f90

修复方案

问题根源是Fortran 2003标准禁止直接对逻辑数组使用sum或int内置函数,旧编译器的非标准隐式转换逻辑在gfortran-7的严格标准模式下失效。针对每个错误点修改:

  • 处理sum(npsortie>0)
    原代码意图统计逻辑数组中.true.元素的数量,替换为标准函数count(专门用于统计逻辑数组真元素个数):

    if (count(npsortie>0) > 0) write(*,'(a,i8,a,i8,a,i8,a,i8,a)') &
    
  • 处理sum(int(mask_part))
    直接用count(mask_part)替代,无需数值转换即可统计真元素数量:

    if(count(mask_part)>0) then
    
  • 处理除法中的sum(int(mask_part))
    同样替换为count(mask_part):

    meanz=sum(ztra1(1:numpart),DIM=1,MASK=mask_part)/count(mask_part)
    

替代方案(若需保留数值转换逻辑)

如果必须保留逻辑转数值的操作,可通过merge函数显式转换:

sum(merge(1, 0, mask_part))

但优先推荐count函数,更符合Fortran标准规范且可读性更高。

内容的提问来源于stack exchange,提问作者monos

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 09:40:30