Irf*_*anS 3 c++ fortran if-statement logical-operators or-operator
将以下C++逻辑运算符转换为Fortran 90(.f90)会是什么?如果(vx存在或vy存在).这里vx和vy有速度的部件
if(vx || vy){
vT=sqrt(vx*vx + vy*vy);
}
Run Code Online (Sandbox Code Playgroud)
我试过以下
if(vx .or. vy) then
vT = sqrt(vx*vx + vy*vy)
end if
Run Code Online (Sandbox Code Playgroud)
但我收到错误:
operands of logical operator `.or.` at (1) are REAL(8)/REAL(8).
Run Code Online (Sandbox Code Playgroud)
谁能指导我在这里?
C++版本隐含地比较vx并且vy为零.
在Fortran中,你必须明确地这样做1:
if (vx /= 0 .or. vy /= 0) then
Run Code Online (Sandbox Code Playgroud)
由于if语句看起来像一个性能优化,它可能是值得质疑是否它完全需要或可以与无条件转让被替换vT(这将设置vT为零如果两个vx和vy为零).
1我希望我做对了; 多年没有在Fortran编程.