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
New Contributor I
2,697 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
Honored Contributor III
2,669 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
2,683 Views

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

0 Kudos
Nick3
New Contributor I
2,677 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.

0 Kudos
Steve_Lionel
Honored Contributor III
2,670 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
Honored Contributor III
2,663 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
2,617 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

0 Kudos
JohnNichols
Valued Contributor III
2,589 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'

0 Kudos
Arjen_Markus
Honored Contributor I
2,561 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.

0 Kudos
mecej4
Honored Contributor III
2,550 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.

0 Kudos
Steve_Lionel
Honored Contributor III
2,529 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.

 
0 Kudos
mecej4
Honored Contributor III
2,521 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.

0 Kudos
JohnNichols
Valued Contributor III
2,505 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.  

0 Kudos
n_scott_pearson
Super User
2,487 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

0 Kudos
AlHill
Super User
2,478 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

0 Kudos
JohnNichols
Valued Contributor III
2,466 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 

 

0 Kudos
JohnNichols
Valued Contributor III
2,460 Views

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

0 Kudos
Reply