This preview shows pages 1–3. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Chapter 4 More on Intrisic Scalar Data Types Fortran was developed primarily for computations and as such has builtin capabil ities to deal with data types common in mathematical fields as varied as complex numbers, linear algebra, optimization, and differential equations. Here we present briefly some of the other intrinsic data types we will encounter, complex numbers and character constants. We also give some details as to how computers represent integers and floating point numbers. 4.1 Complex Complex number computations requires special handling for the real and imaginary parts as shown in the calculation below: (1 + 2 i ) + (3 + 4 i )(5 + 6 i ) = 1 + 2 i + 15 + 18 i + 20 i + 24 i 2 (4.1) = (1 + 15 24) + (2 + 18 + 20) i (4.2) = 8 + 40 i (4.3) First the cross products are found, and then remembering that i 2 = 1 we collect the real and imaginary components separately. We could code this logic using REAL data types only but it makes for complicated code. Fortran being a pro gramming language for numerical computations has builtin support for complex arithmetic using the complex versions of the algebraic operators defined on REAL s. This is much more convenient codewise. The following code illustrates how com plex numbers are declared, assigned values and manipulated: program cmplx complex :: a=(1.0,2.0),b=(3.0,4.0),c=(5,6), d d = a + b*c 25 26 CHAPTER 4. MORE ON INTRISIC SCALAR DATA TYPES print *,d print *, ’The real and imaginary parts are=’,real(d), aimag(d) stop end program cmplx Fortran stores complex numbers as pairs of REAL s, and/or pairs of DOUBLEPRECISION s. The real and imaginary parts of a complex number can be extracted using intrinsic functions real and aimag . 4.2 Character FORTRAN provides the intrinsic data type character to represent alphabetical characters, and strings of characters. We will give a brief overview of this data type as it is not central to numerical computing. A single character constant occupies one byte of memory and is declared as followed: character :: letter=’a’ character(len=4) :: letters=’abcd’ character(len=21) :: hello=’Hello World!’ The variable letter is declared as a character and is assigned the single letter a. To store multiple characters, like a string, a length must be assigned to the character variables, essentially the number of bytes allocated for that variable, as shown in the letters and hello declarations. In the first one letters is given enough space to have 4 characters within it whereas hello has 21. Notice that the string Hello World! has only 12 characters; the remaining characters 13–21 are left blank. Substrings of characters can be referenced using a colon notation: print *,letters(1:2) ! will print ab print *,letters(3:4) ! will print cd It is an error to reference a character beyong the limit defined in the variable declaration. The only operation permitted on characters is concatenation and usesdeclaration....
View
Full
Document
 Fall '08
 Staff

Click to edit the document details