在参数语句中编译时除以零

Division by zero at compilation in parameter statement

编译此模块时

module mConstants

    use, intrinsic :: iso_fortran_env,  only : input_unit, output_unit, error_unit
    use mPrecisionDefinitions,          only : ip, rp

    implicit none

    ! real constants
    real ( rp ), parameter    :: zero = 0.0_rp
    real ( rp ), parameter    :: one  = 1.0_rp
    real ( rp ), parameter    :: half = 0.5_rp

    real ( rp ), parameter    :: pi = acos ( one )

    real ( rp ), parameter    :: rad_to_deg = 180.0_rp / pi
    real ( rp ), parameter    :: deg_to_rad = pi / 180.0_rp
    ...    
end module mConstants

此错误不断出现:

mod_constants.f08:16:54:

     real ( rp ), parameter    :: rad_to_deg = 180.0_rp / pi
                                                      1
Error: Division by zero at (1)
make: *** [mod_constants.o] Error 1

这个说法有什么问题吗?

参数 rp 设置使用:

integer, parameter :: rp = selected_real_kind ( REAL64 )

makefile 生成此编译命令:

gfortran -c -g -ffpe-trap=denormal -fbacktrace -Wall -Waliasing -Wconversion-extra -Wextra -Wsurprising -Wimplicit-procedure -Wintrinsics-std -Wuse-without-only -Og -pedantic -fcheck=bounds -fmax-errors=5 -Wuse-without-only -o mod_constants.o mod_constants.f08

问题在 gcc 5(如图所示)、gcc 6 (MacPorts 6.1.0_0) 和 gcc 7 (MacPorts gcc7 7-20160605_0) 下仍然存在。

gfortran -v
Using built-in specs.
COLLECT_GCC=gfortran
COLLECT_LTO_WRAPPER=/opt/local/libexec/gcc/x86_64-apple-darwin15/5.4.0/lto-wrapper
Target: x86_64-apple-darwin15
Configured with: /opt/local/var/macports/build/_opt_mports_dports_lang_gcc5/gcc5/work/gcc-5.4.0/configure --prefix=/opt/local --build=x86_64-apple-darwin15 --enable-languages=c,c++,objc,obj-c++,lto,fortran,java --libdir=/opt/local/lib/gcc5 --includedir=/opt/local/include/gcc5 --infodir=/opt/local/share/info --mandir=/opt/local/share/man --datarootdir=/opt/local/share/gcc-5 --with-local-prefix=/opt/local --with-system-zlib --disable-nls --program-suffix=-mp-5 --with-gxx-include-dir=/opt/local/include/gcc5/c++/ --with-gmp=/opt/local --with-mpfr=/opt/local --with-mpc=/opt/local --with-isl=/opt/local --enable-stage1-checking --disable-multilib --enable-lto --enable-libstdcxx-time --with-build-config=bootstrap-debug --with-as=/opt/local/bin/as --with-ld=/opt/local/bin/ld --with-ar=/opt/local/bin/ar --with-bugurl=https://trac.macports.org/newticket --with-pkgversion='MacPorts gcc5 5.4.0_0'
Thread model: posix
gcc version 5.4.0 (MacPorts gcc5 5.4.0_0)

0 的余弦为 1,因此 acos(1)=0。因此你的圆周率是零,你除以零。

你可能想要 pi = acos(-1._rp).

正如 Vladimir 所说,您将 pi 定义为 0。定义 pi 的最佳方法是

real ( kind=8 ), parameter :: pi=4.*atan(1.0)