^ is a bitwise xor operator in C++, not a mathematical exponentiation operator. You have a few alternatives.
- Because you're storing the constant in an intrinsically lossy format, a
float
, you could just work out the base-10e
form literal and use that: perhaps something like4.6566128730773926e-010
for 2^-31. This is prone to error (I've made one, for example), and isn't necessarily portable between floating point formats. - You can set the constant using a constant expression that can be evaluated at compile time, and use an integer literal: either
0x80000000
or1UL << 31
for 2^31, or1.0f / 0x80000000
for 2^-31 for example.
You could use a pow
function of some kind to calculate the value at runtime, of course.
Incidentally, if you're using integers, why not just use a long long
or other 64bit integral type, instead of a floating point which may well present you with rounding errors? It isn't totally clear from your question whether you are looking at floating point values because you need floating point value ranges, or because you are merely worried about overflowing a 32-bit integer value.