fortran - 在 Fortran 中使用 CMPLX 函数实现 double 或四精度

标签 fortran gfortran

这是我的代码:

    Program Complex_Double_Quad_Precision

    Use, intrinsic :: iso_fortran_env

    Implicit none

    Integer, parameter :: sp = REAL32
    Integer, parameter :: dp = REAL64
    Integer, parameter :: qp = REAL128

    Real(sp),parameter:: Some_Value_0 = 4.56
    Real(dp),parameter:: Some_Value_1 = 4.56
    Real(qp),parameter:: Some_Value_2 = 4.56

    Complex(sp):: Value_0
    Complex(dp):: Value_1
    Complex(qp):: Value_2

    Value_0 = cmplx( Some_Value_0 , Some_Value_0 )
    Value_1 = cmplx( Some_Value_1 , Some_Value_1 )
    Value_2 = cmplx( Some_Value_2 , Some_Value_2 )

    Write(*,*) Value_0
    Write(*,*) Value_1
    Write(*,*) Value_2

    End Program Complex_Double_Quad_Precision

我的集成开发环境: 代码块 16.01。

我打算学习如何在复杂计算中使用 double 或四精度。在我的代码中,编译并启动代码后,我收到了这条消息:

Conversion from REAL(8) to default-kind COMPLEX(4) might lose precision, consider using the KIND argument

是否有任何方法可以使用 KIND 参数以及 dpqpCMPLX 函数结果的精度?

最佳答案

如警告消息所述,您希望使用 CMPLX 的可选第三个参数来指定返回值的类型。参见例如https://gcc.gnu.org/onlinedocs/gfortran/CMPLX.html

或者,在您的代码中:


Value_1 = cmplx( Some_Value_1 , Some_Value_1, dp )
Value_2 = cmplx( Some_Value_2 , Some_Value_2, qp )

关于fortran - 在 Fortran 中使用 CMPLX 函数实现 double 或四精度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47857242/

相关文章:

linux - 写入 fifo(命名管道)

compiler-construction - gfortran 编译器错误?

fortran - 错误: Unexpected element '*' in format string at (1)

python - Fortran 类似于 python 的 super()?

Fortran:将任意 "structures"传递给模块子例程

loops - Gfortran -Wuninitialized 标志在 do 循环中丢失变量

r - 安装 r-packages 时找不到 gfortran

arrays - c_loc() 与动态数组

将复杂条件从 C 转换为 Fortran

octave - Homebrew Octave 在小牛队中找不到 gfortran