Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does C++ not have an exponentiation operator?

I am taking a class in C++ and I noticed there are only a few math operators to use. I also noticed that C++ does not come with an exponential operator within its math library.

Why must one always write a function for this? Is there a reason for the makers of C++ to omit this operator?

like image 784
James Hayek Avatar asked Sep 05 '25 16:09

James Hayek


2 Answers

You don't write a function for this (unless you're insane, of course). There's a perfectly good pow function defined in the <cmath> header.

Aside: if you try to use ^ as a power operator, as some people are wont to do, you'll be in for a nasty surprise. It's the exclusive-or (XOR) operator (see here).

like image 76
paxdiablo Avatar answered Sep 07 '25 08:09

paxdiablo


According to Bjarne Stroustrup in his book The design and evolution of C++. They decided to avoid exponential operator because :

  • An operator provides notational convenience, but does not provide any new functionality. Members of the working group, representing heavy users of scientific/engineering computation, indicated that the operator syntax provides minor syntactic convenience.
  • Every user of C++ must learn this new feature
  • Users have stressed the importance of substituting their own specialized exponentiation functions for the system default, which would not be possible with an intrinsic operator
  • The proposal is not sufficiently well motivated. In particular, by looking at one 30000 line Fortran program one cannot conclude that the operator would be widely used in C++
  • The proposal requires adding a new operator and adding another precedence level thus increasing the complexity of the language
like image 27
PixelsTech Avatar answered Sep 07 '25 08:09

PixelsTech