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

What is -2.0**2

Nick3
Novice
922 Views

I'm looking at some code that for repro-sakes I can reduce to this:

program main
real rslt
rslt = -2.0**2
print *,rslt
end

The actual equation is 1.E0-6.E0/(PI**2)*EXP(-PI**2*TAU2/A0**2) but I think the above example will suffice for my question.

Why is rslt -4.0 ?  How does the compiler determine that I want 

2.0

squared

negate

Rather than

-2.0

squared

The minus sign just confuses me here ... can somebody point me to a Fortran standard for how this is evaluated, or is this a compiler standard?

0 Kudos
1 Solution
Steve_Lionel
Black Belt Retired Employee
894 Views

That page is simply wrong. Here is what the standard says - note the example that is exactly your case.

Screenshot 2021-03-23 124418.jpg

View solution in original post

15 Replies
Ulrich_M_
New Contributor I
908 Views

google "operator precedence fortran" and you will see that exponentiation is executed before the negation in the absence of parentheses...

Nick3
Novice
902 Views

The very first hit

https://www.tutorialspoint.com/fortran/fortran_operators_precedence.htm

Says "Logical NOT and negative sign" takes precedence before Exponentiation, and this contradicts what I'm seeing.

Steve_Lionel
Black Belt Retired Employee
895 Views

That page is simply wrong. Here is what the standard says - note the example that is exactly your case.

Screenshot 2021-03-23 124418.jpg

Steve_Lionel
Black Belt Retired Employee
888 Views

I should also point out that Fortran differs from some other languages in how it treats things such as -2.0.  This is not a negative 2.0 as a single entity, it's 2.0 with a unary negation applied to it. This trips up programmers coming from other languages.

n_scott_pearson
Super User Retired Employee
842 Views

LOL! And even programmers who started in Fortran and then 'went to the dark side for a time'. Ok, I just realized it's been 45 years - and it was Fortran II - do I get a pass for that?           ;^)

...S

JohnNichols
Valued Contributor II
814 Views

No passes -- sorry you should know order of operation - this is why LISP is so much better

although some say Lost in Stupid Paratheses'

Arjen_Markus
Valued Contributor III
786 Views

Interestingly enough, if I were to see the expression "-2.0**2" (or the like) in a mathematical context, I would interpret it as "-(2.0**2)". In maths notation is everything, including precedence.

mecej4
Black Belt
775 Views

I have sometimes wondered why the creators of Fortran chose to deviate from mathematical conventions in what the expression a/bc means, for real variables. In algebra, it means 'a' divided by the product of 'b' and 'c'. In Fortran, a/b*c is the same as ac/b, assuming no overflow, etc.

Steve_Lionel
Black Belt Retired Employee
754 Views

That's simple - and most other languages I know are the same. "bc" could be a distinct variable. Keep in mind that blanks are ignored in fixed-form source.

I recall from my college days that the compiler we used, PL/C (a PL/I variant) tried to "fix" syntax errors, including replacing "b c" with "b*c".  In order to recognize "bc" as a multiplication, the compiler would have to know that all variables are single letters and that b and c were previously defined. Fortran, with implicit declarations, couldn't do that.

 
mecej4
Black Belt
746 Views

Of course, an accommodation had to be made for variable names longer than one character, and it was necessary to show the multiplication operator explicitly.

What I wanted to say was that, to be consistent with the conventions of algebra that have existed for a few centuries, in Fortran, too, a/b*c should have stood for a/(b*c), not (a/b)*c.

A few years ago, I had stated the same question in a forum (possibly here), and Walt Brainerd had replied that he was in agreement.

JohnNichols
Valued Contributor II
730 Views

However, anecdotal evidence suggests that reverse Polish notation is more difficult for users to learn than algebraic notation

A note from Wikipedia -- but I still think the HP RPN is the best way to do math - fewer errors and in engineering errors are critical 

( + (+ 3 4) 6) in LISP is completely clear 

In moment of inertia calculation you get the A*y*y  - if you use an excel chart or Fortran you get some negative y's, so it makes it interesting to teach -- I had to explain that the -y is the number squared not negative y squared.  

n_scott_pearson
Super User Retired Employee
712 Views

This all leads into the topic of defensive programming. After having a number of 'trips' going from language to language - and especially to C++ - I got in the practice of explicitly including the brackets (or braces, whichever you prefer to call them). Some of the folks I worked with used to complain about me doing this. Too bad; better safe than sorry.

...S

AlHill
Super User
704 Views

More than defensive programming, it is actually part of documentation of the source, stating clearly what the programmer intended.   Always a good practice.

Doc

JohnNichols
Valued Contributor II
692 Views

1. LISP is never ambiguous 

2. Fortran is interesting 

3. C++ is for nerds 

4. Python is for _________________

5. Forth is for real nerds

6. Basic is fum 

 

JohnNichols
Valued Contributor II
686 Views

The Basica random number generator is still more random than any Intel Fortran Random number generator --

Reply