Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Question on I descriptor in Fortran

  1. Jan 2, 2012 #1
    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
     

    Attached Files:

    • 1.jpg
      1.jpg
      File size:
      26.8 KB
      Views:
      56
  2. jcsd
  3. Jan 2, 2012 #2
    The minimum number of digits needed to represent 0 (in fortran) is zero, so the blank is acceptable.
     
  4. Jan 3, 2012 #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.
     
  5. Jan 3, 2012 #4

    jedishrfu

    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.
     
  6. Jan 3, 2012 #5
    well jedi, thats 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.
     
  7. Jan 3, 2012 #6

    jedishrfu

    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.
     
  8. Jan 3, 2012 #7

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    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.
     
  9. Jan 3, 2012 #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.
     
  10. Jan 4, 2012 #9

    jedishrfu

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

    jedishrfu

    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
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Question on I descriptor in Fortran
  1. Question - Fortran (Replies: 2)

  2. FORTRAN Question (Replies: 4)

  3. Fortran question (Replies: 2)

  4. Fortran Question (Replies: 32)

Loading...