Fortran – Intrinsic Functions

Fortran intrinsic functions

In this guide, we will discuss Fortran Intrinsic Functions. Intrinsic functions are some common and important functions that are provided as a part of the Fortran language. We have already discussed some of these functions in the Arrays, Characters and String chapters.

Intrinsic functions can be categorised as −

  • Numeric Functions
  • Mathematical Functions
  • Numeric Inquiry Functions
  • Floating-Point Manipulation Functions
  • Bit Manipulation Functions
  • Character Functions
  • Kind Functions
  • Logical Functions
  • Array Functions.

We have discussed the array functions in the Arrays chapter. In the following section we provide brief descriptions of all these functions from other categories.

In the function name column,

  • A represents any type of numeric variable
  • R represents a real or integer variable
  • X and Y represent real variables
  • Z represents complex variable
  • W represents real or complex variable

Numeric Functions

Sr.NoFunction & Description
1ABS (A)
It returns the absolute value of A
2AIMAG (Z)
It returns the imaginary part of a complex number Z
3AINT (A [, KIND])
It truncates fractional part of A towards zero, returning a real, whole number.
4ANINT (A [, KIND])
It returns a real value, the nearest integer or whole number.
5CEILING (A [, KIND])
It returns the least integer greater than or equal to number A.
6CMPLX (X [, Y, KIND])
It converts the real variables X and Y to a complex number X+iY; if Y is absent, 0 is used.
7CONJG (Z)
It returns the complex conjugate of any complex number Z.
8DBLE (A)
It converts A to a double precision real number.
9DIM (X, Y)
It returns the positive difference of X and Y.
10DPROD (X, Y)
It returns the double precision real product of X and Y.
11FLOOR (A [, KIND])
It provides the greatest integer less than or equal to number A.
12INT (A [, KIND])
It converts a number (real or integer) to integer, truncating the real part towards zero.
13MAX (A1, A2 [, A3,…])
It returns the maximum value from the arguments, all being of same type.
14MIN (A1, A2 [, A3,…])
It returns the minimum value from the arguments, all being of same type.
15MOD (A, P)
It returns the remainder of A on division by P, both arguments being of the same type (A-INT(A/P)*P)
16MODULO (A, P)
It returns A modulo P: (A-FLOOR(A/P)*P)
17NINT (A [, KIND])
It returns the nearest integer of number A
18REAL (A [, KIND])
It Converts to real type
19SIGN (A, B)
It returns the absolute value of A multiplied by the sign of P. Basically it transfers the of sign of B to A.

Example

program numericFunctions
implicit none  

   ! define constants  
   ! define variables
   real :: a, b 
   complex :: z
   
   ! values for a, b 
   a = 15.2345
   b = -20.7689
    
   write(*,*) 'abs(a): ',abs(a),' abs(b): ',abs(b)   
   write(*,*) 'aint(a): ',aint(a),' aint(b): ',aint(b) 
   write(*,*) 'ceiling(a): ',ceiling(a),' ceiling(b): ',ceiling(b)   
   write(*,*) 'floor(a): ',floor(a),' floor(b): ',floor(b)  
    
   z = cmplx(a, b)
   write(*,*) 'z: ',z   
   
end program numericFunctions

When you compile and execute the above program, it produces the following result −

abs(a): 15.2344999   abs(b): 20.7688999    
aint(a): 15.0000000  aint(b): -20.0000000    
ceiling(a): 16  ceiling(b): -20
floor(a): 15  floor(b): -21
z: (15.2344999, -20.7688999)

Mathematical Functions

Sr.NoFunction & Description
1ACOS (X)
It returns the inverse cosine in the range (0, π), in radians.
2ASIN (X)
It returns the inverse sine in the range (-π/2, π/2), in radians.
3ATAN (X)
It returns the inverse tangent in the range (-π/2, π/2), in radians.
4ATAN2 (Y, X)
It returns the inverse tangent in the range (-π, π), in radians.
5COS (X)
It returns the cosine of argument in radians.
6COSH (X)
It returns the hyperbolic cosine of argument in radians.
7EXP (X)
It returns the exponential value of X.
8LOG (X)It returns the natural logarithmic value of X.
9LOG10 (X)
It returns the common logarithmic (base 10) value of X.
10SIN (X)
It returns the sine of argument in radians.
11SINH (X)
It returns the hyperbolic sine of argument in radians.
12SQRT (X)
It returns square root of X.
13TAN (X)
It returns the tangent of argument in radians.
14TANH (X)
It returns the hyperbolic tangent of argument in radians.

Example

The following program computes the horizontal and vertical position x and y respectively of a projectile after a time, t −

Where, x = u t cos a and y = u t sin a – g t2 / 2

program projectileMotion  
implicit none  

   ! define constants  
   real, parameter :: g = 9.8  
   real, parameter :: pi = 3.1415927  
   
   !define variables
   real :: a, t, u, x, y   
   
   !values for a, t, and u 
   a = 45.0
   t = 20.0
   u = 10.0
   
   ! convert angle to radians  
   a = a * pi / 180.0  
   x = u * cos(a) * t   
   y = u * sin(a) * t - 0.5 * g * t * t  
   
   write(*,*) 'x: ',x,'  y: ',y   
   
end program projectileMotion

When you compile and execute the above program, it produces the following result −

x: 141.421356  y: -1818.57861  

Numeric Inquiry Functions

These functions work with a certain model of integer and floating-point arithmetic. The functions return properties of numbers of the same kind as the variable X, which can be real and in some cases integer.

Sr.NoFunction & Description
1DIGITS (X)
It returns the number of significant digits of the model.
2EPSILON (X)
It returns the number that is almost negligible compared to one. In other words, it returns the smallest value such that REAL( 1.0, KIND(X)) + EPSILON(X) is not equal to REAL( 1.0, KIND(X)).
3HUGE (X)
It returns the largest number of the model
4MAXEXPONENT (X)
It returns the maximum exponent of the model
5MINEXPONENT (X)
It returns the minimum exponent of the model
6PRECISION (X)
It returns the decimal precision
7RADIX (X)
It returns the base of the model
8RANGE (X)
It returns the decimal exponent range
9TINY (X)
It returns the smallest positive number of the model

Floating-Point Manipulation Functions

Sr.NoFunction & Description
1EXPONENT (X)
It returns the exponent part of a model number
2FRACTION (X)
It returns the fractional part of a number
3NEAREST (X, S)
It returns the nearest different processor number in given direction
4RRSPACING (X)
It returns the reciprocal of the relative spacing of model numbers near given number
5SCALE (X, I)
It multiplies a real by its base to an integer power
6SET_EXPONENT (X, I)
It returns the exponent part of a number
7SPACING (X)
It returns the absolute spacing of model numbers near given number

Bit Manipulation Functions

Sr.NoFunction & Description
1BIT_SIZE (I)
It returns the number of bits of the model
2BTEST (I, POS)
Bit testing
3IAND (I, J)
Logical AND
4IBCLR (I, POS)
Clear bit
5IBITS (I, POS, LEN)
Bit extraction
6IBSET (I, POS)
Set bit
7IEOR (I, J)
Exclusive OR
8IOR (I, J)
Inclusive OR
9ISHFT (I, SHIFT)
Logical shift
10ISHFTC (I, SHIFT [, SIZE])
Circular shift
11NOT (I)
Logical complement

Character Functions

Sr.NoFunction & Description
1ACHAR (I)
It returns the Ith character in the ASCII collating sequence.
2ADJUSTL (STRING)
It adjusts string left by removing any leading blanks and inserting trailing blanks
3ADJUSTR (STRING)
It adjusts string right by removing trailing blanks and inserting leading blanks.
4CHAR (I [, KIND])
It returns the Ith character in the machine specific collating sequence
5IACHAR (C)
It returns the position of the character in the ASCII collating sequence.
6ICHAR (C)
It returns the position of the character in the machine (processor) specific collating sequence.
7INDEX (STRING, SUBSTRING [, BACK])
It returns the leftmost (rightmost if BACK is .TRUE.) starting position of SUBSTRING within STRING.
8LEN (STRING)
It returns the length of a string.
9LEN_TRIM (STRING)
It returns the length of a string without trailing blank characters.
10LGE (STRING_A, STRING_B)
Lexically greater than or equal
11LGT (STRING_A, STRING_B)
Lexically greater than
12LLE (STRING_A, STRING_B)
Lexically less than or equal
13LLT (STRING_A, STRING_B)
Lexically less than
14REPEAT (STRING, NCOPIES)
Repeated concatenation
15SCAN (STRING, SET [, BACK])
It returns the index of the leftmost (rightmost if BACK is .TRUE.) character of STRING that belong to SET, or 0 if none belong.
16TRIM (STRING)
Removes trailing blank characters
17VERIFY (STRING, SET [, BACK])
Verifies the set of characters in a string

Kind Functions

Sr.NoFunction & Description
1KIND (X)
It returns the kind type parameter value.
2SELECTED_INT_KIND (R)
It returns kind of type parameter for specified exponent range.
3SELECTED_REAL_KIND ([P, R])
Real kind type parameter value, given precision and range

Logical Function

Sr.NoFunction & Description
1LOGICAL (L [, KIND])
Convert between objects of type logical with different kind type parameters

Next Topic : Click Here

This Post Has One Comment

Leave a Reply