In the C Programming Language, the pow function returns x raised to the power of y.
C++ does not include an exponent operator. Note that the parameters (and return value) of function pow() are of type double.
But in C, there is no built-in exponentiation operator, and the ^ operator is used for bitwise exclusive or (i.e., bitwise XOR). In C, the pow library function (with its siblings powf and powl) performs exponentiation.
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.
For two reasons
The symbol ^
is reserved for bit-wise xor operation
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!
Because that's the exclusive or bitwise operator.
There are functions called "pow" that do what you want though.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With