Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.

C && equivalent in Fortran

onkelhotte
New Contributor II
1,778 Views

Hi there,

I have this if clause:

if(index==0 .and. value(index) > 0.) then

When value is an array from 1 to n and index is 0, I get an error, because value(0) doesnt exist.
Is there a || comparism in Fortan? AFAIK, when using in C

if(index > 0&& value(index) > 0)

the expression value(index) will not be evaluated whenindex == 0.

My workaround looks like this:

if(index == 0) then
...do something
else if(value(index) > 0) then
...do the same something again...
end if

Is there a more elegant way?

Thanks in advance,
Markus

0 Kudos
7 Replies
tropfen
New Contributor I
1,778 Views
Hello,

i have seen the same problem in my work. This is the reason why i havent transfert many of my programs from cvf to ivf. Cvf was able to solve the problem. All i have understand from statements from Steve Lionel is: IVF works in this way and you have to live with it. In my point of view there only one way to go: adding a feature request.

Frank
0 Kudos
Les_Neilson
Valued Contributor II
1,778 Views
I guess the root of the problem possibly lies in the fact that C has "sequence points" and therefore ensures that if the first clause fails then the second clause is not executed. Fortran doesn't have "sequence points" and indeed optimisation may result in clause 2 being evaluated before clause 1.

Les
0 Kudos
jimdempseyatthecove
Honored Contributor III
1,778 Views

>>if(index>0 .and. value(index) > 0.) then

nest the clauses (keep C++ syntax as comment)

! ** if(index>0&& value(index) > 0.) then **
if(index>0) then
if(value(index) > 0.) then
...
endif
endif

Jim Dempsey
0 Kudos
TimP
Honored Contributor III
1,778 Views
The direct Fortran equivalent of the C code
if(index > 0&& value(index) > 0){
...
}
would be
if(index > 0)then
if(value(index) > 0))then
....
endif
endif

This has been explained several times on this forum, including the explanation that CVF didn't actually implement the proposed extension to Fortran, but a few people got away with coding something different from what they meant.

0 Kudos
onkelhotte
New Contributor II
1,778 Views
Oh, in my original posting I mistyped the C if clause... It should be

if(index== 0 && value(index) > 0) { ... }

This means, the expression value(index) will not be evaluated when index == 0 and the instructions inside the brackets will be processed.

In Fortran, I have to use this:

if(index == 0) then
...do something
else if(value(index) > 0) then
...do the same something again...
end if

There seems no other way rather then typing the intructions two times.


Markus
0 Kudos
TimP
Honored Contributor III
1,778 Views
It seems pointless to me to continue in this vein of "why doesn't Fortran offer the same compound conditionals as C."
Do you have a rule in your C style guide against writing

if(index== 0)if(value(index) > 0) { ... }

or is that unacceptably straightforward?
You can't impose your C practices on Fortran. In this case, Fortran doesn't have as many ways of writing the same thing as C.
In C, if you wanted to give the compiler the option of "optimizing" this expression, you could write

if(index== 0 & value(index) > 0) { ... }

and I don't think you can convince me that Fortran shouldn't have an equivalent to the latter version.

Anyway, it's too late to revise history which goes back over 50 years for Fortran and over 30 years for C.
0 Kudos
Jeffrey_A_Intel
Employee
1,778 Views
Quoting - tropfen
Hello,

i have seen the same problem in my work. This is the reason why i havent transfert many of my programs from cvf to ivf. Cvf was able to solve the problem. All i have understand from statements from Steve Lionel is: IVF works in this way and you have to live with it. In my point of view there only one way to go: adding a feature request.

Frank

Since Steve is away, I'll refer you toone of his previous responses on this topic.

Note that CVF didn't "solve the problem." If it worked for you,that wasfortuitous. If you change your code, it may no longer work. If you change the compilation switches, it may no longer work. You are depending on undefinedcompiler behaviour.

Jeff
0 Kudos
Reply