1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
<h1 id="firstHeading" class="firstHeading">ldexp, ldexpf, ldexpl</h1> <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code><math.h></code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl t-since-c99"> <td> <pre data-language="c">float ldexpf( float arg, int exp );</pre>
</td> <td> (1) </td> <td> <span class="t-mark-rev t-since-c99">(since C99)</span> </td> </tr> <tr class="t-dcl"> <td> <pre data-language="c">double ldexp( double arg, int exp );</pre>
</td> <td> (2) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl t-since-c99"> <td> <pre data-language="c">long double ldexpl( long double arg, int exp );</pre>
</td> <td> (3) </td> <td> <span class="t-mark-rev t-since-c99">(since C99)</span> </td> </tr> <tr class="t-dsc-header"> <th> Defined in header <code><tgmath.h></code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl t-since-c99"> <td> <pre data-language="c">#define ldexp( arg, exp )</pre>
</td> <td> (4) </td> <td> <span class="t-mark-rev t-since-c99">(since C99)</span> </td> </tr> </table> <div class="t-li1">
<span class="t-li">1-3)</span> Multiplies a floating point value <code>arg</code> by the number 2 raised to the <code>exp</code> power.</div> <div class="t-li1">
<span class="t-li">4)</span> Type-generic macro: If <code>arg</code> has type <code>long double</code>, <code>ldexpl</code> is called. Otherwise, if <code>arg</code> has integer type or the type <code>double</code>, <code>ldexp</code> is called. Otherwise, <code>ldexpf</code> is called, respectively.</div> <h3 id="Parameters"> Parameters</h3> <table class="t-par-begin"> <tr class="t-par"> <td> arg </td> <td> - </td> <td> floating point value </td>
</tr> <tr class="t-par"> <td> exp </td> <td> - </td> <td> integer value </td>
</tr>
</table> <h3 id="Return_value"> Return value</h3> <p>If no errors occur, <code>arg</code> multiplied by 2 to the power of <code>exp</code> (arg×2<sup class="t-su">exp</sup>) is returned.</p>
<p>If a range error due to overflow occurs, <code><a href="huge_val" title="c/numeric/math/HUGE VAL">±HUGE_VAL</a></code>, <code>±HUGE_VALF</code>, or <code>±HUGE_VALL</code> is returned.</p>
<p>If a range error due to underflow occurs, the correct result (after rounding) is returned.</p>
<h3 id="Error_handling"> Error handling</h3> <p>Errors are reported as specified in <a href="math_errhandling" title="c/numeric/math/math errhandling"><code>math_errhandling</code></a>.</p>
<p>If the implementation supports IEEE floating-point arithmetic (IEC 60559),</p>
<ul>
<li> Unless a range error occurs, <code><a href="../fenv/fe_exceptions" title="c/numeric/fenv/FE exceptions">FE_INEXACT</a></code> is never raised (the result is exact) </li>
<li> Unless a range error occurs, the <a href="../fenv/fe_round" title="c/numeric/fenv/FE round">current rounding mode</a> is ignored </li>
<li> If <code>arg</code> is ±0, it is returned, unmodified </li>
<li> If <code>arg</code> is ±∞, it is returned, unmodified </li>
<li> If <code>exp</code> is 0, then <code>arg</code> is returned, unmodified </li>
<li> If <code>arg</code> is NaN, NaN is returned </li>
</ul> <h3 id="Notes"> Notes</h3> <p>On binary systems (where <code><a href="../../types/limits" title="c/types/limits">FLT_RADIX</a></code> is <code>2</code>), <code>ldexp</code> is equivalent to <code><a href="scalbn" title="c/numeric/math/scalbn">scalbn</a></code>.</p>
<p>The function <code>ldexp</code> ("load exponent"), together with its dual, <code><a href="frexp" title="c/numeric/math/frexp">frexp</a></code>, can be used to manipulate the representation of a floating-point number without direct bit manipulations.</p>
<p>On many implementations, <code>ldexp</code> is less efficient than multiplication or division by a power of two using arithmetic operators.</p>
<h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include <stdio.h>
#include <math.h>
#include <float.h>
#include <errno.h>
#include <fenv.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 values
printf("ldexp(-0, 10) = %f\n", ldexp(-0.0, 10));
printf("ldexp(-Inf, -1) = %f\n", ldexp(-INFINITY, -1));
//error handling
errno = 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");
}</pre></div> <p>Possible output:</p>
<div class="text source-text"><pre data-language="c">ldexp(7, -4) = 0.437500
ldexp(1, -1074) = 4.94066e-324 (minimum positive subnormal double)
ldexp(nextafter(1,0), 1024) = 1.79769e+308 (largest finite double)
ldexp(-0, 10) = -0.000000
ldexp(-Inf, -1) = -inf
ldexp(1, 1024) = inf
errno == ERANGE: Numerical result out of range
FE_OVERFLOW raised</pre></div> </div> <h3 id="References"> References</h3> <ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul>
<li> 7.12.6.6 The ldexp functions (p: 244) </li>
<li> 7.25 Type-generic math <tgmath.h> (p: 373-375) </li>
<li> F.10.3.6 The ldexp functions (p: 522) </li>
</ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul>
<li> 7.12.6.6 The ldexp functions (p: 225) </li>
<li> 7.22 Type-generic math <tgmath.h> (p: 335-337) </li>
<li> F.9.3.6 The ldexp functions (p: 459) </li>
</ul>
<li> C89/C90 standard (ISO/IEC 9899:1990): </li>
<ul><li> 4.5.4.3 The ldexp function </li></ul>
</ul> <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="frexp" title="c/numeric/math/frexp"> <span class="t-lines"><span>frexp</span><span>frexpf</span><span>frexpl</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-c99">(C99)</span></span><span><span class="t-mark-rev t-since-c99">(C99)</span></span></span></div> </td> <td> breaks a number into significand and a power of <code>2</code> <br> <span class="t-mark">(function)</span> </td>
</tr> <tr class="t-dsc"> <td> <div><a href="scalbn" title="c/numeric/math/scalbn"> <span class="t-lines"><span>scalbn</span><span>scalbnf</span><span>scalbnl</span><span>scalbln</span><span>scalblnf</span><span>scalblnl</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-c99">(C99)</span></span><span><span class="t-mark-rev t-since-c99">(C99)</span></span><span><span class="t-mark-rev t-since-c99">(C99)</span></span><span><span class="t-mark-rev t-since-c99">(C99)</span></span><span><span class="t-mark-rev t-since-c99">(C99)</span></span><span><span class="t-mark-rev t-since-c99">(C99)</span></span></span></div> </td> <td> computes efficiently a number times <code><a href="../../types/limits" title="c/types/limits">FLT_RADIX</a></code> raised to a power <br> <span class="t-mark">(function)</span> </td>
</tr> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/numeric/math/ldexp" title="cpp/numeric/math/ldexp">C++ documentation</a></span> for <code>ldexp</code> </td>
</tr> </table> <div class="_attribution">
<p class="_attribution-p">
© cppreference.com<br>Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.<br>
<a href="https://en.cppreference.com/w/c/numeric/math/ldexp" class="_attribution-link">https://en.cppreference.com/w/c/numeric/math/ldexp</a>
</p>
</div>
|