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 ^.
Many languages have a power operator; why doesn’t C++? For example, Fortran and Python
Share
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 lotof 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 ofcourse, having
2. ^ 8 == 256., but2 ^ 8 == 10isn’t verypleasant either).
It couldn’t be
**, because that would break existingprograms (which might have something like
x**p, withxanint, andpanint*).It could be
*^, because this sequence isn’t currently legalin 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.