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
Nouveau contributeur I
3 854 Visites

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 Compliments
1 Solution
Steve_Lionel
Contributeur émérite III
3 826 Visites

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

Voir la solution dans l'envoi d'origine

15 Réponses
Ulrich_M_
Nouveau contributeur I
3 840 Visites

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

0 Compliments
Nick3
Nouveau contributeur I
3 834 Visites

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 Compliments
Steve_Lionel
Contributeur émérite III
3 827 Visites

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
Contributeur émérite III
3 820 Visites

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 utilisateur
3 774 Visites

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 Compliments
JohnNichols
Précieux contributeur III
3 746 Visites

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 Compliments
Arjen_Markus
Contributeur émérite II
3 718 Visites

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 Compliments
mecej4
Contributeur émérite III
3 707 Visites

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 Compliments
Steve_Lionel
Contributeur émérite III
3 686 Visites

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 Compliments
mecej4
Contributeur émérite III
3 678 Visites

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 Compliments
JohnNichols
Précieux contributeur III
3 662 Visites

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 Compliments
n_scott_pearson
Super utilisateur
3 644 Visites

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 Compliments
AlHill
Super utilisateur
3 635 Visites

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 Compliments
JohnNichols
Précieux contributeur III
3 623 Visites

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 Compliments
JohnNichols
Précieux contributeur III
3 617 Visites

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

0 Compliments
Répondre