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. ![]() One idiosyncrasy of Fortran is that when it performs arithmetic on integers, it insists on giving an answer that is likewise an integer. If the answer is not really an integer, Fortran makes it one by discarding the decimal point and all digits thereafter. 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. Most of the built-in functions in Fortran apply to real numbers, and attempts to apply them to integers result in compiler error messages. The compiler will protest if you ask Fortran to compute sqrt(5), but it has no problem with sqrt(5.). 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. Which converts x to a real number (if it is not already one). 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)). 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. Likewise, if you insert the statement A = sqrt (5.), Fortran will truncate sqrt (5.) = 2.23606801 and deduce that A = 2. GNU Fortran uses up to 9 digits, not counting the decimal point, to represent real numbers.īut errors such as these are easily avoided if you are careful to make correct type declaration statements for all variables at the beginning of your program.Ī real number, or more precisely a single precision real number, is written with a decimal point by Fortran, even when it is a whole number. ![]() įortran can use also scientific notation to represent real numbers. The sequence "En" attached to the end of a number, where n is an integer, means that the number is to be multiplied by 10 n.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |