除以零不适用于gfortran 5.4中的ieee_arithmetic [英] division by zero doesn't work with ieee_arithmetic in gfortran 5.4

查看:226
本文介绍了除以零不适用于gfortran 5.4中的ieee_arithmetic的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在运行gfortran版本5.4.0的Linux机器上将ieee_arithmetic与Fortran一起使用.

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

尝试初始化InfNaN的值时出现除以零的错误.

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

ieee_arithmetic似乎没有问题,因为文件中的其他地方我可以成功调用ieee_is_finite()而没有任何问题.

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.

我认为ieee_arithmetic允许在这些特定情况下使用零除,但我肯定缺少一些东西.下面是代码示例:

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.

要重现该错误,请将上面的代码片段另存为rcrlib_gnu_example.f90,然后执行以下行: gfortran -o rcr rcrlib_gnu_example.f90

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

产生的错误输出是

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)

推荐答案

感谢 Pascal Cuoq ,我解决了问题.

Thanks to Pascal Cuoq, I solved the problem.

编译的initialize子例程的版本如下:

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

因此,基本上将infinity设置为最大浮点数加100,然后将NaN设置为无穷大值与其自身之间的差.

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

非常感谢您对我缺乏FORTRAN经验的快速反应和耐心.

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

这篇关于除以零不适用于gfortran 5.4中的ieee_arithmetic的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆