Fortran Question on I descriptor in Fortran

  • Thread starter Thread starter issacnewton
  • Start date Start date
  • Tags Tags
    Fortran
AI Thread Summary
The discussion centers around the use of the I descriptor in Fortran's format statements, particularly the confusion surrounding the output of the 210 format statement with 2I5.0. Participants clarify that the I descriptor's general form rIw.m indicates that m is the minimum number of digits to display, and in this case, a value of zero means that no digits will be printed for zero values. The variable "index" is displayed because it is negative, while "index + 12" is not displayed when it equals zero. This behavior is noted as a common "gotcha" in Fortran, leading many programmers to avoid such formatting in practical applications. The discussion also touches on historical context, explaining that early Fortran aimed to provide control over report formatting, which resulted in some quirks that persist in the language today. Overall, the consensus is that while the I descriptor can suppress zero values, it is often more practical to use a standard format that displays zeros.
issacnewton
Messages
1,035
Reaction score
37
Hi

I am learning fortran from Chapman's "fortran 90-95 for scientists and engineers". I have question about the I descriptor. In the attached image, look at the second format statement, 210 format. In the output, the variable index+12 is not printed. I don't understand that. The book says that the I descriptor has general form rIw.m where m is the minimum number of digits to be displayed. So in the statement 210 format, we have 2I5.0, which means that minimum number of digits to be displayed is zero , but then in the output we see that "index" is displayed , while "index+12" is not displayed. I don't understand this confusing behavior.

thanks
 

Attachments

  • 1.jpg
    1.jpg
    26.8 KB · Views: 431
Technology news on Phys.org
The minimum number of digits needed to represent 0 (in fortran) is zero, so the blank is acceptable.
 
I don't quite follow, antiphon. In that case, why is the variable "index" is displayed when that is also given the same descriptor.
 
I think it's due to the index being negative that it gets displayed. It's a kinda gotcha that teachers want you to remember. Practicing programmers tend not to use these kinds of formatting because of this behavior. Basically if I'm printing a number I want to see a number.

Another famous gotcha for some fortrans is printing a 1 in column 1 to slew the paper. Beginners would write a simple program to print numbers from 100 to 200 one on each line and would wonder why they got a Stack of paper with the first digit of each number missing.
 
well jedi, that's not true. I changed index to 12 and with descriptor 2I5.0 , the values now printed are 12 for index and 24 for index+12.
 
Okay so the best answer is that if it's zero then nothing is printed. It might be a hidden feature where you're generating a report and when a row value is zero then print nothing.

I5 means use 5 columns and right justify the number ie like I5.1

whereas I5.0 means use 5 columns and print the number right justified with a minimum of zero digits which means 1-9 prints as one digit but zero prints as zero digits.

And I5.2 would print 1-9 as ___01 ___02 ... And zero as ___00. With underscore as space

Anyway, just remember this for your test, that's why it's given to you.
 
IssacNewton said:
I don't quite follow, antiphon. In that case, why is the variable "index" is displayed when that is also given the same descriptor.

When you use Ix.y, y is the MINIMUM number of non-blank characters that will be used to display the number.

If the "conventional" display of the number would take LESS than y characters, it is padded with 0's to produce output like 00012 or -0012.

If the number takes MORE than y characters to display, then more than y characters are used (up to a maximum of x characters, of course)

The only practical reason for using a Ix.0 format is if you want a field that would contain zero to be left completely blank. In "real life", you would almost always just use an Ix format, and get zero values printed as 0.
 
Thank you jedi and aleph. Makes some sense. But its weird feature built in fortran.
For all practical reasons, like in real life, don't use this confusing format.
 
IssacNewton said:
Thank you jedi and aleph. Makes some sense. But its weird feature built in fortran.
For all practical reasons, like in real life, don't use this confusing format.

It's the reality of many early programming languages. Programmers wanted some control in making both scientific and business style reports. They develop a printing convention to suppress zero via a specific format as there was no easy way to do it otherwise. Similarly for the one and zero in column one of the print line. Early fortran was a batch processing via card decks and computer line printouts on fanfold paper.

All this string stuff came later in c programming and was added to fortran. Also remember the CS mantra you can always add new features, functions and methods, and deprecate obsolete ones but you'll really get a lot of sh?? If you remove or change a function, method or convention that someone has used in a program already. Hence fortran acquires many quirky things that make it so lovable.
 
Last edited:
  • #10
IssacNewton said:
Thank you jedi and aleph. Makes some sense. But its weird feature built in fortran.
For all practical reasons, like in real life, don't use this confusing format.

Not quite true only if you do fortran for computer simulation work, but years ago engineers would sometimes be given business reports to do. I know I was one such engineer.
 
Last edited:

Similar threads

Replies
11
Views
11K
Replies
5
Views
2K
Replies
6
Views
2K
Replies
10
Views
9K
Replies
13
Views
4K
Replies
7
Views
137K
Replies
1
Views
2K
Back
Top