Updated April 15, 2023
Introduction to C++ pow
The C++ pow is defined as the mathematical function pow(), which has calculated the base number will be raised it to the exponent number powers the <cmath> header file have these function the base number will be any type of the numbers. It supports both positive and negative finite integers. Still, the exponent of the power is finite means. Still, it does not support an integer type of values because sometimes it throws a domain error the function it may also cause several range errors. It returns the values that will be either large or small numbers; it depends upon the library implementations.
Syntax
The pow() function also computes the base numbers always will be raised to the exponent numbers powers. Always this is one of the mathematical component functions, and these library functions will come from the <cmath> header files; and these functions will take the different types of parameters it includes the base values and exponent.
#include<iostream>
#include<cmath>
using namespace std
data type main()
{
data type basevalue, exponent, output;
basevalue=number;
expoenent=number;
output=pow(basevalue,exponent);
---some logics---
}
The above codes are the basic syntax to calculate the pow() function in the application logic based on the requirements; it calculates the power as well as the data type number values also applicable for calculating the function values.
How does the pow function work in C++?
We used different types of mathematical functions in C++ language among these pow() function computes the base values, and the exponent is calculated with the number ranges. When we use base value has a finite range that is the negative value at the same time exponent also calculates the finite set of ranges, but it does not support an integer type of values it causes and throws a domain type of errors.
If both base and exponent values are zero, it causes a domain type of error around certain types of implementations. It may vary about the exponent range of values if the base value is zero and the exponent number range is negative means it may also cause a domain type of errors and the error is like a pole type of errors. The errors also throw, and it comes with the library implementations because it only identifies and validates the numbers from both the base values and the exponent components.
The C++ languages will use some other mathematical functions parallely like std::pow,std::powf,std::powl; these are some standard power-based functions in the library. We can use other data types like float, double, long double, etc. these data type values are computed the base value ranges, and the power is raised using with the exp and iexp while the two type of arguments in the pow() like base and exponent both are passed with the same data type values additionally we can also add the iexp is the integer exponent values.
If sometimes no errors occurred means the base value will automatically be raised to the power of exp like iexp and base with the power of exponent is returned, and the same domain has occurred if the implementation based value is returned; that is, NaN type of values are supported and returned in the functions. If the pole type of errors or some number range errors will be occurred due to the number overflow occurs in the value ranges that can be returned in both types of parameters that parameter number ranges occurred due to the underflow conditions in some situations, the output of the result will always be returned with some number ranges. We can return the errors using the error handling mechanism that will be reported with some specific math_errhandling library if the base value is finite. It will be the positive and negative exp for both domain errors.
Examples of C++ pow
Given below are the examples of C++ pow:
Example #1
Code:
#include <iostream>
#include <thread>
#include <chrono>
#include <cmath>
using namespace std;
void first() {
std::this_thread::sleep_for(std::chrono::seconds(2));
}
void second() {
std::this_thread::sleep_for(std::chrono::seconds(4));
}
int main ()
{
long double bse = 45.3, output;
int expn = -7;
output = pow(bse, expn);
cout << bse << "^" << expn << " = " << output << endl;
int b = -7, ex = 3;
double rslt;
rslt = pow(b, ex);
cout << b << "^" << ex << " = " << rslt;
std::cout << "Welcome To My Domain Starting the first thread.\n";
std::thread example(first);
std::cout << "Welcome To My Domain Starting the second thread...\n";
std::thread example1(second);
std::cout << "Thanks users we will waiting for the threads completion..." << std::endl;
example.join();
example1.join();
std::cout << "Thread completion is over !\n";
return 0;
}
Output:
Example #2
Code:
#include <iostream>
#include <thread>
#include <chrono>
#include <cmath>
#include <cerrno>
#include <cfenv>
#include <cstring>
#pragma STDC FENV_ACCESS ON
using namespace std;
void first() {
std::this_thread::sleep_for(std::chrono::seconds(2));
}
void second() {
std::this_thread::sleep_for(std::chrono::seconds(4));
}
int main ()
{
std::cout << "pow(3, 9) = " << std::pow(3,9) << '\n'
<< "pow(4, 1.7) = " << std::pow(4,1.7) << '\n'
<< "pow(-5, -7) = " << std::pow(-5,-7) << '\n';
std::cout << "pow(-2, NAN) = " << std::pow(-2,NAN) << '\n'
<< "pow(+2, NAN) = " << std::pow(+2,NAN) << '\n'
<< "pow(INFINITY, 3) = " << std::pow(INFINITY, 3) << '\n'
<< "pow(INFINITY, -5) = " << std::pow(INFINITY, -5) << '\n';
errno = 1;
std::feclearexcept(FE_ALL_EXCEPT);
std::cout << "pow(-2, 2/5) = " << std::pow(-2, 1.7/5) << '\n';
if (errno == EDOM)
std::cout << " errno == errors " << std::strerror(errno) << '\n';
if (std::fetestexcept(FE_INVALID))
std::cout << " Invalid power is raised\n";
std::feclearexcept(FE_ALL_EXCEPT);
std::cout << "pow(-3, -7) = " << std::pow(-0.3, -7) << '\n';
if (std::fetestexcept(FE_DIVBYZERO))
std::cout << " The number is divisible by zero\n";
std::cout << "Welcome To My Domain Starting the first thread.\n";
std::thread example(first);
std::cout << "Welcome To My Domain Starting the second thread...\n";
std::thread example1(second);
std::cout << "Thanks users we will waiting for the threads completion..." << std::endl;
example.join();
example1.join();
std::cout << "Thread completion is over !\n";
return 0;
}
Output:
Example #3
Code:
#include <stdio.h>
#include <math.h>
int main ()
{
printf ("9 ^ 5 = %f\n", pow (9.0, 5.0) );
printf ("3.17 ^ 13 = %f\n", pow (3.17, 13.0) );
printf ("37.09 ^ 2.37= %f\n", pow (37.09, 2.37) );
printf ("38.09 ^ 2.37= %f\n", pow (38.09, 2.37) );
printf ("39.09 ^ 2.37= %f\n", pow (39.09, 2.37) );
printf ("40.09 ^ 2.37= %f\n", pow (40.09, 2.37) );
return 1;
}
Output:
Conclusion
In C++, the mathematical power is calculated using the pow(), and this function has supported all types of data type values, and it covers both an integer, float, and other decimal supported values. The values are covered and calculated using the base number and exponent numbers; these values will return the data type integers.
Recommended Articles
This is a guide to C++ pow. Here we discuss How does pow function work in C++ and Examples along with the codes and outputs. You may also have a look at the following articles to learn more –