Is there an exponent operator in C#?
The C# language doesn't have a power operator. However, the .NET Framework offers the Math.Pow
method:
Returns a specified number raised to the specified power.
So your example would look like this:
float Result, Number1, Number2;
Number1 = 2;
Number2 = 2;
Result = Math.Pow(Number1, Number2);
Exponent operator
The e
is not an operator. Is is part of the real literal syntax of the C# language. It is evaluated when the code is parsed and can only be used to express a constant literal.
Just like you can use suffixes f
to denote a float, or d
to denote a double, the XeY
syntax allows you to define doubles with the value X * 10^Y
. It’s simply meant as a convenience when you’re writing code with constant numbers.
Here are some more examples on what you can do with all these different syntaxes, and which type and constant literal they are equivalent to:
var a = 2e5; // (double) 200000
var b = 2.3e3; // (double) 2300
var c = 13f; // (single) 13
var d = 53d; // (double) 53
var e = 1.2e3f; // (single) 1200
var f = 2.3m; // (decimal) 2.3
var g = 1.23e-4m; // (decimal) 0.000123
However, none of that is exposed to the .NET runtime, it is all evaluated at compile time and stored as exposed as constant literals—there is absolutely no difference between the short syntax and the full constant. As such, this functionality is not available for variables which only exist at run-time. If you need to exponentiate those, you will have to use other methods, for example myVariable * 10e5
.
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);
Overloaded exponents C# and exponent operator
You could create an extension method to act as a shortcut to Math.Pow(), this is actually a vey common thing to do.
perhaps:
public static double Pow(this double a, double b)
{
return Math.Pow(a, b);
}
So then you can use it like this:
myDouble = myDouble.Pow(3);
There's various formats you can play around with when it comes to extension methods. If your goal is brevity, I'm sure you can come up with something you'll be happy with.
Math.Pow() vs Math.Exp() C# .Net
Math.Pow
computes x y for some x and y.
Math.Exp
computes e x for some x, where e is Euler's number.
Note that while Math.Pow(Math.E, d)
produces the same result as Math.Exp(d)
, a quick benchmark comparison shows that Math.Exp
actually executes about twice as fast as Math.Pow
:
Trial Operations Pow Exp
1 1000 0.0002037 0.0001344 (seconds)
2 100000 0.0106623 0.0046347
3 10000000 1.0892492 0.4677785
How do you do *integer* exponentiation in C#?
A pretty fast one might be something like this:
int IntPow(int x, uint pow)
{
int ret = 1;
while ( pow != 0 )
{
if ( (pow & 1) == 1 )
ret *= x;
x *= x;
pow >>= 1;
}
return ret;
}
Note that this does not allow negative powers. I'll leave that as an exercise to you. :)
Added: Oh yes, almost forgot - also add overflow/underflow checking, or you might be in for a few nasty surprises down the road.
C# how to do e.g. 5*10^4 or numbers to the power of ten etc
You can write the literal float constant 5.0e4
(and it works in C, C++, Java, Fortran also).
How do I calculate power-of in C#?
See Math.Pow. The function takes a value and raises it to a specified power:
Math.Pow(100.00, 3.00); // 100.00 ^ 3.00
Related Topics
How to Get the Local Network Ip Address of a Computer Programmatically
Datagridview Checkbox Column - Value and Functionality
Why Does This Simple .Net Console App Have So Many Threads
When to Use Releasecomobject VS Finalreleasecomobject
When Is It Ok to Catch an Outofmemoryexception and How to Handle It
Edit Specific Element in Xdocument
Regex: Repeated Capturing Groups
Should I Transform Entity (Persistent) Objects to Dto Objects
Get String Name of Property Using Reflection
Thread Safety of a Dictionary<Tkey, Tvalue>
Setting Dropdownlist Selecteditem Programmatically
Is There a Synchronization Class That Guarantee Fifo Order in C#
Reasons for Why a Winforms Label Does Not Want to Be Transparent
"The Semaphore Timeout Period Has Expired" Error for Usb Connection