Sign in to follow this  
SeiryuEnder

(C++) 1.#IND [How to return this value]

Recommended Posts

SeiryuEnder    199
I need to generate an unsigned int indeterminate value safely, this is to prevent wraparound on operations generating a value which the container cannot hold. I know how to check for one, but I've never created one. I'm thinking it's a specific bitfield but for the life of me can't find it. For instance... unsigned int DoThing(unsigned int A) { if(A > 1) return [1.#IND]; return A*UINT_MAX; } Thanks!

Share this post


Link to post
Share on other sites
cache_hit    614
If memory serves me corerctly, 1.#IND means "Quiet NaN". According to Wikipedia

Quote:

A bit-wise example of a IEEE floating-point standard single precision NaN: x111 1111 1axx xxxx xxxx xxxx xxxx xxxx where x means don't care. If a = 1, it is a quiet NaN, otherwise it is a signalling NaN.


Since you want a Quiet NaN, you'll want to set a=1. So:

x111 1111 11xx xxxx xxxxxxxx xxxxxxxx

Thus you'll want bits 22-30 to be 1, the rest doesn't matter. Therefore, given some floating point number f, you can just OR it with 0x7FC00000

Share this post


Link to post
Share on other sites
DevFred    840
How about

#include <stdexcept>

unsigned int DoThing(unsigned int A)
{
if (A > 1) throw std::invalid_argument("A must be greater than 1");
return -A;
}

Did I get the return value right? :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this