Wednesday, February 1, 2017

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

Function Description
ABS (A) It returns the absolute value of A
AIMAG (Z) It returns the imaginary part of a complex number Z
AINT (A [, KIND]) It truncates fractional part of A towards zero, returning a real, whole number.
ANINT (A [, KIND]) It returns a real value, the nearest integer or whole number.
CEILING (A [, KIND]) It returns the least integer greater than or equal to number A.
CMPLX (X [, Y, KIND]) It converts the real variables X and Y to a complex number X+iY; if Y is absent, 0 is used.
CONJG (Z) It returns the complex conjugate of any complex number Z.
DBLE (A) It converts A to a double precision real number.
DIM (X, Y) It returns the positive difference of X and Y.
DPROD (X, Y) It returns the double precision real product of X and Y.
FLOOR (A [, KIND]) It provides the greatest integer less than or equal to number A.
INT (A [, KIND]) It converts a number (real or integer) to integer, truncating the real part towards zero.
MAX (A1, A2 [, A3,...]) It returns the maximum value from the arguments, all being of same type.
MIN (A1, A2 [, A3,...]) It returns the minimum value from the arguments, all being of same type.
MOD (A, P) It returns the remainder of A on division by P, both arguments being of the same type (A-INT(A/P)*P)
MODULO (A, P) It returns A modulo P: (A-FLOOR(A/P)*P)
NINT (A [, KIND]) It returns the nearest integer of number A
REAL (A [, KIND]) It Converts to real type
SIGN (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

Function Description
ACOS (X) It returns the inverse cosine in the range (0, π), in radians.
ASIN (X) It returns the inverse sine in the range (-π/2, π/2), in radians.
ATAN (X) It returns the inverse tangent in the range (-π/2, π/2), in radians.
ATAN2 (Y, X) It returns the inverse tangent in the range (-π, π), in radians.
COS (X) It returns the cosine of argument in radians.
COSH (X) It returns the hyperbolic cosine of argument in radians.
EXP (X) It returns the exponential value of X.
LOG (X) It returns the natural logarithmic value of X.
LOG10 (X) It returns the common logarithmic (base 10) value of X.
SIN (X) It returns the sine of argument in radians.
SINH (X) It returns the hyperbolic sine of argument in radians.
SQRT (X) It returns square root of X.
TAN (X) It returns the tangent of argument in radians.
TANH (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.
Function Description
DIGITS (X) It returns the number of significant digits of the model.
EPSILON (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)).
HUGE (X) It returns the largest number of the model
MAXEXPONENT (X) It returns the maximum exponent of the model
MINEXPONENT (X) It returns the minimum exponent of the model
PRECISION (X) It returns the decimal precision
RADIX (X) It returns the base of the model
RANGE (X) It returns the decimal exponent range
TINY (X) It returns the smallest positive number of the model

Floating-Point Manipulation Functions

Function Description
EXPONENT (X) It returns the exponent part of a model number
FRACTION (X) It returns the fractional part of a number
NEAREST (X, S) It returns the nearest different processor number in given direction
RRSPACING (X) It returns the reciprocal of the relative spacing of model numbers near given number
SCALE (X, I) It multiplies a real by its base to an integer power
SET_EXPONENT (X, I) it returns the exponent part of a number
SPACING (X) It returns the absolute spacing of model numbers near given number

Bit Manipulation Functions

Function Description
BIT_SIZE (I) It returns the number of bits of the model
BTEST (I, POS) Bit testing
IAND (I, J) Logical AND
IBCLR (I, POS) Clear bit
IBITS (I, POS, LEN) Bit extraction
IBSET (I, POS) Set bit
IEOR (I, J) Exclusive OR
IOR (I, J) Inclusive OR
ISHFT (I, SHIFT) Logical shift
ISHFTC (I, SHIFT [, SIZE]) Circular shift
NOT (I) Logical complement

Character Functions

Function Description
ACHAR (I) It returns the Ith character in the ASCII collating sequence.
ADJUSTL (STRING) It adjusts string left by removing any leading blanks and inserting trailing blanks
ADJUSTR (STRING) It adjusts string right by removing trailing blanks and inserting leading blanks.
CHAR (I [, KIND]) It returns the Ith character in the machine specific collating sequence
IACHAR (C) It returns the position of the character in the ASCII collating sequence.
ICHAR (C) It returns the position of the character in the machine (processor) specific collating sequence.
INDEX (STRING, SUBSTRING [, BACK]) It returns the leftmost (rightmost if BACK is .TRUE.) starting position of SUBSTRING within STRING.
LEN (STRING) It returns the length of a string.
LEN_TRIM (STRING) It returns the length of a string without trailing blank characters.
LGE (STRING_A, STRING_B) Lexically greater than or equal
LGT (STRING_A, STRING_B) Lexically greater than
LLE (STRING_A, STRING_B) Lexically less than or equal
LLT (STRING_A, STRING_B) Lexically less than
REPEAT (STRING, NCOPIES) Repeated concatenation
SCAN (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.
TRIM (STRING) Removes trailing blank characters
VERIFY (STRING, SET [, BACK]) Verifies the set of characters in a string

Kind Functions

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

Logical Function

Function Description
LOGICAL (L [, KIND]) Convert between objects of type logical with different kind type parameters

No comments:

Post a Comment