将浮点数乘以二的整数幂。
语法
double ldexp(
double x,
int exp
);
float ldexpf(
float x,
int exp
);
long double ldexpl(
long double x,
int exp
);
#define ldexp(X, INT) // Requires C11 or later
float ldexp(
float x,
int exp
); // C++ only
long double ldexp(
long double x,
int exp
); // C++ only
参数
x
浮点值。
exp
整数指数。
返回值
如果成功,ldexp 函数将返回 x * 2exp 的值。 溢出时,根据 x 的符号,ldexp 将返回 +/– HUGE_VAL;将 errno 值设置为 ERANGE。
有关 errno 和可能的错误返回值的详细信息,请参阅errno、 _doserrno、 _sys_errlist和_sys_nerr。
备注
由于 C++ 允许重载,因此你可以调用采用 ldexp 或 float 类型的 long double 重载。 在 C 程序中,除非使用 <tgmath.h> 宏来调用此函数,否则 ldexp 始终采用 double 和 int 并返回 double。
如果使用 <tgmath.h>ldexp() 宏,则参数的类型将决定选择哪个版本的函数。 有关详细信息,请参阅泛型类型数学。
默认情况下,此函数的全局状态范围限定为应用程序。 若要更改此行为,请参阅 CRT 中的全局状态。
要求
| 例程 | C 标头 | C++ 标头 |
|---|---|---|
| .- . | <math.h> | <cmath> |
ldexp 宏 |
<tgmath.h> |
有关兼容性信息,请参阅兼容性。
示例
// crt_ldexp.c
#include <math.h>
#include <stdio.h>
int main( void )
{
double x = 4.0, y;
int p = 3;
y = ldexp( x, p );
printf( "%2.1f times two to the power of %d is %2.1f\n", x, p, y );
}
输出
4.0 times two to the power of 3 is 32.0