Question on I descriptor in Fortran

1. Jan 2, 2012

issacnewton

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

File size:
26.8 KB
Views:
97
2. Jan 2, 2012

Antiphon

The minimum number of digits needed to represent 0 (in fortran) is zero, so the blank is acceptable.

3. Jan 3, 2012

issacnewton

I don't quite follow, antiphon. In that case, why is the variable "index" is displayed when that is also given the same descriptor.

4. Jan 3, 2012

Staff: Mentor

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.

5. Jan 3, 2012

issacnewton

well jedi, thats 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.

6. Jan 3, 2012

Staff: Mentor

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.

7. Jan 3, 2012

AlephZero

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.

8. Jan 3, 2012

issacnewton

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.

9. Jan 4, 2012

Staff: Mentor

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: Jan 4, 2012
10. Jan 4, 2012

Staff: Mentor

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: Jan 4, 2012