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

时间:2016-06-14 22:11:30

标签: gcc compilation fortran gfortran divide-by-zero

编译此模块时

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)

2 个答案:

答案 0 :(得分:4)

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

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

答案 1 :(得分:0)

正如弗拉基米尔所说,你将pi定义为0.定义pi的最佳方法是

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