简体   繁体   English

将OR逻辑运算符从C ++转换为Fortran

[英]Conversion of OR logical operator from C++ to Fortran

What would be the conversion of following C++ logical operator into Fortran 90 (.f90)? 将以下C ++逻辑运算符转换为Fortran 90(.f90)会是什么? If ( vx is present or vy is present). 如果(存在vxvy存在)。 Here vx and vy are components of velocity 这里vxvy是速度的组成部分

if(vx || vy){
vT=sqrt(vx*vx + vy*vy);
}

I have tried following 我试过以下

if(vx .or. vy) then
vT = sqrt(vx*vx + vy*vy)
end if

but I am getting error: 但我收到错误:

operands of logical operator `.or.` at (1) are REAL(8)/REAL(8).

Can anyone guide me here? 谁能指导我在这里?

The C++ version is implicitly comparing vx and vy with zero. C ++版本隐式地将vxvy与零进行比较。

In Fortran, you have to do so explicitly 1 : 在Fortran中,你必须明确地这样做1

if (vx /= 0 .or. vy /= 0) then

Since the if statement looks like a performance optimization, it might be worth questioning whether it's needed altogether or could be replaced with an unconditional assignment to vT (that would set vT to zero if both vx and vy are zero). 由于if语句看起来像性能优化,因此可能值得质疑它是否需要完全或者可以用无条件赋值转换为vT (如果vxvy都为零,则将vT设置为零)。

1 I hope I got this right; 1我希望我做对了; haven't programmed in Fortran for many years. 多年没有在Fortran编程。

In the present case it is not relevant, but in general it should be noted that Fortran logical operations are not short-circuited. 在目前的情况下,它是不相关的,但一般来说应该注意Fortran逻辑运算没有短路。 So, for example, the following C++ code 因此,例如,以下C ++代码

if (a == 0 || 10 / a == 1)
{
   ...
}

is not equivalent to 不等于

if (a == 0 .or. 10 / a == 1) then
    ...
end if

in Fortran. 在Fortran。 A compiler may decide to evaluate the second term first and then... oops. 编译器可能决定先评估第二个术语,然后再... oops。 It should be written using two nested if s. 它应该使用两个嵌套的if来编写。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM