(C++) 1.#IND [How to return this value]
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!
If memory serves me corerctly, 1.#IND means "Quiet NaN". According to Wikipedia
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
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
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.
edit: Oh, yeah, no such thing for integers though. Sorry.
Oh yea, duh. >.> Only works for floating points. If you want an undeterminate value for ints, use boost::optional
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!
UINT_MAX will do nicely for my purposes.
All answers were extremely useful and concise, love it!
How about
Did I get the return value right? :)
#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? :)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement