- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

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?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

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

Link Copied

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

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

although some say Lost in Stupid Paratheses'

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

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

Doc

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

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

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page