首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >参数语句编译时除以零

参数语句编译时除以零
EN

Stack Overflow用户
提问于 2016-06-14 22:11:30
回答 2查看 230关注 0票数 1

在编译此模块时

代码语言:javascript
复制
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

这个错误不断出现:

代码语言:javascript
复制
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使用以下方法设置:

代码语言:javascript
复制
integer, parameter :: rp = selected_real_kind ( REAL64 )

makefile生成以下编译命令:

代码语言:javascript
复制
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)和0 (MacPorts gcc7 7-20160605_0)的情况下仍然存在。

代码语言:javascript
复制
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)
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-06-14 23:35:44

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

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

票数 4
EN

Stack Overflow用户

发布于 2016-06-15 15:45:42

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

代码语言:javascript
复制
real ( kind=8 ), parameter :: pi=4.*atan(1.0)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37822970

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档