Question on I descriptor in Fortran

  • Fortran
  • Thread starter issacnewton
  • Start date
  • Tags
    Fortran
In summary: They would print on one side of a sheet of paper and the first column would have zeros all the way to 100, and the second column would have ones all the way to 199. So the ones column would have 1, 2, 3, 4, 5, 6, 7, 8, 9, and so on. Then the zeros column would have 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, and so on. The engineers would put the report in the correct column and then just press the print button.This is 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
  • #1
issacnewton
1,026
36
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: 394
Technology news on Phys.org
  • #2
The minimum number of digits needed to represent 0 (in fortran) is zero, so the blank is acceptable.
 
  • #3
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
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
well jedi, that's not true. I changed index to 12 and with descriptor [itex]2I5.0[/itex] , the values now printed are 12 for index and 24 for index+12.
 
  • #6
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
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.
 
  • #8
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
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:

Related to Question on I descriptor in Fortran

1. What is an I descriptor in Fortran?

An I descriptor in Fortran is used for integer data types and specifies the number of characters or digits to be used when displaying or reading data. It is used in I/O statements to control the formatting of data.

2. How is an I descriptor different from other descriptors in Fortran?

An I descriptor is specifically used for integer data types, while other descriptors such as A and F are used for character and floating-point data types, respectively. Additionally, I descriptors can be used in I/O statements for both reading and writing data.

3. What happens if the number of specified digits in an I descriptor is greater than the value being displayed?

If the number of specified digits in an I descriptor is greater than the value being displayed, the extra digits will be filled with leading zeros. For example, if the value is 5 and the I descriptor is specified as I5, the output will be 00005.

4. Can an I descriptor be used for non-numeric data?

No, an I descriptor is specifically used for integer data types. Attempting to use it for non-numeric data may result in errors or unexpected output.

5. How can I use an I descriptor to read and write data from a file in Fortran?

To use an I descriptor in file I/O, you can use the READ and WRITE statements along with the I descriptor to specify the format of the data being read or written. Additionally, you can also use the FORMAT statement to define the format of the data to be read or written using I descriptors.

Similar threads

  • Programming and Computer Science
Replies
11
Views
10K
  • Programming and Computer Science
Replies
5
Views
2K
Replies
11
Views
2K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
12
Views
8K
  • Programming and Computer Science
Replies
10
Views
9K
  • Programming and Computer Science
Replies
13
Views
4K
  • Programming and Computer Science
Replies
7
Views
135K
  • Programming and Computer Science
Replies
1
Views
2K
Back
Top