24

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?

Bill the Lizard
  • 369,957
  • 201
  • 546
  • 842
James Hayek
  • 573
  • 2
  • 6
  • 23
  • I implemented exponentiation in a limited way (no fractions in the exponent) for a widely used and certified COBOL compiler. Nobody ever noticed the limitation: at least, nobody ever reported it as a bug. If they had, I would have fixed it, with logarithms and all that. – user207421 Jan 27 '14 at 06:21

5 Answers5

56

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

Community
  • 1
  • 1
paxdiablo
  • 772,407
  • 210
  • 1,477
  • 1,841
11

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 susbtituting 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
PixelsTech
  • 2,736
  • 1
  • 29
  • 29
  • 1
    An exponentiation operator would allow the such computations to be included within compile-time-constant expressions. Such operations aren't used a huge amount, but there are certainly times when they can be helpful. – supercat Feb 26 '15 at 00:58
  • 6
    No exponent operator because "Every user of C++ must learn this new feature". So making them learn dozens of super convoluted, subtle, and arcane semantics rules in one of the most overly complex languages ever created is totally cool, but **this** is just too much. – RBF06 Feb 25 '19 at 19:20
11

Most C operations readily intended to mapped to a single processor instruction when C was invented. At the time, exponentiation was not a machine instruction, thus the library routine.

msw
  • 40,500
  • 8
  • 77
  • 106
  • 1
    "Intended" may be too strong. C *did* map fairly directly onto the instruction sets of common architectures of the era, but I've never read of K *or* R saying "we can't have that feature, it's not something the machine does...". – dmckee --- ex-moderator kitten Sep 23 '10 at 03:37
  • 3
    In fact, float-to-int casts and back are implicit, i.e. don't require an operation, but they were far more expensive than a single processor instruction. – MSalters Sep 23 '10 at 07:49
  • @MSalters: Float-to-int coercion is nasty; I think Pascal handles such concepts much more nicely (using separate `Round` and `Trunc` functions for the cases where one wants a rounded or truncated result). – supercat Feb 26 '15 at 00:59
1

Python has the ** operator for exponentiation. In C++ you can actually define an operator like that with some trickery. By combining the unary * operator with the binary * operator, like this:

#include <cmath>
#include <iostream>

struct Num {
    double value;

    Num(double value) : value(value) { }

    typedef Num* HalfStar;

    HalfStar operator*() const { return HalfStar(this); }
    Num operator*(const HalfStar& rhs) const
    {
        return Num(std::pow(value, rhs->value));
    }
    Num operator*(const Num& rhs) const
    {
        return Num(value * rhs.value);
    }

    friend std::ostream& operator<<(std::ostream& os, const Num& n)
    {
        return os << n.value;
    }
};

int main(int argc, char**argv)
{
    Num a = 10;
    Num b = 9;

    std::cout << "a*b = " << (a*b) << "\n";
    std::cout << "a**b = " << (a**b) << "\n";

    return 0;
}

This will not work with non-class types though, so you can not do: x**2.

See here for a live example.

Willem Hengeveld
  • 2,569
  • 19
  • 19
-2

What platform and which compiler are you using? My guess is TurboC. Mostly cmath file has most of the mathematical functions covered in other compilers.

Manoj R
  • 3,073
  • 1
  • 19
  • 32