- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
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
Link Copied
6 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
use, intrinsic :: IEEE_ARITHMETIC
...
array = IEEE_VALUE(IEEE_QUIET_NAN)
You can then use IEEE_IS_NAN to do the test.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Ah... this makes sense. That's exactly what I was looking for. Thanks!
Olivier
Olivier

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page