Read Chapter 4, and example trig.f. Start Homework 4, Due 1/31
Long before there were scientific calculators, scientists and engineers realized that they needed easy ways to obtain results from common functions like sine, cosine, natural logarithm, and many more. These needs have been considered with each Fortran standard, resulting in a long list of built in functions (intrinsic functions) to make your life easier. I will not cover all of the functions in the current standard (Fortran 90), but will give you some key ones in this and later discussions.
The syntax for use of intrinsic functions may be very familiar to you, because they also appear in spreadsheets. If I enter the magnitude of a velocity in box A1, and the angle between the velocity and the x-axis (in radians) in box A2, then I can compute the x-component of the velocity in box A3 with the formula "=A1*COS(A2)".
In Fortran this result might be obtained with an assignment statement such as
iabs(I) - Absolute value of an integer I (pre-90 Fortran abs didn't like integer arguments.)
sin(x) - Returns the sine of x (x is not an integer)
cos(x) - Returns the cosine of x (x is not an integer)
tan(x) - Returns the tangent of x (x is not an integer)
exp(x) - calculates e (2.7183...) to the x power (x is not an integer)
log(x) - calculates the natural logarithm of x (x is not an integer and > 0)
log10(x) - calculates base 10 logarithm of x (x is not an integer and > 0)
asin(x) - Returns the arcsine ( inverse sine) of x (x is real)
acos(x) - Returns the arccosine ( inverse cosine) of x (x is real)
atan(x) - Returns the arctangent ( inverse tangent) of x (x is real)
sqrt(x) - Returns the square root of x (x is not an integer and > 0)
nint(x) - Returns the nearest integer to the real number x
min(x1,x2,...) - Returns the minimum of x1,x2, .... (arguments must be same type)
max(x1,x2,...) - Returns the maximum of x1,x2, .... (arguments must be same type)
The max and min functions are unusual in that they take any number of arguments. The generic forms "min" and "max" were not a manditory part of the Fortran 77 standard, but are in Fortran 90. In many Fortran 77 codes you will see functions:
amax1(x1,x2,...)- Returns the maximum of x1,x2, ... as a real number (arguments are real)
amax0(i1,i2,...) - Returns the maximum of i1,i2, ... as a real number (arguments are integer)
max0(i1,i2,...) - Returns the maximum of i1,i2, ... as an integer (arguments are integer)
max1(x1,x2,...) - Returns the maximum of x1,x2,... as an integer (arguments are real)
Similar forms appeared for "min". In addition "log" and "log10" were optional forms in Fortran 77. Older programs often use "alog" and "alog10" to start the function name with a letter characteristic of a real rather than integer value. In general Fortran 77 was more dependent on you specifically picking a function appropriate for the argument types and type of value to be returned.
While on the subject of Fortran 90 intrinsic functions, it is worth noting two that you have seen in the example ranges.f, and one related function, that are useful in programs running on a range of machines.
tiny(x) - Returns the smallest positive number that can be represented on the current computer for real argument x
huge(x) - Returns the largest positive number that can be represented on the current computer for real argument x
precision(x) - Returns the a approximate decimal precision available on the current computer for the real argument x
The high cost of "exp" and "log" is also reflected in the use of the operator "**". Usually an expression like "x**y" results in the compiler inserting code equivalent to "exp(y*log(x))". However, most compilers are smart enough to realize that if y is an integer, they can use one or more multiplications (x**2=x*x, x**3=x*x*x, etc.). Such compilers contain the logic to know the break-even point, in terms of size of y, between such multiplication and the combination of "exp" and "log". It is always faster to program "x**2" than "x**2.0", so be careful in your choice of types for exponents.
Speed is also a factor in the existence of the "sqrt" intrinsic function. This is a special algorithm for computing the square root of a number, that is always faster than raising the number to the 0.5 power. When the option exists use "sqrt(x)" rather than "x**0.5". In my experience "sqrt(sqrt(x))" is faster than "x**0.25".
While we are on the subject of speed, we should review the relative speed of other operations. Add and subtract are always the fastest. Multiply comes second. Divide is slower than multiply, but significantly faster than sqrt. If you are going to divide by a variable x frequently (more than 2 or 3 times), it is a good idea to define another variable say rx with the equation "rx=1./x", then multiply by rx where you would have divided by x.
Test your knowledge of this material with some review questions.
Maintained by John Mahaffy : firstname.lastname@example.org