exp2()

Anonymous contributor's avatar
Anonymous contributor
Anonymous contributor's avatar
Anonymous contributor
Published Nov 21, 2022Updated Dec 21, 2022
Contribute to Docs

The exp2() function returns the base-2 exponential function of a given number x, or 2 raised to the power of x (i.e., 2x).

Syntax

exp2(x)

The exp2() function requires a single x parameter that can be positive, negative, or zero. The returned value is in the range of [0, ∞).

  • If a range error due to overflow occurs, HUGE_VAL, HUGE_VALF, or HUGE_VALL is returned.
  • If a range error occurs due to underflow, the correct result (after rounding) is returned.
  • If the argument is ±0, 1 is returned.
  • If the argument is -∞, +0 is returned.
  • If the argument is +∞, +∞ is returned.
  • If the argument is NaN, NaN is returned.
  • The return type for the exp2() function is double, float, or long double.

Note: The <cmath> header provides additional overloads for other combinations of arithmetic types (double, float, or long double). Overloaded functions cast the arguments to a double type before the calculation.

Example

The following example uses exp2() to return 2 raised to the given argument:

#include <iostream>
#include <cmath>
using namespace std;
int main() {
double x = -6.19, result;
result = exp2(x);
cout << "exp2(x) = " << result << endl;
return 0;
}

This will return the following output:

exp2(x) = 0.013697

Codebyte Example

The following example is runnable and returns 2 raised to the given argument:

us
Visit us
code
Hide code
Code
Output
Hide output
Hide output
Loading...

All contributors

Looking to contribute?

Learn C++ on Codecademy