- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Is there a way to have a function return a NaN without calling a math function and causing a NaN
Link Copied
3 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
Thus, f = TRANSFER(Z'7FFFFFFF', f) or even f = TRANSFER(Z'FFFFFFFF', f) should produce a 32-bit NaN; similar holds for double precision.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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

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