如何在 Fortran 模块中设置 mpreal 参数

How to set a mpreal parameter inside a Fortran module

我非常希望能够在 Fortran 模块中定义一些参数,以便主程序及其所有子例程都可以访问它们。

问题是,即使我尝试在程序主体中声明它们,例如:

type (mp_real), parameter :: p1   = 1.98342E+5 !(or say 1.1d0)

我收到以下错误:

Error: Can't convert REAL(4) to TYPE(mp_real) at (1)

基本上我在程序体内设置参数的值没有问题,但是如果我尝试在变量声明区内进行,gfortran就不高兴了。这阻止了我在模块中定义它们。 有没有办法用 mpreal 设置模块内的参数值,或者我应该重新编写整个程序?

我最好的猜测是您正在使用 mpfun 库,它定义了一个类型 mp_real 您如何使用它。

然后,他们重载赋值运算符以便能够转换类型。此运算符在编译时不可用,这会导致您收到错误。

这里有一个例子来说明这一点:

module test_mod

  type my_type
    integer :: val
  end type

  interface assignment (=)
    module procedure my_assign
  end interface

contains

  subroutine my_assign( t, v )
    type(my_type),intent(out) :: t
    integer, intent(in)       :: v
  end subroutine
end module

program test
  use test_mod

  type(my_type),parameter :: t = 1
end program

据我所知,无法按照您建议的方式使用派生类型。但是,您可以将 p1 存储为真实值,并使用初始化子例程...