# exp2() function in C++ STL

The exp2() is a builtin function in C++ STL that computes the base-2 exponential function of a given number. It can also be written as 2num.

Syntax:

`exp2(data_type num)`

Parameter:The function accepts a single mandatory parameter num which specifies the value of the exponent. It can be positive, negative or 0. The parameter can be of type double, float or long double.

Return Value: It returns either a double, float or long double value which is equivalent to 2num.

Program 1:

 `// C++ program to illustrate the ` `// exp2() function for negative double numbers ` `#include ` `#include ` ` `  `using` `namespace` `std; ` ` `  `int` `main() ` `{ ` `    ``double` `n = -3.14; ` ` `  `    ``double` `ans = exp2(n); ` `    ``cout << ``"exp2(-3.14) = "` `<< ans << endl; ` ` `  `    ``return` `0; ` `} `

Output:

```exp2(-3.14) = 0.11344
```

Program 2:

 `// C++ program to illustrate the ` `// exp2() function for positive numbers ` `#include ` `#include ` ` `  `using` `namespace` `std; ` ` `  `int` `main() ` `{ ` `    ``int` `n = 6; ` ` `  `    ``int` `ans = exp2(n); ` `    ``cout << ``"exp2(6) = "` `<< ans << endl; ` ` `  `    ``return` `0; ` `} `

Output:

```exp2(6) = 64
```

Program 3:

 `// C++ program to illustrate the ` `// exp2() function for 0 ` `#include ` `#include ` ` `  `using` `namespace` `std; ` ` `  `int` `main() ` `{ ` `    ``int` `n = 0; ` ` `  `    ``int` `ans = exp2(n); ` `    ``cout << ``"exp2(0) = "` `<< ans << endl; ` ` `  `    ``return` `0; ` `} `

Output:

```exp2(0) = 1
```

Errors and Execptions: If the magnitude of the result is too large to be represented by a value of the return type, the function returns HUGE_VAL (or HUGE_VALF or HUGE_VALL) with the proper sign, and an overflow range error occurs.

Below program illustrate the error.

 `// C++ program to illustrate the ` `// exp2() function for range overflow ` `#include ` `#include ` ` `  `using` `namespace` `std; ` ` `  `int` `main() ` `{ ` `    ``// overflow will occur as 2^100 will not ` `    ``// fit to int data-type ` `    ``int` `n = 100; ` ` `  `    ``int` `ans = exp2(n); ` `    ``cout << ``"exp2(100) = "` `<< ans << endl; ` ` `  `    ``return` `0; ` `} `

Output:

```exp2(100) = -2147483648
```

My Personal Notes arrow_drop_up

Budding Web DeveloperKeen learnerAverage CoderDancer&Social Activist

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.