📜  Fortran-内部函数

📅  最后修改于: 2020-11-04 06:21:31             🧑  作者: Mango


内部函数是作为Fortran语言的一部分提供的一些常见且重要的函数。我们已经在数组,字符和字符串一章中讨论了其中一些功能。

内在函数可以归类为-

  • 数值函数
  • 数学函数
  • 数值查询功能
  • 浮点运算功能
  • 位操作功能
  • 字符功能
  • 种类函数
  • 逻辑功能
  • 数组函数。

我们已经在“数组”一章中讨论了数组函数。在以下部分中,我们将简要介绍其他类别中的所有这些功能。

在函数名称列中,

  • A代表任何类型的数值变量
  • R代表实数或整数变量
  • X和Y代表实变量
  • Z代表复变量
  • W代表实变量或复变量

数值函数

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.

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

当您编译并执行上述程序时,它将产生以下结果-

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)

数学函数

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.

以下程序在时间t之后分别计算弹丸的水平位置x和垂直位置y-

其中,x = ut cos a和y = ut 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

当您编译并执行上述程序时,它将产生以下结果-

x: 141.421356  y: -1818.57861  

数值查询功能

这些函数与整数和浮点运算的特定模型一起使用。函数返回与变量X相同类型的数字的属性,该属性可以是实数,在某些情况下为整数。

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

浮点运算功能

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

位操作功能

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

字符功能

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 (C)

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

6

ICHAR (C)

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

种类函数

Sr.No Function & Description
1

KIND (X)

It returns the kind type parameter value.

2

SELECTED_INT_KIND (R)

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

逻辑功能

Sr.No Function & Description
1

LOGICAL (L [, KIND])

Convert between objects of type logical with different kind type parameters