Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Initialize a real array with NaNs

OP1
New Contributor III
1,945 Views
I have a subroutine which initializes an array of real numbers. At the end of this subroutine, I want to make sure that all elements of the array have been initialized. All real values are admissible initial values (including signed infinity numbers, etc).

Is is possible to force all values of my array to be NaNs at the beginning of the subroutine; then check at the end if there are any NaNs left (using ISNAN). How can I conveniently set my array elements to be NaNs?

Thanks,

Olivier
0 Kudos
6 Replies
Steven_L_Intel1
Employee
1,946 Views
I think this should work:

use, intrinsic :: IEEE_ARITHMETIC
...
array = IEEE_VALUE(IEEE_QUIET_NAN)

You can then use IEEE_IS_NAN to do the test.
0 Kudos
OP1
New Contributor III
1,946 Views
Thanks Steve - I just tried the sample program below:

PROGRAM TEST_NAN
USE,INTRINSIC :: IEEE_ARITHMETIC
IMPLICIT NONE
INTEGER,PARAMETER :: DP = SELECTED_REAL_KIND(10,100)
REAL(KIND=DP) R
R = IEEE_VALUE(IEEE_QUIET_NAN)
WRITE(*,*) R
END PROGRAM TEST_NAN

I get compiler errors. I am using IVF 10.1, so I suppose that your example works for higher versions of IVF.Do you think there mightbe a work around (manipulating the bits of R, for instance)?

Olivier
0 Kudos
TimP
Honored Contributor III
1,946 Views
If your compiler doesn't support IEEE_ARITHMETIC, this makes a good case for an upgrade.
For earlier compilers, you could generate a NaN by TRANSFERing a z'....' constant, or by calling a function (as quick and dirty as could be imagined) such as
function rnan(x)
!dir$ optimize:0
rnan = (x-x)/(x-x)
return
0 Kudos
OP1
New Contributor III
1,946 Views
Quoting - tim18
If your compiler doesn't support IEEE_ARITHMETIC, this makes a good case for an upgrade.
For earlier compilers, you could generate a NaN by TRANSFERing a z'....' constant, or by calling a function (as quick and dirty as could be imagined) such as
function rnan(x)
!dir$ optimize:0
rnan = (x-x)/(x-x)
return

Thanks! This is the obvious way, and it works. It's not very elegant though, and possibly subject to how the compiler handles this. Steve's answer is more in line with what I was looking for; it just turns out that in IVF 10.1 declaring usage ofthe intrinsic module IEEE_ARITHMETIC (through USE,INTRINSIC :: IEEE_ARITHMETIC) does not trigger a compiler error, but the variables and functions Steve mentionned will. It seems they are not declared (or accessible) in this module.

Olivier
0 Kudos
Steven_L_Intel1
Employee
1,946 Views

Version 10.1 included the module as a stub.

I do not recommend using the method here - the compiler is free to optimize that into something unexpected. Use this instead:

! Quiet NAN, double precision.
REAL(8), PARAMETER :: D_QNAN = &
TRANSFER((/ Z'00000000', Z'7FF80000' /),1.0_8)
0 Kudos
OP1
New Contributor III
1,946 Views
Ah... this makes sense. That's exactly what I was looking for. Thanks!

Olivier
0 Kudos
Reply