WANtaroHP (Intrinsic functions in Fortran 90)

toJA

Outline of this page



1. Numerical functions

ABS : Absolute value

Description:

ABS(A) computes the absolute value of A.

Class:

Elemental function

Syntax:

RESULT = ABS(A)

Arguments:

A : The type of the argument shall be an INTEGER, REAL, or COMPLEX.

Return value:

The return value is of the same type and kind as the argument except the return value is REAL for a COMPLEX argument.

Example:

program test_abs
integer :: i = -1
real :: x = -1.e0
complex :: z = (-1.e0,0.e0)
i = abs(i)
x = abs(x)
x = abs(z)
end program test_abs


AIMAG : Imaginary part of complex number

Description:

AIMAG(Z) yields the imaginary part of complex argument Z.

Class:

Elemental function

Syntax:

RESULT = AIMAG(Z)

Arguments:

Z : The type of the argument shall be COMPLEX.

Return value:

The return value is of type REAL with the kind type parameter of the argument.

Example:

program test_aimag
complex(4) z4
complex(8) z8
z4 = cmplx(1.e0_4, 0.e0_4)
z8 = cmplx(0.e0_8, 1.e0_8)
print *, aimag(z4), dimag(z8)
end program test_aimag


AINT : Truncate to a whole number

Description:

AINT(A [, KIND]) truncates its argument to a whole number.

Class:

Elemental function

Syntax:

RESULT = AINT(A [, KIND])

Arguments:

Return value:

Example:

program test_aint
real(4) x4
real(8) x8
x4 = 1.234E0_4
x8 = 4.321_8
print *, aint(x4), dint(x8)
x8 = aint(x4,8)
end program test_aint


ANINT : Nearest whole number

Description:

ANINT(A [, KIND]) rounds its argument to the nearest whole number.

Class:

Elemental function

Syntax:

RESULT = ANINT(A [, KIND])

Arguments:

Return value:

Example:

program test_anint
real(4) x4
real(8) x8
x4 = 1.234E0_4
x8 = 4.321_8
Chapter 8: Intrinsic Procedures 73
print *, anint(x4), dnint(x8)
x8 = anint(x4,8)
end program test_anint


CEILING : Integer ceiling function

Description:

CEILING(A) returns the least integer greater than or equal to A.

Class:

Elemental function

Syntax:

RESULT = CEILING(A [, KIND])

Arguments:

Return value:

The return value is of type INTEGER(KIND) if KIND is present and a defaultkind INTEGER otherwise.

Example:

program test_ceiling
real :: x = 63.29
real :: y = -63.59
print *, ceiling(x) ! returns 64
print *, ceiling(y) ! returns -63
end program test_ceiling


CMPLX : Complex conversion function

Description:

Class:

Elemental function

Syntax:

RESULT = CMPLX(X [, Y [, KIND]])

Arguments:

Return value:

Example:

program test_cmplx
integer :: i = 42
real :: x = 3.14
complex :: z
z = cmplx(i, x)
print *, z, cmplx(x)
end program test_cmplx


CONJG : Complex conjugate function

Description:

Class:

Elemental function

Syntax:

Z = CONJG(Z)

Arguments:

Z : The type shall be COMPLEX.

Return value:

The return value is of type COMPLEX.

Example:

program test_conjg
complex :: z = (2.0, 3.0)
complex(8) :: dz = (2.71_8, -3.14_8)
z= conjg(z)
print *, z
dz = dconjg(dz)
print *, dz
end program test_conjg


DBLE : Double conversion function

Description:

DBLE(A) Converts A to double precision real type.

Class:

Elemental function

Syntax:

RESULT = DBLE(A)

Arguments:

A : The type shall be INTEGER, REAL, or COMPLEX.

Return value:

The return value is of type double precision real.

Example:

program test_dble
real :: x = 2.18
integer :: i = 5
complex :: z = (2.3,1.14)
print *, dble(x), dble(i), dble(z)
end program test_dble


DIM : Positive difference

Description:

DIM(X,Y) returns the difference X-Y if the result is positive; otherwise returns zero.

Class:

Elemental function

Syntax:

RESULT = DIM(X, Y)

Arguments:

Return value:

The return value is of type INTEGER or REAL.

Example:

program test_dim
integer :: i
real(8) :: x
i = dim(4, 15)
x = dim(4.345_8, 2.111_8)
print *, i
print *, x
end program test_dim


DPROD : Double product function

Description:

DPROD(X,Y) returns the product X*Y.

Class:

Elemental function

Syntax:

RESULT = DPROD(X, Y)

Arguments:

Return value:

The return value is of type REAL(8).

Example:

program test_dprod
real :: x = 5.2
real :: y = 2.3
real(8) :: d
d = dprod(x,y)
print *, d
end program test_dprod


FLOOR : Integer floor function

Description:

FLOOR(A) returns the greatest integer less than or equal to X.

Class:

Elemental function

Syntax:

RESULT = FLOOR(A [, KIND])

Arguments:

Return value:

The return value is of type INTEGER(KIND) if KIND is present and of defaultkind INTEGER otherwise.

Example:

program test_floor
real :: x = 63.29
real :: y = -63.59
print *, floor(x) ! returns 63
print *, floor(y) ! returns -64
end program test_floor


INT : Convert to integer type

Description:

Convert to integer type

Class:

Elemental function

Syntax:

RESULT = INT(A [, KIND))

Arguments:

Return value:

These functions return a INTEGER variable or array under the following rules:

Example:

program test_int
integer :: i = 42
complex :: z = (-3.7, 1.0)
print *, int(i)
print *, int(z), int(z,8)
end program


MAX : Maximum value of an argument list

Description:

Returns the argument with the largest (most positive) value.

Class:

Elemental function

Syntax:

RESULT = MAX(A1, A2 [, A3 [, ...]])

Arguments:

Return value:

The return value corresponds to the maximum value among the arguments, and has the same type and kind as the first argument.



MIN : Minimum value of an argument list

Description:

Returns the argument with the smallest (most negative) value.

Class:

Elemental function

Syntax:

RESULT = MIN(A1, A2 [, A3, ...])

Arguments:

Return value:

The return value corresponds to the maximum value among the arguments, and has the same type and kind as the first argument.



MOD : Remainder function

Description:

Class:

Elemental function

Syntax:

RESULT = MOD(A, P)

Arguments:

Return value:

The kind of the return value is the result of cross-promoting the kinds of the arguments.

Example:

program test_mod
print *, mod(17,3)
print *, mod(17.5,5.5)
print *, mod(17.5d0,5.5)
print *, mod(17.5,5.5d0)
print *, mod(-17,3)
print *, mod(-17.5,5.5)
print *, mod(-17.5d0,5.5)
print *, mod(-17.5,5.5d0)
print *, mod(17,-3)
print *, mod(17.5,-5.5)
print *, mod(17.5d0,-5.5)
print *, mod(17.5,-5.5d0)
end program test_mod


NINT : Nearest whole number

Description:

NINT(A) rounds its argument to the nearest whole number.

Class:

Elemental function

Syntax:

RESULT = NINT(A [, KIND])

Arguments:

Return value:

Returns A with the fractional portion of its magnitude eliminated by rounding to the nearest whole number and with its sign preserved, converted to an INTEGER of the default kind.

Example:

program test_nint
real(4) x4
real(8) x8
x4 = 1.234E0_4
x8 = 4.321_8
print *, nint(x4), idnint(x8)
end program test_nint


REAL : Convert to real type

Description:

REAL(A [, KIND]) converts its argument A to a real type.

Class:

Elemental function

Syntax:

RESULT = REAL(A [, KIND])
RESULT = REALPART(Z)

Arguments:

Return value:

These functions return a REAL variable or array under the following rules:

Example:

program test_real
complex :: x = (1.0, 2.0)
print *, real(x), real(x,8), realpart(x)
end program test_real


SIGN : Sign copying function

Description:

SIGN(A,B) returns the value of A with the sign of B.

Class:

Elemental function

Syntax:

RESULT = SIGN(A, B)

Arguments:

Return value:

Example:

program test_sign
print *, sign(-12,1)
print *, sign(-12,0)
print *, sign(-12,-1)
print *, sign(-12.,1.)
print *, sign(-12.,0.)
print *, sign(-12.,-1.)
end program test_sign


2. Mathematical functions

ACOS : Arccosine function

Description:

ACOS(X) computes the arccosine of X (inverse of COS(X)).

Class:

Elemental function

Syntax:

RESULT = ACOS(X)

Arguments:

X : The type shall either be REAL with a magnitude that is less than or equal to one - or the type shall be COMPLEX.

Return value:

Example:

program test_acos
real(8) :: x = 0.866_8
x = acos(x)
end program test_acos


ASIN : Arcsine function

Description:

ASIN(X) computes the arcsine of its X (inverse of SIN(X)).

Class:

Elemental function

Syntax:

RESULT = ASIN(X)

Arguments:

X : The type shall be either REAL and a magnitude that is less than or equal to one - or be COMPLEX.

Return value:

Example:

program test_asin
real(8) :: x = 0.866_8
x = asin(x)
end program test_asin


ATAN : Arctangent function

Description:

ATAN(X) computes the arctangent of X.

Class:

Elemental function

Syntax:

RESULT = ATAN(X)
RESULT = ATAN(Y, X)

Arguments:

Return value:

Example:

program test_atan
real(8) :: x = 2.866_8
x = atan(x)
end program test_atan


ATAN2 : Arctangent function

Description:

Class:

Elemental function

Syntax:

RESULT = ATAN2(Y, X)

Arguments:

Return value:

Example:

program test_atan2
real(4) :: x = 1.e0_4, y = 0.5e0_4
x = atan2(y,x)
end program test_atan2


COS : Cosine function

Description:

COS(X) computes the cosine of X.

Class:

Elemental function

Syntax:

RESULT = COS(X)

Arguments:

X : The type shall be REAL or COMPLEX.

Return value:

Example:

program test_cos
real :: x = 0.0
x = cos(x)
end program test_cos


COSH : Hyperbolic cosine function

Description:

COSH(X) computes the hyperbolic cosine of X.

Class:

Elemental function

Syntax:

X = COSH(X)

Arguments:

X : The type shall be REAL or COMPLEX.

Return value:

Example:

program test_cosh
real(8) :: x = 1.0_8
x = cosh(x)
end program test_cosh


EXP : Exponential function

Description:

EXP(X) computes the base e exponential of X.

Class:

Elemental function

Syntax:

RESULT = EXP(X)

Arguments:

X : The type shall be REAL or COMPLEX.

Return value:

The return value has same type and kind as X.

Example:

program test_exp
real :: x = 1.0
x = exp(x)
end program test_exp


LOG : Natural logarithm function

Description:

LOG(X) computes the natural logarithm of X, i.e. the logarithm to the base e.

Class:

Elemental function

Syntax:

RESULT = LOG(X)

Arguments:

X : The type shall be REAL or COMPLEX.

Return value:

Example:

program test_log
real(8) :: x = 2.7182818284590451_8
complex :: z = (1.0, 2.0)
x = log(x) ! will yield (approximately) 1
z = log(z)
end program test_log


LOG10 : Base 10 logarithm function

Description:

LOG10(X) computes the base 10 logarithm of X.

Class:

Elemental function

Syntax:

RESULT = LOG10(X)

Arguments:

X : The type shall be REAL.

Return value:

Example:

program test_log10
real(8) :: x = 10.0_8
x = log10(x)
end program test_log10


SIN : Sine function

Description:

SIN(X) computes the sine of X.

Class:

Elemental function

Syntax:

RESULT = SIN(X)

Arguments:

X : The type shall be REAL or COMPLEX.

Return value:

The return value has same type and kind as X.

Example:

program test_sin
real :: x = 0.0
x = sin(x)
end program test_sin


SINH : Hyperbolic sine function

Description:

SINH(X) computes the hyperbolic sine of X.

Class:

Elemental function

Syntax:

RESULT = SINH(X)

Arguments:

X : The type shall be REAL or COMPLEX.

Return value:

The return value has same type and kind as X.

Example:

program test_sinh
real(8) :: x = - 1.0_8
x = sinh(x)
end program test_sinh


SQRT : Square-root function

Description:

SQRT(X) computes the square root of X.

Class:

Elemental function

Syntax:

RESULT = SQRT(X)

Arguments:

X : The type shall be REAL or COMPLEX.

Return value:

Example:

program test_sqrt
real(8) :: x = 2.0_8
complex :: z = (1.0, 2.0)
x = sqrt(x)
z = sqrt(z)
end program test_sqrt


TAN : Tangent function

Description:

TAN(X) computes the tangent of X.

Class:

Elemental function

Syntax:

RESULT = TAN(X)

Arguments:

X : The type shall be REAL or COMPLEX.

Return value:

The return value has same type and kind as X.

Example:

program test_tan
real(8) :: x = 0.165_8
x = tan(x)
end program test_tan


TANH : Hyperbolic tangent function

Description:

TANH(X) computes the hyperbolic tangent of X.

Class:

Elemental function

Syntax:

X = TANH(X)

Arguments:

X : The type shall be REAL or COMPLEX.

Return value:

Example:

program test_tanh
real(8) :: x = 2.1_8
x = tanh(x)
end program test_tanh


3. Character string functions

ACHAR : Character in ASCII collating sequence

Description:

ACHAR(I) returns the character located at position I in the ASCII collating sequence.

Class:

Elemental function

Syntax:

RESULT = ACHAR(I [, KIND])

Arguments:

Return value:

Example:

program test_achar
character c
c = achar(32)
end program test_achar


ADJUSTL : Left adjust a string

Description:

Class:

Elemental function

Syntax:

RESULT = ADJUSTL(STRING)

Arguments:

STRING : The type shall be CHARACTER.

Return value:

The return value is of type CHARACTER and of the same kind as STRING where leading spaces are removed and the same number of spaces are inserted on the end of STRING.

Example:

program test_adjustl
character(len=20) :: str = ' gfortran'
str = adjustl(str)
print *, str
end program test_adjustl


ADJUSTR : Right adjust a string

Description:

Class:

Elemental function

Syntax:

RESULT = ADJUSTR(STRING)

Arguments:

STRING : The type shall be CHARACTER.

Return value:

The return value is of type CHARACTER and of the same kind as STRING where trailing spaces are removed and the same number of spaces are inserted at the start of STRING.

Example:

program test_adjustr
character(len=20) :: str = 'gfortran'
str = adjustr(str)
print *, str
end program test_adjustr


IACHAR : Code in ASCII collating sequence

Description:

IACHAR(C) returns the code for the ASCII character in the first character position of C.

Class:

Elemental function

Syntax:

RESULT = IACHAR(C [, KIND])

Arguments:

Return value:

Example:

program test_iachar
integer i
i = iachar(' ')
end program test_iachar


ICHAR : Character-to-integer conversion function

Description:

Class:

Elemental function

Syntax:

RESULT = ICHAR(C [, KIND])

Arguments:

Return value:

Example:

program test_ichar
integer i
i = ichar(' ')
end program test_ichar

Note: No intrinsic exists to convert between a numeric value and a formatted character string representation -- for instance, given the CHARACTER value '154', obtaining an INTEGER or REAL value with the value 154, or vice versa. Instead, this functionality is provided by internal-file I/O, as in the following Example:

program read_val
integer value
character(len=10) string, string2
string = '154'
Chapter 8: Intrinsic Procedures 137
! Convert a string to a numeric value
read (string,'(I10)') value
print *, value
! Convert a value to a formatted string
write (string2,'(I10)') value
print *, string2
end program read_val


CHAR : Character conversion function

Description:

CHAR(I [, KIND]) returns the character represented by the integer I.

Class:

Elemental function

Syntax:

RESULT = CHAR(I [, KIND])

Arguments:

Return value:

The return value is of type CHARACTER(1)

Example:

program test_char
integer :: i = 74
character(1) :: c
c = char(i)
print *, i, c ! returns 'J'
end program test_char


INDEX : Position of a substring within a string

Description:

Class:

Elemental function

Syntax:

RESULT = INDEX(STRING, SUBSTRING [, BACK [, KIND]])

Arguments:

Return value:



LEN : Length of a character entity

Description:

Class:

Inquiry function

Syntax:

L = LEN(STRING [, KIND])

Arguments:

Return value:



LEN_TRIM : Length of a character entity without trailing blank characters

Description:

Returns the length of a character string, ignoring any trailing blanks.

Class:

Elemental function

Syntax:

RESULT = LEN_TRIM(STRING [, KIND])

Arguments:

Return value:



REPEAT : Repeated string concatenation

Description:

Concatenates NCOPIES copies of a string.

Class:

Transformational function

Syntax:

RESULT = REPEAT(STRING, NCOPIES)

Arguments:

Return value:

A new scalar of type CHARACTER built up from NCOPIES copies of STRING.

Example:

program test_repeat
write(*,*) repeat("x", 5) ! "xxxxx"
end program


SCAN : Scan a string for the presence of a set of characters

Description:

Class:

Elemental function

Syntax:

RESULT = SCAN(STRING, SET[, BACK [, KIND]])

Arguments:

Return value:

Example:

PROGRAM test_scan
WRITE(*,*) SCAN("FORTRAN", "AO") ! 2, found 'O'
WRITE(*,*) SCAN("FORTRAN", "AO", .TRUE.) ! 6, found 'A'
WRITE(*,*) SCAN("FORTRAN", "C++") ! 0, found none
END PROGRAM


TRIM : Remove trailing blank characters of a string

Description:

Removes trailing blank characters of a string.

Class:

Transformational function

Syntax:

RESULT = TRIM(STRING)

Arguments:

STRING : Shall be a scalar of type CHARACTER.

Return value:

A scalar of type CHARACTER which length is that of STRING less the number of trailing blanks.

Example:

PROGRAM test_trim
CHARACTER(len=10), PARAMETER :: s = "GFORTRAN "
WRITE(*,*) LEN(s), LEN(TRIM(s)) ! "10 8", with/without trailing blanks
END PROGRAM


VERIFY : Scan a string for characters not a given set

Description:

Class:

Elemental function

Syntax:

RESULT = VERIFY(STRING, SET[, BACK [, KIND]])

Arguments:

Return value:

Example:

PROGRAM test_verify
WRITE(*,*) VERIFY("FORTRAN", "AO") ! 1, found 'F'
WRITE(*,*) VERIFY("FORTRAN", "FOO") ! 3, found 'R'
WRITE(*,*) VERIFY("FORTRAN", "C++") ! 1, found 'F'
WRITE(*,*) VERIFY("FORTRAN", "C++", .TRUE.) ! 7, found 'N'
WRITE(*,*) VERIFY("FORTRAN", "FORTRAN") ! 0' found none
END PROGRAM


4. Intrinsic subroutines

DATE_AND_TIME : Date and time subroutine

Description:

Class:

Subroutine

Syntax:

CALL DATE_AND_TIME([DATE, TIME, ZONE, VALUES])

Arguments:



SYSTEM_CLOCK : Time function

Description:

Class:

Subroutine

Syntax:

CALL SYSTEM_CLOCK([COUNT, COUNT_RATE, COUNT_MAX])

Arguments:

Example:

PROGRAM test_system_clock
INTEGER :: count, count_rate, count_max
CALL SYSTEM_CLOCK(count, count_rate, count_max)
WRITE(*,*) count, count_rate, count_max
END PROGRAM


RANDOM_NUMBER : Pseudo-random number

Description:

Class:

Subroutine

Syntax:

RANDOM_NUMBER(HARVEST)

Arguments:

HARVEST : Shall be a scalar or an array of type REAL.

Example:

program test_random_number
REAL :: r(5,5)
CALL init_random_seed() ! see example of RANDOM_SEED
CALL RANDOM_NUMBER(r)
end program


RANDOM_SEED : Initialize a pseudo-random number sequence

Description:

Class:

Subroutine

Syntax:

CALL RANDOM_SEED([SIZE, PUT, GET])

Arguments:

Example:

SUBROUTINE init_random_seed()
INTEGER :: i, n, clock
INTEGER, DIMENSION(:), ALLOCATABLE :: seed
CALL RANDOM_SEED(size = n)
ALLOCATE(seed(n))
CALL SYSTEM_CLOCK(COUNT=clock)
seed = clock + 37 * (/ (i - 1, i = 1, n) /)
CALL RANDOM_SEED(PUT = seed)
DEALLOCATE(seed)
END SUBROUTINE


5. Function and subroutines by GNU extension

IARGC : Get the number of command line arguments

Description:

Class:

Function

Syntax:

RESULT = IARGC()

Arguments:

None.

Return value:

The number of command line arguments, type INTEGER(4).



GETARG : Get command line arguments

Description:

Class:

Subroutine

Syntax:

CALL GETARG(POS, VALUE)

Arguments:

Return value:

Example:

PROGRAM test_getarg
INTEGER :: i
CHARACTER(len=32) :: arg
DO i = 1, iargc()
CALL getarg(i, arg)
WRITE (*,*) arg
END DO
END PROGRAM


SYSTEM : Execute a shell command

Description:

Class:

Subroutine, function

Syntax:

CALL SYSTEM(COMMAND [, STATUS])
STATUS = SYSTEM(COMMAND)

Arguments:



toJA
inserted by FC2 system