C++ Standard Library C++ STL Library

C++ <cmath> - exp2() Function



The C++ <cmath> exp2() function returns 2 raised to the power of specified number, i.e., 2x.

Syntax

double exp2 (double x);
float exp2 (float x);
long double exp2 (long double x);
double exp2 (T x);                              

Parameters

x Specify the exponent of 2.

Return Value

Returns 2 raised to the power of specified number, i.e., 2x.

Example:

In the example below, exp2() function is used to calculate 2 raised to the power of specified number.

#include <iostream>
#include <cmath>
using namespace std;
 
int main (){
  cout<<exp2(2)<<"\n";
  cout<<exp2(-2)<<"\n";
  cout<<exp2(1.5)<<"\n";
  cout<<exp2(-1.5)<<"\n";  
  return 0;
}

The output of the above code will be:

4
0.25
2.82843
0.353553

❮ C++ <cmath> Library