Skip to main content

C++ Exponents

According to mathematics exponent is a number indicates how many times to multiply the base number. When it comes to C++ exponents every programmer should know two std functions, std::pow and std::exp. To use these methods you should include cmath.

Below basic program is to illustrate the mathematics behind exponents.


std::pow


This method can be used to get the power of any number. As you can see in the example this method accepts two arguments. Simply the base and exponent. You can refer this link to read more information about std::pow.

9^3 in mathematics = std::pow(9, 3) in c++

std::exp


This method can only be used to get power of base-e. You only have to pass the exponent (power) as the argument. You can refer this link to read more information about std::pow.

e^3 in mathematics = std::exp(3) in c++

Note: You can include math.h as well instead of cmath. When you include math.h you pow and exp methods are not in std namespace. So you should not use std:: if you include math.h. I will discuss more on that in a separate article. For now I recommend to include cmath if you are using C++11 or above. For previous versions you should include math.h.