Understanding Character Data Types in Fortran
Learn about complex numbers, character data, and how to declare, assign, and manipulate strings, along with formatting and handling various data types in Fortran programming.
Understanding Character Data Types in Fortran
E N D
Presentation Transcript
MET 50 CHARACTER DATA
Character data We have extensively used REAL and INTEGER data types. There is also: CHARACTER data COMPLEX data LOGICAL data MET 50, FALL 2011
Character data COMPLEX numbers take the form: Z = X + iY where X and Y are both real numbers and i2= -1, so that i = (-1) MET 50, FALL 2011
Character data Z = X + iY X is called the real part of Z {denoted Re(Z)} REAL (X) value Y is called the imaginary part of Z {denoted Im(Z)} AIMAG (X) value MET 50, FALL 2011
Character data Fortran stores Z as two real numbers: X and Y When you write PRINT*,Z You get: (12.0, -9.0) MET 50, FALL 2011
Character data CHARACTER DATA Letters, spaces, punctuation marks Combined into something called a STRING MET 50, FALL 2011
Character data Written in code as follows: “STRING” or ‘STRING’ MET 50, FALL 2011
Character data Declared as follows: CHARACTER (n) :: string Where “n” is the length = number of characters in the string MET 50, FALL 2011
Character data example: CHARACTER (3) :: month Then “month” is type character with 3 elements, such as janfebmar Perhaps you want to print a table of results with the month printed at the top … see later for format! MET 50, FALL 2011
Character data Other ways to declare: CHARACTER (LEN=n) :: string1 CHARACTER :: string2 Length in 2nd example is assumed to be ONE!!! MET 50, FALL 2011
Character data Other ways to declare: CHARACTER :: stringa*10, stringb*20, stringc*25 MET 50, FALL 2011
Character data To specify the value of a string: CHARACTER (8) :: name name=“John Doe” Note that the space counts as a character!! CHARACTER (21) :: filename filename=“temperature_data_2005” MET 50, FALL 2011
Character data Formatted read/write of a string: In the FORMAT statement, use the descriptor: “An” As in: format (1x, I5, A20, F10.2) Options: “I”, “F”, “E”, and now…”A” (also “D”…last slides) MET 50, FALL 2011
Character data Unformatted read/write: CHARACTER (3) :: MONTH INTEGER :: NUMBER READ*,MONTH, NUMBER You might enter: “JAN” 150 MET 50, FALL 2011
Character data Defects of character (hahaha!) If a string is read in that is too long, it is truncated on the right. CHARACTER (6) :: DAY DAY = “WEDNESDAY” DAY is stored as WEDNES MET 50, FALL 2011
Character data If a string is read in that is too short, it is padded with blanks on the right. CHARACTER (12) :: DAY DAY = “WEDNESDAY” DAY is stored as “WEDNESDAY ” MET 50, FALL 2011
Character data Character data can be “added” !!! The operation is called: concatenation “cat” for short character :: first*5, second*6, third*11 first = ‘minor’ second = ‘ thing’ third = first // second produces… third = ‘minor thing’ MET 50, FALL 2011
Character data Useful??? Creation of file names inside some code… character :: first*9, second*4, third*9 character :: title*22 first = ‘data-for-’ third = ‘-smoothed’ DO YEAR = 1,10 if (year == 10) then second = ‘2010’ title = first // second // third endif ! Produces title = ‘data-for-2010-smoothed’ OPEN (15, file=‘title’) END DO MET 50, FALL 2011
Character data DOUBLE PRECISION data… Suppose you declare: REAL :: TEMP and later print a value with PRINT*,TEMP a number with certain number of digits after the decimal. MET 50, FALL 2011
Character data Instead if you declare: DOUBLE PRECISION:: TEMP and later print a value with PRINT*,TEMP • a number with DOUBLE the number of digits after the decimal. • Double precision! MET 50, FALL 2011
Character data This may happen if you get some code that has run on a high-end machine (e.g., 64-bit) and you now want to run it on a cheapo workstation (e.g., 16-bit) … less resolution of numbers … less accuracy See page 230 !! MET 50, FALL 2011