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

Returning a NaN

Dishaw__Jim
Beginner
480 Views
Is there a way to have a function return a NaN without calling a math function and causing a NaN
0 Kudos
3 Replies
Jugoslav_Dujic
Valued Contributor II
480 Views
TRANSFER is your friend. If I recall correctly, an IEEE single-precision NaN has the bit pattern 0-11111111-xxxxxxxxxxxxxxx, where x's are not zero. See IEEE 754.

Thus, f = TRANSFER(Z'7FFFFFFF', f) or even f = TRANSFER(Z'FFFFFFFF', f) should produce a 32-bit NaN; similar holds for double precision.
0 Kudos
TimP
Honored Contributor III
480 Views
It's not entirely clear what you are getting at. Depending on the compiler, you might be able to get a NaN constant by 0./0. This lacks generality, as many compilers would make this a fatal error, unless you hide it somehow. transfer() with a suitable bit pattern might be what you mean. It also lacks generality, but might work for all platforms which support ifort.
0 Kudos
Dishaw__Jim
Beginner
480 Views
There were two applications that I had in mind. I want to set the return value of a function to NaN when the input values are illegal. The other application was to use NaN as a guard value to determine if a variable was changed from its initialized state. The guard value I was using, a large negative number, was not always reliable.

The TRANSFER() approach seems like the way to go

0 Kudos
Reply