Likewise, if you insert the statement A = sqrt (5.), Fortran will truncate sqrt (5.) = 2.23606801 and deduce that A = 2. If you declare that A is an integer and later make the assignment A = 3.45, Fortran will not complain but it will truncate 3.45 and assign A the value A = 3. The compiler will have no objection if N is an integer variable and you ask Fortran to compute a composition like sqrt(real(N)) or cos(real(N)). Which converts x to a real number (if it is not already one). One way around this problem is to use the intermediate function Likewise, if you declare N to be an integer variable and ask Fortran to compute sqrt(N) or cos(N) or log(N), your program will not compile since these functions cannot act on integers. The compiler will protest if you ask Fortran to compute sqrt(5), but it has no problem with sqrt(5.). Most of the built-in functions in Fortran apply to real numbers, and attempts to apply them to integers result in compiler error messages. Integer arithmetic in Fortran can lead to other weird surprises - for instance, the distributive law of division is invalid, as demonstrated by the example If you want Fortran to give you the correct value of 11/8, you tell it to compute 11./8., so that it interprets the numbers as real numbers and produces the correct value 1.375. If the answer is not really an integer, Fortran makes it one by discarding the decimal point and all digits thereafter. One idiosyncrasy of Fortran is that when it performs arithmetic on integers, it insists on giving an answer that is likewise an integer. and 4.0 as real numbers.Īn integer N in GNU Fortran must lie within the range Examples of numbers not considered integers by Fortran areīecause of the decimal points, Fortran will regard 3. Examples of numbers considered integers by Fortran areįor positive integers the plus sign is optional, but negative integers must be preceded by a minus sign. In this lesson we look more closely at some of the rules and conventions that must be observed.Īn integer in Fortran is a whole number it cannot contain commas or a decimal point. Whereas Basic is more lenient, allowing some flexibility in mixing variables and numbers of different types, Fortran is less forgiving and will make you pay for oversights. To avoid mistakes in Fortran arithmetic you must pay close attention to rules regarding working with numbers of the various types. In Fortran Lesson 1 we briefly looked at the types of variables in Fortran. Fortran Lesson 3 FORTRAN LESSON 3 Lesson Topics
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
January 2023
Categories |