• Advertisement
Sign in to follow this  

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

This topic is 2933 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
Advertisement
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
I quess some of the members of the numeric_limits class will help. For example numeric_limits<float>::quiet_NaN().

edit: Oh, yeah, no such thing for integers though. Sorry.

Share this post


Link to post
Share on other sites
Oh yea, duh. >.> Only works for floating points. If you want an undeterminate value for ints, use boost::optional

Share this post


Link to post
Share on other sites
Thanks, I thought it was a weird request!
UINT_MAX will do nicely for my purposes.

All answers were extremely useful and concise, love it!

Share this post


Link to post
Share on other sites
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
Sign in to follow this  

  • Advertisement