fortran之complex精度類型與cmplx函數精度類型大坑


cmplx(x,y)出來的結果為complex的默認精度不管x,y的精度是多少

 

從fortran wiki (http://fortranwiki.org/fortran/show/cmplx)上看cmplx函數有三種形式:

 

1.cmplx(x). x可以是interger、real、complex, 結果為complex的默認精度

 

2.cmplx(x,y). x可以是interger、real,y為integer、real,結果為complex的默認精度

 

3.cmplx(x,y,kind). x可以是interger、real,y為integer、real,kind為interger,結果精度為kind

 

complex(kind=n) 等價於complex*2n。例如:

complex(4) 等價於 complex*8

complex(8) 等價於 complex*16

complex(16) 等價於 complex*32

 

complex默認為complex(real, real)類型,real受編譯器的real影響。

 

-------------------------------------------------------------------------------------------------------------------------

from: http://fortranwiki.org/fortran/show/cmplx

Description

cmplx(x [, y [, kind]]) returns a complex number where x is converted to the real component. If y is present it is converted to the imaginary component. If y is not present then the imaginary component is set to 0.0. If x is complex then y must not be present.

Standard

FORTRAN 77 and later

Class

Elemental function

Syntax

result = cmplx(x [, y [, kind]])

Arguments

  • x - The type may be integerreal, or complex.
  • y - (Optional; only allowed if x is not complex.) May be integer or real.
  • kind - (Optional) An integer initialization expression indicating the kind parameter of the result.

Return value

The return value is of complex type, with a kind equal to kind if it is specified. If kind is not specified, the result is of the default complex kind, regardless of the kinds of x and y.

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

-------------------------------------------------------------------------------------------------------------------------
from:https://software.intel.com/content/www/us/en/develop/documentation/fortran-compiler-developer-guide-and-reference/top/language-reference/data-types-constants-and-variables/intrinsic-data-types/complex-data-types.html
Complex data types can be specified as follows:
COMPLEX
COMPLEX([KIND=]n)
COMPLEX*s
DOUBLE COMPLEX
 
Is a constant expression that evaluates to kind 4, 8, or 16.
 
s
Is 8, 16, or 32. COMPLEX(4) is specified as COMPLEX*8; COMPLEX(8) is specified as COMPLEX*16; COMPLEX(16) is specified as COMPLEX*32.
 
 
If a kind parameter is specified, the complex constant has the kind specified. If no kind parameter is specified, the kind of both parts is default real, and the constant is of type  default complex.
 
Default real is affected by compiler option real-size and by the REAL directive.
 
The default KIND for DOUBLE COMPLEX is affected by compiler option  double-size. If the compiler option is not specified, default DOUBLE COMPLEX is COMPLEX(8). No kind parameter is permitted for data declared with type DOUBLE COMPLEX.
 
A complex number of any kind is made up of a real part and an imaginary part. The REAL and AIMAG intrinsic functions return the real and imaginary parts of a complex number respectively. The CMPLX intrinsic constructs a complex number from two real numbers. The %re and %im complex part designators access the real and imaginary parts of a complex number respectively.
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM