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.

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.

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;

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.

Killerbee :

D = 0.5 * A * T * T + V * T

or, slightly more efficient,

D = (0.5 * A * T + V) * T

with one less multiplication.

Can you please elaborate it by giving an example.

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.0

Axe Murderer: you don't want to do that because of implicit/explicit conversion issues. If you're working with mixed types, you can end up in a situation where your integer field is implicitly converted to a double, and suddenly the ^ operator is doing something completely different from what you expected (XOR).

Thanks. It is working correctly………………

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.

I don't think I understand what you're saying. I don't concur with two of your statements:

1. "…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.

2. "…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.

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?

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

Having known many Microsoft engineers I think this is the reason there is no power operator … "this is my experience therefor everything else is invalid". In this case, perhaps true, since csharp is really not suitable for scientific programming. Symptomatic of MS's greater problems (the same problems Big Blue had when a young Bill Gates decided to think differently about computers) Too bad.

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 Raut

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));

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.

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.

Wait; what ?

let's use the same "argument" with the multiplication operator :

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

float result = value*2;

rather than having to resort to calling. We don't have one in our language.

It would be possible to add a multiplication 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.Mult() 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.Mult(value, 2.0);

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

Maybe you should remove the * operator ?

Conclusion : simple operations are simple. But when I write a scientific code and have real formula where the exponent is a double instead of "2", reading the same formula with Math.Pow is a pain in the ass (and is prone to error). You should really implement a ** (or ^, or anything) operator, or write explicitly in your online help "you shouldn't use C# to do any scientific code".