Search code examples
gcccompilationfortrangfortrandivide-by-zero

Division by zero at compilation in parameter statement


While compiling this module

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

this error keeps appearing:

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

What is wrong with this statement?

The parameter rp is set using:

integer, parameter :: rp = selected_real_kind ( REAL64 )

The makefile generates this compilation command:

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

The problem persists under gcc 5 (shown), gcc 6 (MacPorts 6.1.0_0), and 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)

Solution

  • Cosine of 0 is 1, therefore acos(1)=0. Therefore your pi is zero and you are dividing by zero.

    You probably wanted pi = acos(-1._rp).