Why doesn't C# have a power operator?
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.
Anonymous
June 08, 2009
PingBack from http://cellulitecreamsite.info/story.php?id=9480Anonymous
June 16, 2009
PingBack from http://topalternativedating.info/story.php?id=12859Anonymous
October 13, 2009
The comment has been removedAnonymous
December 15, 2009
Thanks for info. I was lookign for itAnonymous
March 29, 2010
> 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-shiftAnonymous
April 18, 2010
"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 hipAnonymous
May 02, 2010
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.Anonymous
June 09, 2010
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.Anonymous
June 30, 2010
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.Anonymous
July 20, 2010
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.Anonymous
August 17, 2010
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.Anonymous
September 21, 2010
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#.Anonymous
November 19, 2010
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.Anonymous
April 27, 2011
Here's a small script I wrote for power in C#. // Value variable is the number you want multiplied, b is the power you want it multiplied to. int a, b, num, solution; a = value; num = a; for (int x = 1; x < b; x++) num = num * a; solution = num; //return solution;Anonymous
May 19, 2011
The formula for calculating the distance that an object has moved under a constant acceleration is given by D = ½ * A * T^2 + V * T Where D is the distance traveled A is the constant acceleration T is the time traveled in seconds V is the starting velocity For instance, a car that is moving at 10 feet per second and accelerates at 2 feet per second per second will travel 75 feet in 5 seconds. ½ * 2 * 52 + 10 * 5 = 75 Write a program that assigned reasonable values to the acceleration, starting velocity, and time and calculates the distance traveled (in feet). How can I do this in C# and do it in such a way that C# also recognizes the "T^2" which is really T squared.Anonymous
June 09, 2011
Killerbee : D = 0.5 * A * T * T + V * T or, slightly more efficient, D = (0.5 * A * T + V) * T with one less multiplication.Anonymous
July 10, 2011
Can you please elaborate it by giving an example.Anonymous
September 11, 2011
Since it doesn't make much sense to XOR with doubles, why don't you simply take advantage of the polymorphic behavior of the language and define an operator for the ^ that does a Math.Pow when the operands are doubles? public static double operator ^( double power ) { return Math.Pow( this, power ); } I think that's the right syntax. Now as long as you use ints you get XOR int a = 1; int b = 1; int c = a^b; <---- c get set to 0 but when you use doubles you get power functionality: double a = 2.0; double b = 3.0; double c = a^b; <----- c gets set to 8.0Anonymous
November 10, 2011
The comment has been removedAnonymous
July 18, 2012
Thanks. It is working correctly..................Anonymous
August 21, 2012
Just shows that language developers often don't understand real-world development (IMO). Exponentiation is used in interest calculations - very common in business software development. COBOL, Progress OpenEdge, and many other languages can have an exponentiation operator - C# should also hve it.Anonymous
May 31, 2014
I don't think I understand what you're saying. I don't concur with two of your statements:
- "...a simple multiplication (value * value)..." Any mathematically based program will be performing many POWER implementations: What if I needed to raise a number to the 6th degree? The mathematical expression would be so lengthy it would be easily misunderstood. For example, using your second order style: result = (value * value * value * value * value * value); OR, much better: result = value ^ 6; Then add the fact that often times there are parameters of all orders beneath the highest, 6 being the case in my example. Could you imagine the length, and lack of readability for my following example written as you suggest is just as easy? result = (3.14 * value ^ 6) + (0.159 * value ^ 5) - (0.265 * value ^ 4) ..... ; It would literally be so intangible that nobody could read it with any sense of validity, let alone comprehension.
- "...but performing this operation is a fairly rare thing to do in most programs..." To state that this type of operation would rarely be used is to state that nearly no mathematician would ever write a program in C#. I thus RESPECTFULLY disagree with your posting.
Anonymous
July 05, 2014
fairly rare?? Maybe. If one is a literature teacher, I agree. But give C# to a matematician or a physicist and suddenly the power operator becomes as common as the "+" and "-". Why the user discrimination?Anonymous
July 31, 2014
The comment has been removedAnonymous
August 28, 2014
Sir, It's microsofts reputation is the best one. Kindly stood up to it. Agree to the real world scenario. I am a commerce background person but I also observed that this power operator is regularly needed. It will be great if you respond to the feedbacks positively. Regards Milind RautAnonymous
May 26, 2015
you can try with this << operator int i = 4; // Shift i one bit to the left. The result is 2. Console.WriteLine("{0}", 1 << i); Console.WriteLine("{0}", Math.Pow(2,i));Anonymous
May 31, 2015
I love the C# language (and its other predecessors) because of it's ability to write concise code. The other .NET language (not to be mentioned herein) is simply VerBose. However, this other language does implement a power operator, which enables coders to write math expressions concisely. I don't understand the resistance to implementing a power operator in the C# language. The answer provided to subject question is not compelling. Whether or not this operation is performed rarely in most programs should not matter. There seems to be at least an interest (if not a demand) in having it implemented and it does exist in other languages. Since it already exists in that other .NET language, it can't be that difficult to implement. While Math.Pow() is simple, value [op] power is more concise and more closely resembles the formula one wishes to code (In an earlier incarnation, I was a FORTRAN developer). Regarding implementation, document accordingly (e.g., value [op] power is effectively Math.Pow(value, power) and let the developer decide whether to code float result = Math.Pow(value, 2.0) v. float result = value * value.Anonymous
August 14, 2015
You said "performing this operation is a fairly rare thing to do in most programs"... have you ever done any kind of scientific programming, or even gaming? Raising to integer powers is certainly an extremely common thing to do, and I would much rather use an operator to do this, than having to call Math.Pow(...). Much more readable uisng an operator.Anonymous
October 05, 2015
The comment has been removed