Exponential Operator in C++

James Hayek picture James Hayek · Sep 23, 2010 · Viewed 41.9k times · Source

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?

Answer

paxdiablo picture paxdiablo · Sep 23, 2010

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