How to Use Exponents in C++

By Marty Simmons

C++ is a powerful programming language with a diverse set of standard libraries. Each library provides common and useful functions for many different applications. For game and scientific-related programming, mathematical functions are crucial for certain calculations. The "cmath" standard library contains a diverse set of functions, including exponential functions, that would be difficult to recreate using your own functions. All you need to do is include the "cmath" library in your program and every function becomes available.

Step 1

Include the "cmath" library by adding the line "#include" near the top lines of your program. If you are using other libraries, add the line anywhere in the same list. Includes must be placed before anything else in your code.

Step 2

Declare two variables that will represent the base and power values for your exponent. Although you can put numbers directly into the function parameters, it is generally not a good practice, since you may want to easily change the initial values without having to edit every instance of the power function.

Step 3

Call the power function from the "cmath" library. For example, the following line would call the power function using the variables from the previous step and assign the result to a third variable. The first parameter is the base, while the second is the power that the number contained within the "base" is raised.answer = pow(base, power);If "base" was set to 2 and "power" to 3, the variable "answer" would be equal to 8.