書いたらわかるよ
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
ifort を使用して正常にコンパイルされます。しかし、何も書かれておらず、条件は常にfalseであることに気付きましたか? これはなぜですか?
書いたらわかるよ
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
ifort を使用して正常にコンパイルされます。しかし、何も書かれておらず、条件は常にfalseであることに気付きましたか? これはなぜですか?
NaNは数値ではないことを意味します。また、計算によってその結果が得られるさまざまな理由が多数あるため、通常、NaN はそれ自体と等しいとは見なされません。nan-testing を行う場合、f2003 標準 (ほとんどのコンパイラの最新バージョン) をサポートする fortran コンパイラには、次ieee_is_nan
のieee_arithmetic
モジュールがあります。
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
このプログラムをコンパイルして実行すると、
ifort -o nan nan.f90
X is NaN
Y is NaN
X and Y are NaN
Z is a number
残念ながら、執筆時点では gfortran はまだ実装されていないieee_arithmetic
ため、gfortran では非標準の を使用する必要がありますisnan
。