Many languages have a power operator; why doesn't C++? For example, Fortran and Python use **
and is commonly written (in LaTeX, for example) using ^
.

- 4,147
- 2
- 22
- 49
-
2`^` is the exclusive-or operator in C and C++. – Graham Borland Jan 31 '13 at 13:32
-
3Not that this is necessarily the reason, but it was already exclusive or (XOR) in C, so giving it another meaning would have broken backwards compatibility with C. – juanchopanza Jan 31 '13 at 13:33
-
Why doesn't C++ support power (^) operator? Because. – R. Martinho Fernandes Jan 31 '13 at 13:33
-
2Probably because it doesn't map to a nice instruction on a lot of platforms. – Alex Chamberlain Jan 31 '13 at 13:43
-
2Found an answer http://programmers.stackexchange.com/questions/54718/why-there-is-no-power-operator-in-java-c (or at least a duplicate with other answers than "the operator is taken") – Trudbert Jan 31 '13 at 13:46
-
To the edit: Java doesn't have a power operator but python has (also **) http://stackoverflow.com/questions/1991380/what-does-the-operator-do-in-java – Trudbert Jan 31 '13 at 13:50
-
@AlexChamberlain Nor did `/`, back when the original decision was made. – James Kanze Jan 31 '13 at 14:04
-
@JamesKanze I bet it did for integers? – Alex Chamberlain Jan 31 '13 at 14:05
-
@AlexChamberlain Not on the machines I was familiar with. Most smaller machines back then didn't have _any_ floating point support, and no hardware multiply and divide. – James Kanze Jan 31 '13 at 14:13
-
2@JamesKanze: This is 2013, not 1969 (the year when C was invented). Arguing that the C-based family of languages should still be limited to what was available back in the stone age is archaic thinking. There is no valid reason why C++ can't have exponentiation as an operator. – David Hammen Jan 31 '13 at 14:54
-
@DavidHammen I was just explaining why C had `^`. The only reason C++ hasn't added exponentiation as an operator is that the committee, after consulting with numerous experts in the field, was unable to find anyone who felt that it was necessary. Whether you spell exponentiation `x*^y` or `pow(x,y)` doesn't seem to make a difference to scientists and mathematicians. – James Kanze Jan 31 '13 at 15:01
4 Answers
C++ does have a power operator—it's written pow(x, y)
.
Originally, C was designed with system software in mind, and
there wasn't much need for a power operator. (But it has
bitwise operators, like &
and |
, which are absent in a lot
of other languages.) There was some discussion of adding one
during standardization of C++, but the final consensus was more
or less:
It couldn't be
^
, because the priority was wrong (and of course, having2. ^ 8 == 256.
, but2 ^ 8 == 10
isn't very pleasant either).It couldn't be
**
, because that would break existing programs (which might have something likex**p
, withx
anint
, andp
anint*
).It could be
*^
, because this sequence isn't currently legal in C or C++. But this would still require introducing an additional level of precedence.C and C++ already had enough special tokens and levels of precedence, and after discussions with the numerics community, it was concluded that there really wasn't anything wrong with
pow(x, y)
.
So C++ left things as they were, and this doesn't seem to have caused any problems.

- 150,581
- 18
- 184
- 329
-
4There is no ambiguity with `**`. `a ** b` doesn't make sense in the context of multiplication if `b` isn't a pointer. It does make sense in the context of exponentiation. The only rationale I can see is that the C/C++/C# community don't regard scientific programming as real programming. It's a major snub, and it's one of the key reasons Fortran programmers look down on C/C++/C# as being inferior languages for scientific programming. – David Hammen Jan 31 '13 at 14:05
-
6@DavidHammen There are ways of resolving the ambiguity, along the lines of that used for the final `>>` in `vector
>`. But they involve type dependent analysis, where the types of symbols modify the way the lexer works. If `**` were a legal token, the maximum munch rule would tokenize `a**b` as `a`, `**`, `b`, regardless. – James Kanze Jan 31 '13 at 14:09 -
3@DavidHammen And the committee actually discussed the issue with a number of specialists in the numerics field, and they didn't seem to consider it an issue to spell the power operator `pow(x, y)`. One of the reasons (perhaps the main reason) `*^` didn't make it is because we couldn't find anyone in the scientific community who cared. – James Kanze Jan 31 '13 at 14:12
-
5pow() is not an operator. Operators can be evaluated statically. So this does not compile: enum { val = pow(2, 10) }; – theschmitzer May 08 '15 at 16:08
For two reasons
The symbol
^
is reserved for bit-wise xor operationYou may use
std::pow
to achieve the same functionality.
The nice thing about C++ is that you can overload the operator
to do whatever you like it to do!
template< typename T >
T operator^( T x, T y ) {
return std::pow( x, y );
}
However take into account that when you do that, other people who know C++
and don't know you (and I believe there are quite a few of those) might have significant problems understanding your code!

- 111,146
- 38
- 238
- 371
-
5Yeah, but why? `^` could be power, and some other symbol could have been chosen for XOR :-) – juanchopanza Jan 31 '13 at 13:34
-
-
6@juanchopanza Fortran uses ** for power. So you are right "^ is taken" isn't really a reason not to offer power as an operator – Trudbert Jan 31 '13 at 13:38
-
3@juanchopanza: Do you really use power of so often that it warrants an operator on its own? – PlasmaHH Jan 31 '13 at 13:54
-
-
@PlasmaHH that has nothing to do with the reasons there is no such operator in C++. I am just pointing out that this answer doesn't justify *why* `^` was not chosen as a power operator. – juanchopanza Jan 31 '13 at 13:55
-
1@juanchopanza Originally, C was designed for more or less low level systems programming, where xor was a far more frequent operation than power. And after that, you can't change the operators without serious backward compatibility issues. – James Kanze Jan 31 '13 at 13:59
-
1@JamesKanze now *that's* an answer, and exactly the kind of answer I was hoping to elicit. – juanchopanza Jan 31 '13 at 14:00
-
@PlasmaHH having used languages with power operator and languages without I find the power operator a nice feature because math writes and reads more natural with a power operator rather than having to use some power function but thats subjective. But the question was "why isn't there one?" not "why would you need one?" – Trudbert Jan 31 '13 at 14:01
-
As for defining an overload, as suggested: what do you make of an expression like `2^n < 100`, or `a + 2^n`. The precedence is wrong. – James Kanze Jan 31 '13 at 14:01
Because that's the exclusive or bitwise operator.
There are functions called "pow" that do what you want though.

- 11,357
- 8
- 43
- 88

- 126,704
- 14
- 140
- 227
You could help yourself if you want
struct DoubleMock
{
DoubleMock(double v){_v = v;}
double _v;
};
double operator^(DoubleMock x, DoubleMock y)
{
return pow(x._v,y._v);
}
double v = DoubleMock(2.0) ^ 2.0;

- 37,963
- 15
- 156
- 475

- 119
- 4
-
1You must also provide all the operator set, or make it back convert implicitly to double. But the precedence will remain wrong (think to `a + 2^n`) – Emilio Garavaglia Jan 31 '13 at 14:05