Why doesn't C++ have a power operator?

38,264

Solution 1

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, having 2. ^ 8 == 256., but 2 ^ 8 == 10 isn't very pleasant either).

  • It couldn't be **, because that would break existing programs (which might have something like x**p, with x an int, and p an int*).

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

Solution 2

For two reasons

  1. The symbol ^ is reserved for bit-wise xor operation

  2. You 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!

Solution 3

Because that's the exclusive or bitwise operator.

There are functions called "pow" that do what you want though.

Share:
38,264
Admin
Author by

Admin

Updated on September 20, 2020

Comments

  • Admin
    Admin over 3 years

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

  • juanchopanza
    juanchopanza about 11 years
    Yeah, but why? ^ could be power, and some other symbol could have been chosen for XOR :-)
  • Admin
    Admin about 11 years
    Thanks Shai very much for this link!
  • Trudbert
    Trudbert about 11 years
    @juanchopanza Fortran uses ** for power. So you are right "^ is taken" isn't really a reason not to offer power as an operator
  • PlasmaHH
    PlasmaHH about 11 years
    @juanchopanza: Do you really use power of so often that it warrants an operator on its own?
  • Shai
    Shai about 11 years
    @PlasmaHH - its the power of pow!
  • juanchopanza
    juanchopanza about 11 years
    @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.
  • James Kanze
    James Kanze about 11 years
    @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.
  • juanchopanza
    juanchopanza about 11 years
    @JamesKanze now that's an answer, and exactly the kind of answer I was hoping to elicit.
  • Trudbert
    Trudbert about 11 years
    @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?"
  • James Kanze
    James Kanze about 11 years
    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.
  • David Hammen
    David Hammen about 11 years
    There 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.
  • Emilio Garavaglia
    Emilio Garavaglia about 11 years
    You 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)
  • James Kanze
    James Kanze about 11 years
    @DavidHammen There are ways of resolving the ambiguity, along the lines of that used for the final >> in vector<vector<T>>. 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
    James Kanze about 11 years
    @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.
  • theschmitzer
    theschmitzer almost 9 years
    pow() is not an operator. Operators can be evaluated statically. So this does not compile: enum { val = pow(2, 10) };