|
|
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Defined in header <math.h> | ||
float ldexpf(float arg,intexp); | (1) | (since C99) |
double ldexp(double arg,intexp); | (2) | |
longdouble ldexpl(longdouble arg,intexp); | (3) | (since C99) |
Defined in header <tgmath.h> | ||
#define ldexp( arg, exp ) | (4) | (since C99) |
ldexpl is called. Otherwise, ifarg has integer type or the typedouble,ldexp is called. Otherwise,ldexpf is called, respectively.Contents |
| arg | - | floating-point value |
| exp | - | integer value |
If no errors occur,arg multiplied by 2 to the power ofexp (arg×2exp
) is returned.
If a range error due to overflow occurs,±HUGE_VAL,±HUGE_VALF, or±HUGE_VALL is returned.
If a range error due to underflow occurs, the correct result (after rounding) is returned.
Errors are reported as specified inmath_errhandling.
If the implementation supports IEEE floating-point arithmetic (IEC 60559),
On binary systems (whereFLT_RADIX is2),ldexp is equivalent toscalbn.
The functionldexp ("load exponent"), together with its dual,frexp, can be used to manipulate the representation of a floating-point number without direct bit manipulations.
On many implementations,ldexp is less efficient than multiplication or division by a power of two using arithmetic operators.
#include <errno.h>#include <fenv.h>#include <float.h>#include <math.h>#include <stdio.h>// #pragma STDC FENV_ACCESS ON int main(void){printf("ldexp(7, -4) = %f\n", ldexp(7,-4));printf("ldexp(1, -1074) = %g (minimum positive subnormal double)\n", ldexp(1,-1074));printf("ldexp(nextafter(1,0), 1024) = %g (largest finite double)\n", ldexp(nextafter(1,0),1024)); // special valuesprintf("ldexp(-0, 10) = %f\n", ldexp(-0.0,10));printf("ldexp(-Inf, -1) = %f\n", ldexp(-INFINITY,-1)); // error handlingerrno=0;feclearexcept(FE_ALL_EXCEPT);printf("ldexp(1, 1024) = %f\n", ldexp(1,1024));if(errno==ERANGE)perror(" errno == ERANGE");if(fetestexcept(FE_OVERFLOW))puts(" FE_OVERFLOW raised");}
Possible output:
ldexp(7, -4) = 0.437500ldexp(1, -1074) = 4.94066e-324 (minimum positive subnormal double)ldexp(nextafter(1,0), 1024) = 1.79769e+308 (largest finite double)ldexp(-0, 10) = -0.000000ldexp(-Inf, -1) = -infldexp(1, 1024) = inf errno == ERANGE: Numerical result out of range FE_OVERFLOW raised
(C99)(C99) | breaks a number into significand and a power of2 (function)[edit] |
(C99)(C99)(C99)(C99)(C99)(C99) | computes efficiently a number timesFLT_RADIX raised to a power (function)[edit] |
C++ documentation forldexp | |