Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

ldexp — C23 mathematical function

`ldexp(x, exp)` from `<math.h>` returns `x * 2^exp`; `ldexpf` and `ldexpl` serve `float` and `long double`. It therefore scales by an integral power of two regardless of the value of `FLT_RADIX`.

Version: C23

Syntax

double ldexp(double x, int exp);

ldexp(x, exp) from <math.h> returns x * 2^exp; ldexpf and ldexpl serve float and long double. It therefore scales by an integral power of two regardless of the value of FLT_RADIX.

The example calls ldexp(0.75, 3) and ldexp(-0.5, 2). Output 6.0 -2.0 demonstrates scaling by 2^3 and 2^2 without constructing the powers manually.

If a finite result overflows or underflows, a range error occurs according to math_errhandling; do not assume that errno alone is always sufficient. Annex F preserves the sign of zero, returns infinity for infinite x, and propagates NaN. The exp argument is an int, so conversion from a wider exponent also needs a prior range check.

Examples

#include <math.h>
#include <stdio.h>

int main(void) {
    printf("%.1f %.1f\n", ldexp(0.75, 3), ldexp(-0.5, 2));
    return 0;
}

Result

6.0 -2.0

The example calls `ldexp(0.75, 3)` and `ldexp(-0.5, 2)`. Output `6.0 -2.0` demonstrates scaling by `2^3` and `2^2` without constructing the powers manually.

Sources