120 likes | 244 Vues
This text explores the concept of significant digits, illustrating how different measurements can impact calculations and results. It emphasizes the importance of understanding effective digits when performing addition and multiplication, providing examples related to weight and area. Additionally, it covers the data types and their precision in the FORTRAN programming language, including integers, real numbers, and double precision. Learn how to format output and avoid common mistakes related to type assumptions in coding. Mastering these concepts is vital for accurate scientific computation.
E N D
Question • Which is taller? • Fuku’s height: 186 cm • Masa’s height: 186.3 cm Ans: We don’t know… because Fuku could be 186.0 cm or 186.4 cm 186 cm and 186.0 cm means different !!
Significant digits • How about 0.00186 km? • To express the meaningful part of the numbers, we use significant digits(有効数字) • 186 cm : significant digits = 3 • 186.0 cm: significant digits = 4 • 0.00186 km: significant digits = 3 • 190cm: significant digits = 2 or 3 • To make the number of significant digits clear, use floating point notation • 1.9*102 : significant digits = 2 • 1.90*102: significant digits = 3
Calculation and Effective digits • Addition (+) • If we put 25g of potato chips into 1.3g of bag, what is the total weight? WRONG 25 g + 1.3 g = 26.3 g Ans. 26.3 g CORRECT 25 g + 1.3 g = 26.3 g Ans. 26 g Addition: The effective digit of the answer is determined by the most coarse element of the formula.
Calculation and Effective digits • Multiplication (×) • What is the area of a rectangle which is1.3 m (height) by 21.1 m (width) ? WRONG 1.3 m × 21.1 m = 27.43 m2Ans. 27.43 m2 CORRECT 1.3 m × 21.1 m = 27.43 m2Ans. 27 m2 Multiplication: The effective digit of the answer is determined by the smallest effective digit of the formula.
Question • Write your answer for… Calculate the radius of our earth, when π= 3.14 length of equator = 4×104 km
Types in FORTRAN • integer -32768 ~ 32768 • integer*4 -2147483648 ~ 2147483648 • real*4 (real) effective digit = 7 • real*8 (double) effective digit = 15 • complex effective digit = 7 • complex*16 effective digit = 15 • logical .true or .false • character*<size> a string with a length of <size>
Computers can only calculate a formula with homogeneous types !! We need to choose type with big enough effective digit, and check the actual effective digit by yourself.
Understanding types clearly !! program typeTest write (*,*) '4/3=', 4/3 ! int / int int write (*,*) '4.0/3=', 4.0/3 ! real / int real write (*,*) '4/3.0=', 4/3.0 ! int / real real write (*,*) '4.0/3.0=', 4.0/3.0 ! real / real real write (*,*) '4.0d0/3=', 4.0d0/3 ! double / int double write (*,*) '4.0d0/3.0d0=', 4.0d0/3.0d0 ! double / double double stop end 福島康裕@mango ~ $ ./a.exe 4/3= 1 4.0/3= 1.33333337 4/3.0= 1.33333337 4.0/3.0= 1.33333337 4.0d0/3= 1.33333333 4.0d0/3.0d0= 1.33333333 integer real*4 (real) real*8 (double precision)
FORTRAN assumes types… • If we do not declare the numbers like… double r it will assume the type of r. • The assumption is sometimes not the same with how you would want to use r… LOTS OF MISTAKES (T_T)
Checking types to avoid mistakes • Start your code with implicit none ! • If you write this, FORTRAN will check types of parameters for you, when you forget to declare them. program test … x = 4.0 / 3.0 * pi *r**3 stop end program test implicit none real*8 x … x = 4.0 / 3.0 * pi *r**3 stop end f77 will give us a warning !! f77 will assume it is real*4 !!
Formatting output in FORTRAN program typeTest write (*,*) 4/3 write (*,*) 4.0/3 write (*,*) 4/3.0 write (*,*) 4.0/3.0 write (*,*) 4.0d0/3 write (*,*) 4.0d0/3.0d0 write (*,21) 4/3 write (*,21) 4.0/3 write (*,21) 4/3.0 write (*,21) 4.0/3.0 write (*,21) 4.0d0/3 write (*,21) 4.0d0/3.0d0 21 format (e16.10) stop end write (*,11) 4/3 write (*,11) 4.0/3 write (*,11) 4/3.0 write (*,11) 4.0/3.0 write (*,11) 4.0d0/3 write (*,11) 4.0d0/3.0d0 11 format (f12.10) f12.10 : Use floating decimal point notation, 12 characters in total, 10 characters after decimal point e16.10 : Use exponential notation, 16 characters in total, 10 characters after the decimal points
Different formats 福島康裕@mango ~ $ ./a.exe 1 1.33333337 1.33333337 1.33333337 1.33333333 1.33333333 0.0000000000 1.3333333731 1.3333333731 1.3333333731 1.3333333333 1.3333333333 0.1401298464E-44 0.1333333373E+01 0.1333333373E+01 0.1333333373E+01 0.1333333333E+01 0.1333333333E+01 * : Use default notation f12.10 : Use floating decimal point notation, 12 characters in total, 10 characters after decimal point e16.10 : Use exponential notation, 16 characters in total, 10 characters after the decimal points