C++ exp2()

The exp2() function in C++ returns the base-2 exponential function, i.e. 2 raised to the given argument.

The function is defined in <cmath> header file.

[Mathematics] 2x = exp2(x) [C++ Programming]

exp2() prototype [As of C++ 11 standard]

double exp2(double x);
float exp2(float x);
long double exp2(long double x);
double exp2(T x); // For integral type

exp2() Parameters

The exp2() function takes a single mandatory argument (can be positive, negative or 0).


exp2() Return value

The exp2() function returns the value in the range of [0, ∞].

If the magnitude of the result is too large to be represented by a value of the return type, the function returns HUGE_VAL with the proper sign, and an overflow range error occurs.


Example 1: How exp2() function works in C++?

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
	double x = -6.19, result;
	
	result = exp2(x);
	cout << "exp2(x) = " << result << endl;

	return 0;
}

When you run the program, the output will be:

exp2(x) = 0.013697

Example 2: exp2() function with integral type

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
	long int x = 14;
	double result;
	
	result = exp2(x);
	cout << "exp2(x) = " << result << endl;

	return 0;
}

When you run the program, the output will be:

exp2(x) = 16384