Why doesn't C# have a power operator?

Why doesn't C# have a power operator?

  • Comments 25

Some languages provide a power operator, so one can write something like:

float result = value^2;

rather than having to resort to calling. We don't have one in C#.

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.

I also worry a bit about the implementation of such operators. It seems likely that most compilers would translate my example above to:

float result = Math.Pow(value, 2.0);

That works, but has the unfortunate side effect of replacing a simple multiplication (value * value) with a complex trig function.

Leave a Comment
  • Please add 8 and 3 and type the answer here:
  • Post
  • PingBack from http://cellulitecreamsite.info/story.php?id=9480

  • PingBack from http://topalternativedating.info/story.php?id=12859

  • I think having a power operator in C# would be really useful personally.

    http://www.createwebspace.co.uk

  • Thanks for info. I was lookign for it

  • > but performing this operation is a fairly rare thing to do in most programs

    ????????? No rarer than many operations for which we have operators ... e.g. bit-shift

  • "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"

    this is sloppy journalism and as pointed out by puzzled is inconsistent reasoning

    you are producing a major language pls don't shoot from the hip

  • If C# team wants to developed a language that would be popular with engineering and scientific community the power operator is a must. It is used as often as other operators. I personally spend hours waisted typing Math.Pow(....).

    What is even more important is the formulas become much more difficult to check with so much more useless characters, this REALLY INTRODUCES A POSSIBILITY FOR A MISTAKE. Just add the operator. Please.

  • It would be nice if the mechanism of defining new operators would be part of the languange, like in Algol 68. For scientific programming the often complex expressions become much easier to read and verify if the operators common to the specific field of science can be used in the expressions in the software.

  • What is wrong with them adding the ^ symbol as the power operator..its used for that in so many other programs, avalible on most keyboards and easy to use.

  • I've  been using C# for the past 10 years and I've just discovered that ^ symbol does work for after 8 hours looking for a bug which doesnt exist !  This is pretty ridiculous Microsoft. I understand the OOP principle of implementing all Math functions in one object, but really. Almost very other language uses the '^' operator.

  • I agree with the six posts above.  Practically everything I do in any variant of C is "a fairly rare thing to do," probably even more so in c#.   I'm beginning to love the language in spite of myself, but add the operator.

  • I agree that it would be nice for C# to have a power operator, but...

    ... ^ is already used for a logical XOR, which it inherits from C/C++. I suggest using "**" (double asterisk) as a power operator, given that there is no pointer dereferencing in C#.

  • I use bit-shift as much as +, yet in 15 years of coding in C/C++/C# have called pow() (or variations of) maybe a dozen times. I am very confident my usage is the norm rather than the exception.

    Keep in mind they are general programming languages. Adding functionality from non-general domains (e.g. math, i/o, graphics, etc.) adds unneeded complexity to the language.

Page 1 of 2 (25 items) 12