Decimal form. Fw.d Exponential form Ew.d Ew.dEe Scientific form ESw.d ESw.dEe Engineering form ENw.d ENw.dEe Lw. Horizontal



Similar documents
A Short Guide to Significant Figures

COMP 250 Fall 2012 lecture 2 binary representations Sept. 11, 2012

.NET Standard DateTime Format Strings

2.2 Scientific Notation: Writing Large and Small Numbers

Rational Exponents. Squaring both sides of the equation yields. and to be consistent, we must have

Unit 1 Number Sense. In this unit, students will study repeating decimals, percents, fractions, decimals, and proportions.

To convert an arbitrary power of 2 into its English equivalent, remember the rules of exponential arithmetic:

Computer Science 281 Binary and Hexadecimal Review

26 Integers: Multiplication, Division, and Order

Chapter 4: Computer Codes

NUMBER SYSTEMS. William Stallings

5.1 Radical Notation and Rational Exponents

A.2. Exponents and Radicals. Integer Exponents. What you should learn. Exponential Notation. Why you should learn it. Properties of Exponents

Part 1 Expressions, Equations, and Inequalities: Simplifying and Solving

The gas can has a capacity of 4.17 gallons and weighs 3.4 pounds.

Multiplying and Dividing Signed Numbers. Finding the Product of Two Signed Numbers. (a) (3)( 4) ( 4) ( 4) ( 4) 12 (b) (4)( 5) ( 5) ( 5) ( 5) ( 5) 20

Digital System Design Prof. D Roychoudhry Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Session 7 Fractions and Decimals

The string of digits in the binary number system represents the quantity

Binary Number System. 16. Binary Numbers. Base 10 digits: Base 2 digits: 0 1

Binary Adders: Half Adders and Full Adders

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)

How to Calculate the Probabilities of Winning the Eight LUCKY MONEY Prize Levels:

Negative Integer Exponents

THE BINARY NUMBER SYSTEM

Useful Number Systems

Lecture 11: Number Systems

Solution for Homework 2

Section 4.1 Rules of Exponents

Base Conversion written by Cathy Saxton

Student Outcomes. Lesson Notes. Classwork. Discussion (10 minutes)

=

Number Conversions Dr. Sarita Agarwal (Acharya Narendra Dev College,University of Delhi)

Section 1.5 Exponents, Square Roots, and the Order of Operations

1 Description of The Simpletron

Quick Reference ebook

Negative Exponents and Scientific Notation

Data Storage. Chapter 3. Objectives. 3-1 Data Types. Data Inside the Computer. After studying this chapter, students should be able to:

Section 1.4 Place Value Systems of Numeration in Other Bases

This 3-digit ASCII string could also be calculated as n = (Data[2]-0x30) +10*((Data[1]-0x30)+10*(Data[0]-0x30));

Fractions to decimals

23. RATIONAL EXPONENTS

A Programming Language for Mechanical Translation Victor H. Yngve, Massachusetts Institute of Technology, Cambridge, Massachusetts

1. Give the 16 bit signed (twos complement) representation of the following decimal numbers, and convert to hexadecimal:

Properties of Real Numbers

Recall the process used for adding decimal numbers. 1. Place the numbers to be added in vertical format, aligning the decimal points.

MULTIPLICATION AND DIVISION OF REAL NUMBERS In this section we will complete the study of the four basic operations with real numbers.

Data Storage 3.1. Foundations of Computer Science Cengage Learning

Teaching Pre-Algebra in PowerPoint

This Unit: Floating Point Arithmetic. CIS 371 Computer Organization and Design. Readings. Floating Point (FP) Numbers

Chapter 7: Additional Topics

Lecture 2. Binary and Hexadecimal Numbers

Figure 1. A typical Laboratory Thermometer graduated in C.

MATH-0910 Review Concepts (Haugen)

Binary Division. Decimal Division. Hardware for Binary Division. Simple 16-bit Divider Circuit

Formatting Numbers with C++ Output Streams

This is a square root. The number under the radical is 9. (An asterisk * means multiply.)

Notes on Complexity Theory Last updated: August, Lecture 1

Binary, Hexadecimal, Octal, and BCD Numbers

The Mathematics 11 Competency Test Percent Increase or Decrease

47 Numerator Denominator

Training Manual. Pre-Employment Math. Version 1.1

Row Echelon Form and Reduced Row Echelon Form

2015 County Auditors Institute. May Excel Workshop Tips. Working Smarter, Not Harder. by David Scott,

CHAPTER 5 Round-off errors

plc numbers Encoded values; BCD and ASCII Error detection; parity, gray code and checksums

Review of Scientific Notation and Significant Figures

SYSTEMS OF EQUATIONS AND MATRICES WITH THE TI-89. by Joseph Collison

Systems I: Computer Organization and Architecture

Fraction Tools. Martin Kindt & Truus Dekker n

Solutions of Linear Equations in One Variable

MATLAB Programming. Problem 1: Sequential

PREPARATION FOR MATH TESTING at CityLab Academy

CHAPTER 4 DIMENSIONAL ANALYSIS

2010/9/19. Binary number system. Binary numbers. Outline. Binary to decimal

IB Math Research Problem

Welcome to Basic Math Skills!

JobTestPrep's Numeracy Review Decimals & Percentages

Ready, Set, Go! Math Games for Serious Minds

Binary Representation. Number Systems. Base 10, Base 2, Base 16. Positional Notation. Conversion of Any Base to Decimal.

VALUE %. $100, (=MATURITY

Automata and Computability. Solutions to Exercises

LESSON PLANS FOR PERCENTAGES, FRACTIONS, DECIMALS, AND ORDERING Lesson Purpose: The students will be able to:

pi: in the examples below.

Mathematical Induction

Exponents. Learning Objectives 4-1

Integer Operations. Overview. Grade 7 Mathematics, Quarter 1, Unit 1.1. Number of Instructional Days: 15 (1 day = 45 minutes) Essential Questions

Math Workshop October 2010 Fractions and Repeating Decimals

HOMEWORK # 2 SOLUTIO

Secrets of printf. 1 Background. 2 Simple Printing. Professor Don Colton. Brigham Young University Hawaii. 2.1 Naturally Special Characters

5.1 Introduction to Decimals, Place Value, and Rounding

Numeral Systems. The number twenty-five can be represented in many ways: Decimal system (base 10): 25 Roman numerals:

Rational Number Project

Basic numerical skills: FRACTIONS, DECIMALS, PROPORTIONS, RATIOS AND PERCENTAGES

Guidance paper - The use of calculators in the teaching and learning of mathematics

All the examples in this worksheet and all the answers to questions are available as answer sheets or videos.

Review of Fundamental Mathematics

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

Commonly Used Excel Formulas

Pre-Algebra Lecture 6

Transcription:

Fortran Formats The tedious part of using Fortran format is to master many format edit descriptors. Each edit descriptor tells the system how to handle certain type of values or activity. Each value requires some positions. For example, an integer of four digits requires at least four positions to print. Therefore, the number of positions to be used is the most important information in an edit descriptor. We shall use the following convention of symbols: w: the number of positions to be used m: the minimum number of positions to be used d: the number of digits to the right of the decimal point e: the number of digits in the exponent part Although we may print a number using as many positions as you want, this is only for input/output. This number of positions is not the precision (or the number of significant digits) of that number. To be more precisely, computers normally can store real numbers up to seven significant digits. This is the precision of real numbers. However, we can print a real number using 50 positions in which 25 positions are for the fraction part. This is only a way of describing the appearance and does not change the precision of real numbers. The following are the editor descriptors: Purpose Edit Descriptors Reading/writing INTEGERs Iw Iw.m Reading/writing REALs Reading/writing LOGICALs Decimal form Fw.d Exponential form Ew.d Ew.dEe Scientific form ESw.d ESw.dEe Engineering form ENw.d ENw.dEe Lw Reading/writing CHARACTERs A Aw Positioning Others Horizontal nx Tabbing Tc TLc and TRc Vertical / Grouping r(...) Format Scanning Control : Sign Control S, SP and SS Blank Control BN and BZ Most edit descriptors can be repeated and several edit descriptors can be grouped into a group. 1/12

INTEGER Output: The I Descriptor The Iw and Iw.m descriptors are for INTEGER output. The general form of these descriptors are as follows: The meaning of r, w and m are: riw and riw.m I is for INTEGER w is the width of field, which indicates that an integer should be printed with w positions. m indicates that at least m positions (of the w positions) must contain digits. If the number to be printed has fewer than m digits, leading 0s are filled. If the number to be output has more than m digits, m is ignored and in this case Iw.m is equivalent to Iw. Note that w must be positive and larger than or equal to m. It is interesting to note that m can be zero! That is, no digits should be printed. In this case, if the number to be printed is non-zero, it will be printed as if Iw is used. However, if the number is zero, all w positions will be filled with spaces! r is the repetition indicator, which gives the number of times the edit descriptor should be repeated. For example, 3I5.3 is equivalent to I5.3, I5.3, I5.3. The sign of a number also needs one position. Thus, if -234 is printed, w must be larger than or equal to 4. The sign of a positive number is not printed. What if the number of positions is less than the number of digits plus the sign? In other words, what if a value of 12345 is printed with I3? Three positions are not enough to print the value of five digits. In this case, traditionally, all w positions are filled with *'s. Therefore, if you see a sequence of asterisks, you know your edit descriptor does not have enough length to print a number. Examples Let us look at the following example. There are three INTEGER variables a, b and c with values 123, -123 and 123456, respectively. In the following table, the WRITE statements are shown at the left and their corresponding output, all using five positions, are shown at the right. 2/12

The first line uses (I5) to print the value of 123. Thus, digits 1, 2 and 3 appear at the right end and two leading spaces are filled. The second line uses (I5.2) to print 123. This means of the five positions, two positions must contain digits. Since the value 123 has already had 3 digits. Therefore,.2 is ignored and the result is the same as that of the first line. The third line uses (I5.4) to print 123. Since m = 4, four positions must be filled with digits. Since 123 has only three digits, a leading 0 must be inserted. Thus, in the output, the five positions contain a space, 0, 1, 2 and 3. The fourth line uses I5.5 and therefore forces two 0s to be inserted. The fifth line uses (I5) to print -123. Since the number is negative, a minus sign is printed. The sixth line produces the same result as that of the fifth line. The seventh line tells us that if leading zeros must be inserted, they are inserted between the minus sign and the number. The eighth line uses (I5.5) to print -123. This is not a good edit descriptor. I5.5 means to print a number using five positions and all five positions must be filled with digits. If the number is positive, there is no problem as shown in the fourth line. However, if the number is negative, there will be no position for the minus sign. As a result, all five positions are filled with asterisks, indicating a problem has occurred. The last line uses I5.2 to print 123456. The number has six digits and the number of positions to print this number is five. Thus, the given number 123456 cannot be printed completely and all five positions are filled with asterisks. Consider the following example. The WRITE statement has three INTEGER variables and consequently the format must also have three I edit descriptors, one for each variable. INTEGER :: a = 3, b = -5, c = 128 WRITE(*,"(3I4.2)") a, b, c The edit descriptor is 3I4.2 and the format is equivalent to (I4.2,I4.2,I4.2) because the repetition indicator is 3. Therefore, each of the three INTEGER variables is printed with I4.2. Based on the discussion earlier, the result is the following: 3/12

REAL Output: The F Descriptor The Fw.d descriptor is for REAL output. The general form is: The meaning of r, w and d are: rfw.d F is for REAL w is the width of field, which indicates that a real number should be printed with w positions. d indicates the number of digits after the decimal point. More precisely, of these w positions, the right-most d positions are for the fraction part of a real number, and the d+1 position from the right is a decimal point. The remaining w-(d+1) positions are for the integral part. This is shown in the figure below: When a real number is printed, its integral part is printed as if it uses Im-(d+1). Therefore, if the number of positions is not enough to print the integral part completely, all w positions will be filled with asterisks. Please note that the integral part contains a sign and as a result w should be greater than or equal to d+2. The fractional part may have more than d digits. In this case, the (d+1)th digit will be rounded to the dth one. For example, if 1.73 is printed with F3.1 (using 3 position to print a real number with the right-most 1 position for the fractional part), since the second digit of the fractional part is 3, the result is 1.7. However, if 1.76 is printed with F3.1, the result becomes 1.8 because 6 is rounded. The fractional part may have fewer than d digits. In this case, trailing zeros will be added. Note that d can be zero. In this case, no fractional part will be printed. More precisely, the right-most position is the decimal point. r is the repetition indicator, which gives the number of times the edit descriptor should be repeated. For example, 3F5.3 is equivalent to F5.3, F5.3, F5.3. Examples Let us look at the following example. There are two REAL variables a and b with values 123.345 and -123.345, respectively. In the following table, the WRITE statements are shown at the left and their corresponding output, all using five positions, are shown at the right. 4/12

The first line prints 123.345 with F10.0. This means that of the ten positions the rightmost one should contain a decimal point (since d is zero). Therefore, no fractional part is printed. The second line is easy. The third line uses F10.2 to print 123.345. Of these ten positions, the last two are used to print the fractional part (i.e., 345). Therefore, the third digit, 5 is rounded yielding 35. Hence the result is 123.35. The fourth line uses F10.4 to print 123.345. Since there are four positions for the fractional part, which is larger than the number of actual digits, a trailing zero is added. The eighth line uses F10.7 to print 123.345. Since there are only 10-(7+1)=2 positions for printing the integral part which has three digits, all 10 positions are filled with asterisks. The eleventh line has the same result. There are 10-(6+1)=3 positions for the integral part. However, the integral part -123 requires four positions. As a result, all 10 positions are filled with asterisks. Consider the following example. The WRITE statement has three REAL variables and consequently the format must also have three I edit descriptors, one for each variable. REAL :: a = 12.34, b = -0.945, c = 100.0 WRITE(*,"(3F6.2)") a, b, c The edit descriptor is 3F6.2 and the format is equivalent to (F6.2,F6.2,F6.2) because the repetition indicator is 3. Therefore, each of these three REAL variables is printed with F6.2. Based on the discussion earlier, the result is the following: 5/12

REAL Output: The E Descriptor The Ew.d and Ew.dEe descriptors are for REAL output. The printed numbers will be in an exponential form. The general form of these descriptors are discussed below. There are two more forms, ESw.d and ENw.d, which will be discussed at the bottom of this page. The meaning of r, w, d and e are: rew.d and rew.dee E is for REAL numbers in exponential forms. w is the width of field, which indicates that a real number should be printed with w positions. To print a number in an exponential form, it is first converted to a normalized form s0.xxx...xxx*10 sxx, where s is the sign of the number and the exponent and x is a digit. For example, 12.345, -12.345, 0.00123 and -0.00123 are converted to 0.12345*10 2, - 0.12345*10 2, 0.123*10-2 and -0.123*10-2. The Ew.d descriptor generates real numbers in the following form: Of these w positions, the last three are for the exponent, including its sign, the middle d positions are for the number in normalized form. Therefore, excluding the exponent part, there are w-4 positions for print the digits of the normalized number. In fact, you can consider the normalized number is printed with Fw-4.d. The above figure also includes a 0 and a decimal point. If the number is negative, we must also include its sign. Hence, 4+3=7 positions are not available for printing the normalized number and, as a result, when you use the E edit descriptor, w must be greater than or equal to d+7; otherwise, your number cannot be printed properly and all w positions will be filled with asterisks. The Ew.dEe descriptor generates real numbers in the following form: As you can see, the only difference is that the exponent part now has e positions. If your data has an exponent larger than 99 or less than -99, Ew.d will not be able to print it properly because there are only two positions for the exponent (therefore, all w positions will be filled with asterisks). As shown in the figure, in addition to the d positions for the normalized number and e positions for the exponent, we need four more positions for printing the sign in the exponent, a decimal point, a leading 0 and the character E. Moreover, if the number is negative, a sign before the 0 is needed. This means that w must be greater than or equal to d+e+5. r is the repetition indicator, which gives the number of times the edit descriptor should be repeated. For example, 3E20.7E2 is equivalent to E20.7E2, E20.7E2, E20.7E2. 6/12

Examples In the following table, the WRITE statements use different E edit descriptors to print the value of 3.1415926. The WRITE statements are shown at the left and their corresponding output, all using 12 positions, are shown at the right. The first example uses E12.5 to print 3.1415926. This number is first converted to 0.31415926*10 1. Therefore, the normalized number is 0.31415926 and the exponent is 1. The exponent part is printed as E+01. Since we have only 5 positions for printing 0.31415926 which has 8 digits, the sixth one will be rounded to the fifth and the result is 0.31416. The second example uses E12.3E4 to print the same number. This E descriptor indicates that four positions are for the exponent and, as a result, the printed exponent part is E+0001. The last example is obvious. However, if the number is changed to -3.1415926, the result will be 12 asterisks, since there is no position for the sign of the number. Editor Descriptor ESw.d and ESw.dEe Scientists write the exponential form in a slightly different way. This ES edit descriptor is for printing a real number in scientific form, which has a non-zero digit as the integral part. If the number is a zero, then all digits printed will be zero. The following shows the printed form: If you understand the form of normalized number, you can just shift the decimal point to the right one position and decrease the exponent by one. The result is in scientific form. For example, if the number is 34.5678, it has a normalized form 0.345678*10 2. Now shifting the decimal point to the right one position gives 3.45678*10 1. The following shows the output printed with ES12.3E3: 7/12

Editor Descriptor ENw.d and ENw.dEe Engineers write the exponential form in yet another way. In an engineering form, the exponent is always a multiple of three, and the printed number always has no more than three and at least one non-zero digits. For example, suppose the given number is 1234.567. The integral part has four digits and the exponent is zero. To convert this number to an engineering form, the decimal point should be shifted to the left three positions. Thus, the given number has a new form 1.234567*10 3. Similarly, if the given number is 0.00001234567, shifting the decimal point to the right three positions gives 0.01234567*10-3. However, this is not yet in the engineering form, because the integral part is still zero. Therefore, we need to shift the decimal point to the right three positions again and this gives 12.34567*10-6. Now, the number is in engineering form. The following shows the output of ENw.d and ENw.dEe. For ENw.d, it requires 4 positions for the exponent, d positions for the fractional part of the number, 3 positions for the integral part, one position for the decimal point, and one position for the possible sign. It requires at least d+9 positions and consequently for ENw.d, w must be larger than or equal to d+9. The same counting yields that for ENw.dEe, w must be larger than or equal to d+e+7. 8/12

LOGICAL Output: The L Descriptor The Lw descriptor is for LOGICAL output. While Fortran uses.true. and.false. to indicate logical values true and false, respectively, the output only show T and F. The general form of this descriptor is as follows: rlw The meaning of r and w are: L is for LOGICAL w is the width of field, which indicates that a logical value should be printed with w positions. The output of a LOGICAL value is either T for.true. or F for.false.. The single character value is shown in the right-most position and the remaining w-1 positions are filled with spaces. The is shown in the figure below. r is the repetition indicator, which gives the number of times the edit descriptor should be repeated. For example, 3I5.3 is equivalent to I5.3, I5.3, I5.3. Examples Let us look at the following example. There are two LOGICAL variables a and b with values.true. and.false., respectively. In the following table, the WRITE statements are shown at the left and their corresponding output are shown at the right. The first WRITE uses L1 and L2 to print.true. and.false., respectively. Therefore, a T is shown in position one (L1) and a F is shown in position three (L2). The second WRITE uses L3 and L4 to print.true. and.false.. Therefore, a T is printed in the third position of the first three positions, and a F is printed in the fourth position of the next four positions. There are seven positions used in total. 9/12

CHARACTER Output: The A Descriptor The A and Aw descriptors are for CHARACTER output. The general form of these descriptors are as follows: The meaning of r and w are: ra and raw A is for CHARACTER w is the width of field, which indicates that a character string should be printed with w positions. The output of the character string depends on two factors, namely the length of the character string and the value of w. Here are the rules: If w is larger than the length of the character string, all characters of the string can be printed and are right-justified. Also, leading spaces will be added. The following example prints the string "12345" of length 5 (i.e., five characters) using A6. WRITE(*,'(A6)') "12345" Since w is larger than the length of the string, all five characters are printed and right-justified. The result is shown below: If w is less than the length of the character string, then the string is truncated and only the left-most w positions are printed in the w positions. The following example prints the string "12345678" of length 8 (i.e., eight characters) using A6. WRITE(*,'(A6)') "12345678" Since w is less than the length of the string, only the first six characters are printed. The result is shown below: If w is equal to the length of the character string, then it is an exact match. All characters can be printed and all w positions are filled. If w is missing (i.e., edit descriptor A), then the value of w is assumed to be the length of the string. The following example shows two WRITE statements. The first one uses A to print a, a string of length 5. The second one also uses A to print b, a string of length 2. CHARACTER(LEN=5) :: a = "abcde" CHARACTER(LEN=2) :: b = "MI" WRITE(*,'(A)') a WRITE(*,'(A)') b 10/12

Since the A edit descriptor will use the length of the string as the value of w, the above is equivalent to the following: CHARACTER(LEN=5) :: a = "abcde" CHARACTER(LEN=2) :: b = "MI" WRITE(*,'(A5)') a WRITE(*,'(A2)') b Therefore, the A edit descriptor is more convenient than Aw. r is the repetition indicator, which gives the number of times the edit descriptor should be repeated. For example, 3A5 is equivalent to A5, A5, A5, and 3A is equivalent to A, A, A. Please keep in mind that the length of the string includes trailing spaces. For example, if we have the following: CHARACTER(LEN=5) :: a = "123" CHARACTER :: b = "*" WRITE(*,"(A,A)") a, b it is equivalent to CHARACTER(LEN=5) :: a = "123" CHARACTER :: b = "*" WRITE(*,"(A5,A1)") a, b and the result is The first string, a, is of length 5 and hence the first five positions are used. Why are two extra spaces when the string is written as a = "123"? Since the length of a is five and "123" has only three characters, two spaces are added to the right end to make up five characters. Therefore, variable a actually contains five characters: 1, 2, 3 and two spaces. This is why two spaces are between 123 and * in the above output. 11/12

Examples Let us look at the following example. CHARACTER(LEN=5) :: a = "12345" CHARACTER :: b = "*" In the following table, the WRITE statements are shown at the left and their corresponding output are shown at the right. In WRITE statements 1 to 5, since the first A edit descriptor is less than or equal to the length of the character variable, the strings to be printed are truncated from the right. In WRITE statements 6 and 7, since the first A edit descriptor is greater than the length of the character variable, leading spaces are added. Finally, in the eighth WRITE statement, since the first A has no width of field, it is assumed to be equal to the length of the variable. As a result, A5 is used and the result is identical to that of the fifth WRITE. 12/12