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

Fortran:将双精度复数转换为单精度,fortran,precision,Fortran,Precision,我可以使用sngl将双精度实变量转换为单精度。但是如何将双精度复数变量转换为单精度复数变量?您可以在单精度复数变量中创建副本,或者 您还可以使用cmplx和dcmplx功能: program test complex*16:: a complex :: b a = (1.d0, 2.d0) b = a print *, a, b print *, cmplx(a), dcmplx(b) end 输出: (1.00000000000000,2.00000000

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

您可以在单精度复数变量中创建副本,或者 您还可以使用cmplx和dcmplx功能:

program test
  complex*16:: a
  complex   :: b
  a = (1.d0, 2.d0)
  b = a

  print *,  a, b
  print *,  cmplx(a), dcmplx(b)
end
输出:

 (1.00000000000000,2.00000000000000) (1.000000,2.000000)
 (1.000000,2.000000) (1.00000000000000,2.00000000000000)

可以在单精度复杂变量中创建副本,或 您还可以使用cmplx和dcmplx功能:

program test
  complex*16:: a
  complex   :: b
  a = (1.d0, 2.d0)
  b = a

  print *,  a, b
  print *,  cmplx(a), dcmplx(b)
end
输出:

 (1.00000000000000,2.00000000000000) (1.000000,2.000000)
 (1.000000,2.000000) (1.00000000000000,2.00000000000000)
对所有实数转换使用通用实数,尽管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)
对于复杂系统,它是相同的,但使用CMPLX:

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

单精度和双精度的整个概念有些过时,已被取代。

对所有实数转换使用通用实数,尽管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)
对于复杂系统,它是相同的,但使用CMPLX:

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


单精度和双精度的整个概念有些过时,已被取代。

我更喜欢使用iso_fortran_env和integer,参数::sp=real32,dp=real64Me,但这在某种程度上改变了语义,我尝试将其保留为默认值和双精度。我更喜欢使用iso_fortran_env和integer,参数::sp=real32,dp=real64Me,但这在一定程度上改变了语义我试图将其保留为默认值和双精度值。我不得不说,现在我更喜欢Valdimir的解决方案,我还应该注意到,complex*16不是标准的Fortran——同样,在moder Fortran中解决这一问题的机制也必须说,现在我更喜欢Valdimir的解决方案,而且我还应该注意到complex*16不是标准的Fortran——同样,在moder Fortran中解决这一问题的机制也是