Fortran:将双精度复杂转换为单精度

DaP*_*hil 1 precision fortran

我可以使用双精度实变量转换为单精度sngl.但是,如何将双精度复数变量转换为单精度变量?

Vla*_*r F 8

REAL()对所有实数转换使用泛型,但SNGL()也可以用于特定情况:

  integer, parameter :: sp = kind(1e0), dp = kind(1d0)

  real(x)  !converts to the default kind, which is the single precision
           !sngl(x) does the same thing
  real(x, sp) ! converts to the kind sp (single precision)
  real(x, dp) ! converts to the kind dp (double precision)
Run Code Online (Sandbox Code Playgroud)

与复杂它是相同的,但使用CMPLX():

  cmplx(x)  !converts to the default kind, which is the single precision
  cmplx(x, sp) ! converts to the kind sp (single precision)
  cmplx(x, dp) ! converts to the kind dp (double precision)
Run Code Online (Sandbox Code Playgroud)

在赋值时,转换是隐式的,您不必(但可以)使用这些显式转换函数.

单精度和双精度的整个概念有些过时,并被Fortran 90种取代.

  • 我也是,但这改变了语义,我试图保持默认和双倍. (2认同)