



Arithmetic Intrinsic Functions Many of the arithmetic intrinsic functions have generic names: that is they can be used with several different types of arguments. The SQRT function, for example, can be used with a real, double precision, or complex argument. The Fortran system automatically selects the correct specific function for the job: SQRT, DSQRT, or CSQRT. These specific names can be ignored in almost all circumstances, and are listed only in the appendix. In most cases the data type of the function is the same as that of its argument but there are a few obvious exceptions such as the type conversion functions.
In the descriptions below, the number and data type of the arguments of each intrinsic function are indicated by a letter: I = integer, R = real, D = double precision, X = complex.
An asterisk on the left indicates that the result has the same data type as the arguments. Note that if multiple arguments are permitted they must all have the same data type. Thus I = NINT(RD) indicates that the NINT function can take a single real or double precision argument but its result is always integer, whereas * = ANINT(RD) indicates that the result has the same type (real or double precision) as the argument.
The functions in this group can all be used on real or double precision arguments, and SIN and COS can also be used on complex numbers. In every case the result has the same data type as the argument.
* = SIN(RDX) | sine of the angle in radians. |
* = COS(RDX) | cosine of the angle in radians. |
* = TAN(RD) | tangent of the angle in radians. |
* = ASIN(RD) | arc-sine; the result is in the range - /2 to + /2. |
* = ACOS(RD) | arc-cosine; the result is in the range 0 to + . |
* = ATAN(RD) | arc-tangent; the result is in the range - /2 to + /2. |
* = ATAN2(RD,RD) | arc-tangent of arg1/arg2; the result is in the range
- to + . Both arguments must not be zero. |
* = SINH(RD) | hyperbolic sine. |
* = COSH(RD) | hyperbolic cosine. |
* = TANH(RD) | hyperbolic tangent. |
Note that the arguments of SIN, COS, and TAN must be angles measured in
radians (not degrees). They can be used on angles of any size, positive or negative,
but if the magnitude is very large the accuracy of the result will be reduced.
Similarly all the inverse trigonometric functions deliver a result in radians; the
argument of ASIN and ACOS must be in the range -1 to +1. The ATAN2 function
can be useful in resolving a result into the correct quadrant of the circle,
thus:
ATAN(0.5) = 0.4636476
ATAN2(2.0,4.0) = 0.4636476
ATAN2(-2.0,-4.0) = -2.677945 ( = 0.4636476 -
).
Other Transcendental Functions
* = SQRT(RDX) | square root. |
* = LOG(RDX) | natural logarithm, i.e. log to base e (where e = 2.718281828...). |
* = EXP(RDX) | returns the exponential, i.e. e to the power of the argument. This is the inverse of the natural logarithm. |
* = LOG10(RD) | logarithm to base 10. |
Note that LOG10, which may be useful to compute decibel ratios etc., is the only one of this group which cannot be used on a complex argument.
These functions can be used to convert from any of the four arithmetic data types to any of the others. They are used automatically whenever mixed data types are encountered in arithmetic expressions and assignments.
I = INT(IRDX) | converts to integer by truncation. |
R = REAL(IRDX) | converts to real. |
D = DBLE(IRDX) | converts to double precision. |
X = CMPLX(IRDX) | converts to complex. |
X = CMPLX(IRD,IRD) | converts to complex. |
The integer conversion of INT rounds towards zero; if you need to round to the nearest integer use the NINT function (described below). The CMPLX function produces a value with a zero imaginary component unless it is used with two arguments (or one which is already complex). It is important to realise that many conversions lose information: in particular a double precision value is likely to lose significant digits if converted to any other data type.
The MIN and MAX functions are unique in being able to take any number of arguments from two upwards; the result has the same data type as the arguments.
* = MIN(IRD,IRD,...) | returns the smallest of its arguments. |
* = MAX(IRD,IRD,...) | returns the largest of its arguments. |
These two functions can, of course, be combined to limit a value to a certain
range. For example, to limit a value TEMPER to the range 32 to 212 you can use an
expression such as:
MAX(32.0, MIN(TEMPER, 212.0))
Note that the minimum of the range is an argument of the MAX function and
vice-versa.
To find the largest (or smallest) element of a large array it is necessary use a loop.
*Find largest value in array T of N elements: TOP = T(1) DO 25,I = 2,N TOP = MAX(T(I), TOP) 25 CONTINUE *TOP now contains the largest element of T. |
* = AINT(RD) | Truncates the fractional part (i.e. as INT) but preserves the data type. |
* = ANINT(RD) | Rounds to the nearest whole number. |
I = NINT(RD) | Converts to integer by rounding to the nearest whole number. |
* = ABS(IRD) | Returns the absolute value of a number (i.e. it changes the sign if negative). |
R = ABS(X) | Computes the modulus of a complex number (i.e. the square-root of the sum of the squares of the two components). |
* = MOD(IRD,IRD) | returns A1 modulo A2, i.e. the remainder after dividing A1 by A2. |
* = SIGN(IRD,IRD) | performs sign transfer: if A2 is negative the result is -A1, if A2 is zero or positive the result is A1. |
* = DIM(IRD,IRD) | returns the positive difference of A1 and A2, i.e. if
A1 > A2 it returns (A1-A2), otherwise zero. |
D = DPROD(R,R) | Computes the double precision product of two real values. |
R = AIMAG(X) | Extracts the imaginary component of a complex number. Note that the real component can be obtained by using the REAL function. |
X = CONJG(X) | Computes the complex conjugate of a complex number. |
The NINT and ANINT functions round upwards if the fractional part of the
argument is 0.5 or more, whereas INT and AINT always round towards zero.
Thus:
INT(+3.5) = 3 NINT(+3.5) = 4
INT(-3.5) = -3 NINT(-3.5) = -4
The fractional part of a floating point number, X, can easily be found either
by:
X - AINT(X)
or
MOD(X, 1.0)
In either case, if X is negative the result will also be negative. The ABS function can
always be used to alter the sign if required.
The MOD function has other uses. For example it can find the day of the week
from an absolute day count such as Modified Julian Date (MJD):
MOD(MJD,7)
has a value between 0 and 6 for days from Wednesday to Tuesday. Similarly if you
use the ATAN2 function but want the result to lie in the range 0 to 2
(rather than
-
to +2
) then, assuming the value of TWOPI is suitably defined, the required
expression is:
MOD(ATAN2(X,Y) + TWOPI, TWOPI)