Is there an exponent operator in C#?

后端 未结 8 1585
萌比男神i
萌比男神i 2020-11-28 08:09

For example, does an operator exist to handle this?

float Result, Number1, Number2;

Number1 = 2;
Number2 = 2;

Result = Number1 (operator) Number2;
<         


        
相关标签:
8条回答
  • 2020-11-28 09:09

    There is a blog post on MSDN about why an exponent operator does NOT exists from the C# team.

    It would be possible to add a power operator to the language, but performing this operation is a fairly rare thing to do in most programs, and it doesn't seem justified to add an operator when calling Math.Pow() is simple.


    You asked:

    Do I have to write a loop or include another namespace to handle exponential operations? If so, how do I handle exponential operations using non-integers?

    Math.Pow supports double parameters so there is no need for you to write your own.

    0 讨论(0)
  • 2020-11-28 09:09

    For what it's worth I do miss the ^ operator when raising a power of 2 to define a binary constant. Can't use Math.Pow() there, but shifting an unsigned int of 1 to the left by the exponent's value works. When I needed to define a constant of (2^24)-1:

    public static int Phase_count = 24;
    public static uint PatternDecimal_Max = ((uint)1 << Phase_count) - 1;
    

    Remember the types must be (uint) << (int).

    0 讨论(0)
提交回复
热议问题