NaN issue in fortran 90

大憨熊 提交于 2019-12-10 20:45:15

问题


I realize that if you write

    Real (Kind(0.d0))::x,y
    x = sqrt(-1.d0)
    y = sqrt(-1.d0)
    if (x == y) then
       write(*,*)'yep, they are equals', x
    endif

It compiles ok using ifort. But nothing is written, the conditional is always false, did you notice that? why is this so?


回答1:


NaN signifies not a number, and since there are many, different, reasons a calculation could give that result, they generally do not compare as equal against themselves. If you want to do nan-testing, fortran compilers that support the f2003 standard (which is recent versions of most compilers) have ieee_is_nan in the ieee_arithmetic module:

program testnan
    use ieee_arithmetic

    real (kind=kind(0.d0)) :: x,y,z

    x = sqrt(-1.d0)
    y = sqrt(-1.d0)
    z = 1.d0

    if ( ieee_is_nan(x) ) then
       write(*,*) 'X is NaN'
    endif
    if ( ieee_is_nan(y) ) then
       write(*,*) 'Y is NaN'
    endif
    if ( ieee_is_nan(x) .and. ieee_is_nan(y) ) then
       write(*,*) 'X and Y are NaN'
    endif

    if ( ieee_is_nan(z) ) then
       write(*,*) 'Z is NaN, too'
    else
       write(*,*) 'Z is a number'
    endif

end program testnan

Compiling and running this program gives

ifort -o nan nan.f90

 X is NaN
 Y is NaN
 X and Y are NaN
 Z is a number

Unfortunately, gfortran still doesn't implement ieee_arithmetic as time of writing, so with gfortran you have to use the non-standard isnan.



来源:https://stackoverflow.com/questions/17349426/nan-issue-in-fortran-90

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