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

setting a fortran variable to NaN

Scott_L_
New Contributor I
6,677 Views
There is the isnan function to test a variable for a NaN value, but is there a function I can use to set a variable to NaN? I am using ifort 17.1 on windows x64 and Linux. It would be preferable to not have to alter the /fpe options to let sqrt(-1) slide but if that's the best option, which fpe value would be safest on Linux and windows visual studio? thanks scott
0 Kudos
1 Solution
Arjen_Markus
Honored Contributor I
6,652 Views

You can do so with the function ieee_value(). Here is a small demo program:

program setnan
    use, intrinsic :: ieee_arithmetic

    implicit none

    real :: x

    x = ieee_value( x, ieee_signaling_nan )

    write(*,*) x

    x = ieee_value( x, ieee_quiet_nan )

    write(*,*) x
end program setnan         

View solution in original post

8 Replies
Arjen_Markus
Honored Contributor I
6,653 Views

You can do so with the function ieee_value(). Here is a small demo program:

program setnan
    use, intrinsic :: ieee_arithmetic

    implicit none

    real :: x

    x = ieee_value( x, ieee_signaling_nan )

    write(*,*) x

    x = ieee_value( x, ieee_quiet_nan )

    write(*,*) x
end program setnan         
Scott_L_
New Contributor I
6,628 Views
0 Kudos
Andrew_Smith
Valued Contributor I
5,677 Views

Is it possible to set a parameter to nan? Calling ieee_value as part of a parameter definition doesn't work:

use, intrinsic :: ieee_arithmetic
real(DP), parameter :: aNan = ieee_value(aNan, ieee_signaling_nan)

error #6259: This array or function or substring is invalid in constant expressions. [IEEE_VALUE]

0 Kudos
Steve_Lionel
Honored Contributor III
5,642 Views

IEEE_VALUE is not permitted in a constant expression, per the standard. I polled a couple of the committee members about this, and it was felt it had limited usefulness. You can use a hex literal if you want, or use run-time initialization.

0 Kudos
jimdempseyatthecove
Honored Contributor III
5,619 Views

The usefulness of having a parameter as a signaling is rather limited.

The use on rhs of = .OR. in IF(var == aNAN) should cause a trap...

... however, if the Fortran compiler generates a integer binary compare, then you will not trap...

however, if binary (integer) compare is performed, then it will detect one of 2^22 or 2^51possible SNAN's.

From: Microsoft.com

 

NaN - Not a Number

It's possible to represent values that aren't real numbers, such as 0 / 0, in the IEEE floating-point format. A value of this kind is called a NaN. A NaN is represented by an exponent of all ones and a non-zero significand. There are two kinds of NaNs, quiet NaNs, or QNaNs, and signaling NaNs, or SNaNs. Quiet NaNs have a leading one in the significand, and get propagated through an expression. They represent an indeterminate value, such as the result of dividing by infinity, or multiplying an infinity by zero. Signaling NaNs have a leading zero in the significand. They're used for operations that aren't valid, to signal a floating-point hardware exception.

Jim Dempsey

0 Kudos
Andrew_Smith
Valued Contributor I
5,595 Views

I use a nan parameter for debugging. I initialize components of derived types to nan so that I can tell if it gets used before initialisation. 

0 Kudos
JohnNichols
Valued Contributor III
5,589 Views

Another way is to initialize to a strange number that you will never use or is unlikely to occur, initialize at creation to that and then test for it, before you use it.  You are just looking for a change are you not. 

0 Kudos
JohnNichols
Valued Contributor III
5,584 Views

Screenshot 2023-05-26 090511.png

There will be the strangely interesting numbers potentially in it anyway, may as well make your own strange so you know you did it.  

0 Kudos
Reply