How to do exponentiation in constant expression?
Since in your particular case you want to raise 2 into MaxExponent
power
2 ** MaxExponent
you can put it as a left shift, but if and only if MaxExponent
is a small positive integer value:
1 << MaxExponent
Like this
// double: see comments below `1L` stands for `long` and so MaxExponent = [0..63]
public const double MaxValue = MaxMantissa * (1L << MaxExponent);
In general case (when MaxExponent
is an arbitrary double
value), you can try changing const
to readonly
public static readonly double MaxValue = MaxMantissa * Math.Pow(2.0, MaxExponent);
You can't, basically (except, as noted, for the trivial case of powers of 2, which can be obtained via the shift operator).
You can hard-code the value and add a comment, or you can use a static readonly
, but note that static readonly
doesn't have the same "bake into the call-site" semantics. In most cases that doesn't present a problem.