CKTIS / C
exp2 — C23 mathematical function
`exp2(x)` from `<math.h>` computes `2^x` and returns it as `double`; `exp2f` and `exp2l` accept `float` and `long double`, respectively. C23 reports a range error when the magnitude of finite `x` is too large.
Version: C23
Syntax
double exp2(double x);exp2(x) from <math.h> computes 2^x and returns it as double; exp2f and exp2l accept float and long double, respectively. C23 reports a range error when the magnitude of finite x is too large.
The example calls exp2 with exponents -3.0, 0.0, and 5.0. Output 0.125 1 32 demonstrates a fractional power of two, the neutral point, and a positive power without replacing the function with a bit shift.
Large positive x can overflow and large negative x can underflow; handle the signals selected by math_errhandling. 1 << n is not a substitute for a negative, fractional, or overly large exponent. Under Annex F, exp2(±0)=1, exp2(-∞)=+0, and exp2(+∞)=+∞.
Examples
#include <math.h>
#include <stdio.h>
int main(void) {
printf("%.3f %.0f %.0f\n", exp2(-3.0), exp2(0.0), exp2(5.0));
return 0;
}
Result
0.125 1 32
The example calls `exp2` with exponents `-3.0`, `0.0`, and `5.0`. Output `0.125 1 32` demonstrates a fractional power of two, the neutral point, and a positive power without replacing the function with a bit shift.