Fortran 简明教程

Fortran - Intrinsic Functions

本征函数是一些通用且重要的函数,它是 Fortran 语言的一部分。我们已经在“数组”、“字符”和“字符串”章节中讨论了一些这些函数。

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 −

  1. Numeric Functions

  2. Mathematical Functions

  3. Numeric Inquiry Functions

  4. Floating-Point Manipulation Functions

  5. Bit Manipulation Functions

  6. Character Functions

  7. Kind Functions

  8. Logical Functions

  9. 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,

  1. A represents any type of numeric variable

  2. R represents a real or integer variable

  3. X and Y represent real variables

  4. Z represents complex variable

  5. W represents real or complex variable

Numeric Functions

Sr.No

Function & Description

1

ABS (A) It returns the absolute value of A

2

AIMAG (Z) It returns the imaginary part of a complex number Z

3

AINT (A [, KIND]) It truncates fractional part of A towards zero, returning a real, whole number.

4

ANINT (A [, KIND]) It returns a real value, the nearest integer or whole number.

5

CEILING (A [, KIND]) It returns the least integer greater than or equal to number A.

6

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.

7

CONJG (Z) It returns the complex conjugate of any complex number Z.

8

DBLE (A) It converts A to a double precision real number.

9

DIM (X, Y) It returns the positive difference of X and Y.

10

DPROD (X, Y) It returns the double precision real product of X and Y.

11

FLOOR (A [, KIND]) It provides the greatest integer less than or equal to number A.

12

INT (A [, KIND]) It converts a number (real or integer) to integer, truncating the real part towards zero.

13

MAX (A1, A2 [, A3,…​]) It returns the maximum value from the arguments, all being of same type.

14

MIN (A1, A2 [, A3,…​]) It returns the minimum value from the arguments, all being of same type.

15

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)

16

MODULO (A, P) It returns A modulo P: (A-FLOOR(A/P)*P)

17

NINT (A [, KIND]) It returns the nearest integer of number A

18

REAL (A [, KIND]) It Converts to real type

19

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

Sr.No

Function & Description

1

ACOS (X) It returns the inverse cosine in the range (0, π), in radians.

2

ASIN (X) It returns the inverse sine in the range (-π/2, π/2), in radians.

3

ATAN (X) It returns the inverse tangent in the range (-π/2, π/2), in radians.

4

ATAN2 (Y, X) It returns the inverse tangent in the range (-π, π), in radians.

5

COS (X) It returns the cosine of argument in radians.

6

COSH (X) It returns the hyperbolic cosine of argument in radians.

7

EXP (X) It returns the exponential value of X.

8

LOG (X) It returns the natural logarithmic value of X.

9

LOG10 (X) It returns the common logarithmic (base 10) value of X.

10

SIN (X) It returns the sine of argument in radians.

11

SINH (X) It returns the hyperbolic sine of argument in radians.

12

SQRT (X) It returns square root of X.

13

TAN (X) It returns the tangent of argument in radians.

14

TANH (X) It returns the hyperbolic tangent of argument in radians.

Example

以下程序分别计算某个时间后的投射物水平和垂直位置 x 和 y,t −

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

其中,x = u t cos a 和 y = u t sin a - g t2 / 2

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

这些函数适用于某一整数和浮点数算术模型。这些函数返回与变量 X 同样的数字的特性,该变量可以是实数值,在某些情况下还可以是整数。

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.No

Function & Description

1

DIGITS (X) It returns the number of significant digits of the model.

2

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)).

3

HUGE (X) It returns the largest number of the model

4

MAXEXPONENT (X) It returns the maximum exponent of the model

5

MINEXPONENT (X) It returns the minimum exponent of the model

6

PRECISION (X) It returns the decimal precision

7

RADIX (X) It returns the base of the model

8

RANGE (X) It returns the decimal exponent range

9

TINY (X) It returns the smallest positive number of the model

Floating-Point Manipulation Functions

Sr.No

Function & Description

1

EXPONENT (X) It returns the exponent part of a model number

2

FRACTION (X) It returns the fractional part of a number

3

NEAREST (X, S) It returns the nearest different processor number in given direction

4

RRSPACING (X) It returns the reciprocal of the relative spacing of model numbers near given number

5

SCALE (X, I) It multiplies a real by its base to an integer power

6

SET_EXPONENT (X, I) it returns the exponent part of a number

7

SPACING (X) It returns the absolute spacing of model numbers near given number

Bit Manipulation Functions

Sr.No

Function & Description

1

BIT_SIZE (I) It returns the number of bits of the model

2

BTEST (I, POS) Bit testing

3

IAND (I, J) Logical AND

4

IBCLR (I, POS) Clear bit

5

IBITS (I, POS, LEN) Bit extraction

6

IBSET (I, POS) Set bit

7

IEOR (I, J) Exclusive OR

8

IOR (I, J) Inclusive OR

9

ISHFT (I, SHIFT) Logical shift

10

ISHFTC (I, SHIFT [, SIZE]) Circular shift

11

NOT (I) Logical complement

Character Functions

Sr.No

Function & Description

1

ACHAR (I) It returns the Ith character in the ASCII collating sequence.

2

ADJUSTL (STRING) It adjusts string left by removing any leading blanks and inserting trailing blanks

3

ADJUSTR (STRING) It adjusts string right by removing trailing blanks and inserting leading blanks.

4

CHAR (I [, KIND]) It returns the Ith character in the machine specific collating sequence

5

IACHAR © It returns the position of the character in the ASCII collating sequence.

6

ICHAR © It returns the position of the character in the machine (processor) specific collating sequence.

7

INDEX (STRING, SUBSTRING [, BACK]) It returns the leftmost (rightmost if BACK is .TRUE.) starting position of SUBSTRING within STRING.

8

LEN (STRING) It returns the length of a string.

9

LEN_TRIM (STRING) It returns the length of a string without trailing blank characters.

10

LGE (STRING_A, STRING_B) Lexically greater than or equal

11

LGT (STRING_A, STRING_B) Lexically greater than

12

LLE (STRING_A, STRING_B) Lexically less than or equal

13

LLT (STRING_A, STRING_B) Lexically less than

14

REPEAT (STRING, NCOPIES) Repeated concatenation

15

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.

16

TRIM (STRING) Removes trailing blank characters

17

VERIFY (STRING, SET [, BACK]) Verifies the set of characters in a string

Kind Functions

Sr.No

Function & Description

1

KIND (X) It returns the kind type parameter value.

2

SELECTED_INT_KIND ® It returns kind of type parameter for specified exponent range.

3

SELECTED_REAL_KIND ([P, R]) Real kind type parameter value, given precision and range

Logical Function

Sr.No

Function & Description

1

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