Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28808 Discussions

Nonstandard order of operations, e.g. a + -5 * b

fortrandave
New Contributor I
1,117 Views

The following code compiles without any warning when I use Intel Fortran whereas I get a warning when using gfortran -- "Extension: Unary operator following arithmetic operator (use parentheses)".  I've been looking through e.g.  https://www.intel.com/content/www/us/en/docs/fortran-compiler/developer-guide-reference/2024-0/summary-of-language-extensions.html and can't find where this behavior is documented.  Can someone please point me to the right location?

program invalid_sequence_of_operators
implicit none
real :: a, b
b = 1
a = b + -5 * b
write(*,*)a
end program invalid_sequence_of_operators

 

0 Kudos
1 Solution
mecej4
Honored Contributor III
1,100 Views

This is an old DEC/Intel extension. The extension is documented, but the relevant lines are not easy to find (unless you know at least a part of the exact wording!). In the manual page that you showed a link for, just before "Examples", you can read this:

Normally, two operators cannot appear together. However, Intel® Fortran allows two consecutive operators if the second operator is a plus or minus.

The manual does not state how such a non-standard expression will be evaluated, but my recollection is that a minus or a plus preceding a constant or a variable name, if it is the second of two consecutive operators,  has higher precedence than the first of the two consecutive operators.

You can use compiler options to flag such extensions as errors.

 

S:\LANG>ifort /warn:stderrors /stand=f03 expext.f90
Intel(R) Fortran Intel(R) 64 Compiler Classic for applications running on Intel(R) 64, Version 2021.11.0 Build 20231010_000000
Copyright (C) 1985-2023 Intel Corporation.  All rights reserved.

expext.f90(5): error #8810: Consecutive operators are an extension to the Fortran 2003 standard.
a = b + -5 * b
--------^
compilation aborted for expext.f90 (code 1)

 

 

 

 

View solution in original post

4 Replies
mecej4
Honored Contributor III
1,101 Views

This is an old DEC/Intel extension. The extension is documented, but the relevant lines are not easy to find (unless you know at least a part of the exact wording!). In the manual page that you showed a link for, just before "Examples", you can read this:

Normally, two operators cannot appear together. However, Intel® Fortran allows two consecutive operators if the second operator is a plus or minus.

The manual does not state how such a non-standard expression will be evaluated, but my recollection is that a minus or a plus preceding a constant or a variable name, if it is the second of two consecutive operators,  has higher precedence than the first of the two consecutive operators.

You can use compiler options to flag such extensions as errors.

 

S:\LANG>ifort /warn:stderrors /stand=f03 expext.f90
Intel(R) Fortran Intel(R) 64 Compiler Classic for applications running on Intel(R) 64, Version 2021.11.0 Build 20231010_000000
Copyright (C) 1985-2023 Intel Corporation.  All rights reserved.

expext.f90(5): error #8810: Consecutive operators are an extension to the Fortran 2003 standard.
a = b + -5 * b
--------^
compilation aborted for expext.f90 (code 1)

 

 

 

 

fortrandave
New Contributor I
1,086 Views

Hey, thanks!  That's exactly what I was looking for.  I'm using a code linter (Coverity cov-run-fortran) that has an option to recognize Intel Fortran extensions.  The linter doesn't recognize this one so having the Intel documentation reference is very helpful when pointing out that the linter is not working correctly.  Agreed that it's hard to find, although this gives me a bit better feel for how the docs are laid out.

0 Kudos
Steve_Lionel
Honored Contributor III
1,026 Views

I discuss this, and other related issues, in Doctor Fortran in "Order! Order!" - Doctor Fortran (stevelionel.com)

0 Kudos
fortrandave
New Contributor I
979 Views
0 Kudos
Reply