2016-06-14 45 views
1

編譯儘管編譯該模塊被零除在參數聲明

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) 

生成文件生成此編譯命令:

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) 

回答

4

0的餘弦爲1,因此爲acos(1)=0。因此你的pi是零,你除以零。你可能想pi = acos(-1._rp)

+0

正確的一次。謝謝。 – dantopa

0

正如普京說你是圓周率定義爲0來定義PI最好的辦法是

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

爲什麼它比acos(-1)好?乘以4 coul失去2位精度(但它不)。善良= 8醜陋,醜陋,醜陋。爲什麼在OP代碼不包含它時引入它?無論如何,你的RHS只有單精度。 –