1 / 36

Basic Elements of Fortran

Basic Elements of Fortran. by Hsu-cherng Chiang. The History of the FORTRAN. FORmula TRANslation IBM developed the first version of the FORTRAN language (1954) IBM released FORTRAN II (1958) FORTRAN IV(1962) In 1966 FORTRAN IV was adopted as an ANSI Standard, and known as FORTRAN 66

evette
Télécharger la présentation

Basic Elements of Fortran

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Basic Elements of Fortran by Hsu-cherng Chiang

  2. The History of the FORTRAN • FORmula TRANslation • IBM developed the first version of the FORTRAN language (1954) • IBM released FORTRAN II (1958) • FORTRAN IV(1962) • In 1966 FORTRAN IV was adopted as an ANSI Standard, and known as FORTRAN 66 • FORTRAN 77 (1977) • FORTRAN 90

  3. 為什麼我們要學FORTRAN? • 計算效率高,特別適合工和科學計算 • 簡單易學 • 因為有ANSI和ISO之標準,較容易跨平台使用 • 已經有許多現存FORTRAN程式,因為惰性且不容易轉換為其他語言而繼續使用

  4. 程式分類 • 直譯式 : MATLAB, BASIC • 編譯式 : FORTRAN, C, C++

  5. Compiling, Linking, and Executing compile Library FORTRAN Program Objective File Executable program link compile

  6. 常見的FORTRAN Compiler • Microsoft Fortran Powerstation • Compaq Visual Fortran – CVF 6.6 • Lahey Fortran • G77 • Portland Group Fortran

  7. Fixed form vs. Free-source form • Fixed formFortran 77 and earlier version80 characters per line1-6 statement labels7 continum8-72 statement • Free-source formFortran 90, 95132 character per line&連續

  8. A simple FORTRAN program • PROGRAM my_first_program! Purpose! To illustrate some of the basic features of a FORTRAN programs. • ! • INTEGER :: I, J, K ! all variables are integers • ! Get the variables to multiply together. • WRITE(*,*) ‘Enter the number to multiply: ‘ • READ(*,*) I,J • K = I*J • WRITE(*,*)’RESULTS = ‘, K • STOP • END PROGRAM

  9. Fortran Character Set • 26 Uppercase letters of alphabet: A through Z • 26 Lowercase letters of alphabet: a through z • 10 Digits: 0 through 9 • 1 Underscore character _ • 5 Arthimetic symbols + - * / ** • 18 Miscellaneous symbols ().=,’$:!”%&;<>?$

  10. FORTRAN program • Declaration section • execution section • termination section

  11. The Declaration Section • PROGRAM name • up to 31 characters long and contain any combination of alphabetic characters, digits and the underscore ( _ ) • The first character must be alphabetic. • INTEGER

  12. Execution section

  13. Termination section • STOPTo stop running the program. • END PROGRAMtell the compiler that there are not more statements to be compiled in the program.

  14. Constant and Variables • A constant does not change values during the execution of the program. • A variable can change values during the execution of a program. • Each variable must have a unique name. • Variable name may up to 31 characters long and contain any combination of alphabetic characters, digits and the underscore ( _ ). • The first character must be alphabetic.

  15. Variable name • Valid variable namestimedistancez123456789I_want_to _go_home • Invalid variable nameThis_is _a_very_long_variable_name3_dayA$my-helpexchange rate

  16. Intrinsic types of variables • Numerical INTEGERREALCOMPLEX • LogicalLOGICAL • Strings of charactersCHARACTER

  17. Default and Explicit Variable Typing • Default type如果變數名稱第一個字母為I,J,K,L,M,N,則預設為整數,如開頭字母為其他字母則預設為實數 • Explicit definedINTEGER:: var1, var2, var3,......REAL :: var1, var2, var3, ...LOGICAL:: var1, var2, var3, ...

  18. Integer Constant and Variables • An integer is any number that does not contain a decimal point. • 32 bits integer -2147483648 <---> 2147483647 • INTEGER:: year, month, day

  19. REAL Constants and Variables • A real constant is any number with a decimal point. • 10.-999.91.eE-3123.45E20 • Precision and range

  20. Character constants and variables • A character constant is astring of characters enclosed in single or double quotes. • The maximum number of characters is often as large as 32767. • ‘This is a test.’“This is a test”“Man’s best friend”

  21. Logical constants and Variables • A logical constant can take one of two possible values: .TRUE. or .FALSE..TRUE..FALSE.

  22. PARAMETER • Named constants are created using the PARAMETER attribute. • type, PARAMETER:: name = value • REAL, PARAMETER:: PI=3.1415926INTEGER, PARAMETER :: IMAX = 100CHARACTER PARAMETER:: errmsg =“Eoorr!”INTEGER, PARAMETER::I=12,J=20,K=25

  23. Assign Statement • Variable_name = expressiona = 1.2b = 3.5c = a + b/5.2a = a + 1

  24. Standard arithmetic operators • + addition- substraction* multiplication/ division** exponentiation • unary operators+23 -a

  25. 注意 • 兩個運算元不能連在一起a * - b ==> a*(-b)a**-2 ==> a**(-2) • 乘號不可省略a(b+c) ==> a * (b+c) • 適當地用括號讓計算式看起來較清楚

  26. 整數相除 • 整數相除其結果仍然為整數,如果相除結果不過整數,電腦會自動將小數點後之值刪除 • 3/4 = 04/4 = 15/4 = 16/4 = 18/ 4 = 2

  27. 計算優先順序 • 括號內先算,由最內層往外層算 • 其次為指數,由右往左算 • 再次為乘,除運算,由左往右依序算 • 最後為加,減運算,由左往右算

  28. Mixed-mode Arithmetic • 1 + 1 / 4 11. + 1 / 4 1.1 + 1./4 1.25 • 想想看,下面計算結果是否相同ave = (n1 + n2 ) / 2ave = (n1 + n2 ) / 2.

  29. Relational Operators • == Equal to (.eq.)/= Not equal to (.ne.)> Greater than (.gt.)>= Greater than or equal to (.ge.)< Less than (.lt.)<= Less than or equal to (.le.)

  30. Combinational Logic Operators • l1 .AND l2 l1.OR. l2l1.EQV. l2 Logical equivalencel1.NEQV. l2 Logical non-equivalence.NOT. l1 Logical NOT

  31. Character Variables • CHARACTER(LEN=3)::FILE_EXTCHARACTER(LEN=3)::FILE_EXT_2FILE_EXT=‘F’FILE_EXT_2 = ‘FILE01’ • FILE_EXT = ‘F ‘FILE_EXT_2 = ‘FIL’

  32. Substring Specification • STR1=‘1234567’STR1(2:4) ==> ‘234’ • CHARACTER(LEN=8)::A,B,CA = ‘ABCDEFGHIJ’B = ‘12345678’C = A(5:7)B(7:8)=A(2:6)

  33. Concatenation(//) operator • CHARACTER(LEN=10):: ACHARACTER(LEN=8):: B, CA = ‘ABCDEFGHIJ’B = ‘12345678’C = A(1:3)//B(4:5)//A(6:8)END PROGRAM • C = ‘ABC45FGH’

  34. Relational operators withcharacter data • ‘123’==‘123’‘123’/=‘1234’‘A’<‘B’ (A 65, B 66)‘A’ < ‘a’ (A 65, a 97)‘AAAAB’ > ‘AAAAA’‘AB’ > ‘AAAA’‘AAAA’>‘AAA’

  35. INTRINSIC FUNCTION(內存函數) • PROGRAM testREAL, PARAMETER:: conv = 3.14159/180.REAL:: ang, yy = SIN(ang*conv)write(*,*) ang, ySTOPEND PROGRAM test

  36. EXAMPLES • y = SIN(3.1415926)y = SIN(x)y = SIN(pi*x)y = SIN(SQRT(x))

More Related