division by zero doesn't work with ieee_arithmetic in gfortran 5.4

旧时模样 提交于 2019-12-11 03:55:22

问题


I'm using ieee_arithmetic with Fortran on a Linux machine that runs gfortran version 5.4.0.

I'm getting an error of division by zero when trying to initialize values for Inf and NaN.

There doesn't seem to be an issue with ieee_arithmetic because elsewhere in the file I can successfully call ieee_is_finite() with no issues.

I thought that ieee_arithmetic allowed division by zero to be used for these specific cases, but I must be missing something. Below is a sample of code:

module rcrlib_gnu
    use, intrinsic :: ieee_arithmetic ! requires gfortran version 5.0 or higher
    implicit none
    integer, parameter :: SP=kind(1.0), DP=selected_real_kind(9,99)
    integer, parameter :: stderr=0
    public SP, DP, is_finite, stderr, initialize

contains

subroutine initialize(infty,nan)
    real(kind=DP), intent(out) :: infty, nan
    infty = 1.0_dp/0.0_dp ! huge(1.0_dp)
    nan = 0.0_dp/0.0_dp
end subroutine initialize

elemental function is_finite(x)
    real(kind=DP), intent(in) :: x
    logical :: is_finite
    is_finite = ieee_is_finite(x) ! This call requires "ieee_arithmetic"
end function is_finite 

end module rcrlib_gnu

It seems I'm missing something basic, so I would appreciate any help.

To reproduce the error, save the above code snippet as rcrlib_gnu_example.f90 and then execute the following line: gfortran -o rcr rcrlib_gnu_example.f90

The resulting error output is

rcrlib_gnu_example.f90:12:18:

     infty = 1.0_dp/0.0_dp ! huge(1.0_dp)
                  1
Error: Division by zero at (1)
rcrlib_gnu_example.f90:13:16:

     nan = 0.0_dp/0.0_dp
                1
Error: Division by zero at (1)

回答1:


Thanks to Pascal Cuoq, I solved the problem.

The version of the initialize subroutine that compiles is below:

subroutine initialize(infty,nan)
    real(kind=DP), intent(out) :: infty, nan
    infty = huge(1.0_dp)+100
    nan = infty-infty
end subroutine initialize

So basically set infinity to be the largest floating point number plus 100, then set NaN to be the difference between infinity and itself.

Thanks, all, for your quick responses and patience with my lack of FORTRAN experience.



来源:https://stackoverflow.com/questions/45744321/division-by-zero-doesnt-work-with-ieee-arithmetic-in-gfortran-5-4

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!