summaryrefslogtreecommitdiff
path: root/devdocs/c/numeric%2Fmath%2Filogb.html
blob: 75847df0b8d0a21ed0c53bf45f354bd3b5f650e9 (plain)
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
80
81
82
83
84
85
86
87
88
89
90
91
    <h1 id="firstHeading" class="firstHeading">ilogb, ilogbf, ilogbl</h1>            <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code>&lt;math.h&gt;</code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl t-since-c99"> <td> <pre data-language="c">int ilogbf( float arg );</pre>
</td> <td> (1) </td> <td> <span class="t-mark-rev t-since-c99">(since C99)</span> </td> </tr> <tr class="t-dcl t-since-c99"> <td> <pre data-language="c">int ilogb( double arg );</pre>
</td> <td> (2) </td> <td> <span class="t-mark-rev t-since-c99">(since C99)</span> </td> </tr> <tr class="t-dcl t-since-c99"> <td> <pre data-language="c">int ilogbl( long double arg );</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>&lt;tgmath.h&gt;</code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl t-since-c99"> <td> <pre data-language="c">#define ilogb( arg )</pre>
</td> <td> (4) </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>&lt;math.h&gt;</code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl t-since-c99"> <td> <pre data-language="c">#define FP_ILOGB0 /*implementation-defined*/</pre>
</td> <td> (5) </td> <td> <span class="t-mark-rev t-since-c99">(since C99)</span> </td> </tr> <tr class="t-dcl t-since-c99"> <td> <pre data-language="c">#define FP_ILOGBNAN /*implementation-defined*/</pre>
</td> <td> (6) </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> Extracts the value of the unbiased exponent from the floating-point argument <code>arg</code>, and returns it as a signed integer value. </div> <div class="t-li1">
<span class="t-li">4)</span> Type-generic macros: If <code>arg</code> has type <code>long double</code>, <code>ilogbl</code> is called. Otherwise, if <code>arg</code> has integer type or the type <code>double</code>, <code>ilogb</code> is called. Otherwise, <code>ilogbf</code> is called.</div> <div class="t-li1">
<span class="t-li">5)</span> Expands to integer constant expression whose value is either <code><a href="../../types/limits" title="c/types/limits">INT_MIN</a></code> or <code><span class="sy2">-</span><a href="http://en.cppreference.com/w/c/types/limits"><span class="kw364">INT_MAX</span></a></code>.</div> <div class="t-li1">
<span class="t-li">6)</span> Expands to integer constant expression whose value is either <code><a href="../../types/limits" title="c/types/limits">INT_MIN</a></code> or <code><span class="sy2">+</span><a href="http://en.cppreference.com/w/c/types/limits"><span class="kw364">INT_MAX</span></a></code>.</div> <p>Formally, the unbiased exponent is the integral part of log<sub class="t-su t-su-b">r</sub>|arg| as a signed integral value, for non-zero arg, where <code>r</code> is <code><a href="../../types/limits" title="c/types/limits">FLT_RADIX</a></code>.</p>
<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>
</table> <h3 id="Return_value"> Return value</h3> <p>If no errors occur, the unbiased exponent of <code>arg</code> is returned as a signed int value.</p>
<p>If <code>arg</code> is zero, <code>FP_ILOGB0</code> is returned.</p>
<p>If <code>arg</code> is infinite, <code><a href="../../types/limits" title="c/types/limits">INT_MAX</a></code> is returned.</p>
<p>If <code>arg</code> is a NaN, <code>FP_ILOGBNAN</code> is returned.</p>
<p>If the correct result is greater than <code><a href="../../types/limits" title="c/types/limits">INT_MAX</a></code> or smaller than <code><a href="../../types/limits" title="c/types/limits">INT_MIN</a></code>, the return value is unspecified and a domain error or range error may occur</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">math_errhandling</a>.</p>
<p>A domain error or range error may occur if <code>arg</code> is zero, infinite, or NaN.</p>
<p>If the correct result is greater than <code><a href="../../types/limits" title="c/types/limits">INT_MAX</a></code> or smaller than <code><a href="../../types/limits" title="c/types/limits">INT_MIN</a></code>, a domain error or a range error may occur</p>
<p>If the implementation supports IEEE floating-point arithmetic (IEC 60559),</p>
<ul>
<li> If the correct result is greater than <code><a href="../../types/limits" title="c/types/limits">INT_MAX</a></code> or smaller than <code><a href="../../types/limits" title="c/types/limits">INT_MIN</a></code>, <code><a href="../fenv/fe_exceptions" title="c/numeric/fenv/FE exceptions">FE_INVALID</a></code> is raised. </li>
<li> If <code>arg</code> is ±0, ±∞, or NaN, <code><a href="../fenv/fe_exceptions" title="c/numeric/fenv/FE exceptions">FE_INVALID</a></code> is raised. </li>
<li> In all other cases, the result is exact (<code><a href="../fenv/fe_exceptions" title="c/numeric/fenv/FE exceptions">FE_INEXACT</a></code> is never raised) and <a href="../fenv/fe_round" title="c/numeric/fenv/FE round">the current rounding mode</a> is ignored </li>
</ul> <h3 id="Notes"> Notes</h3> <p>If <code>arg</code> is not zero, infinite, or NaN, the value returned is exactly equivalent to <code><span class="br0">(</span><span class="kw4">int</span><span class="br0">)</span><a href="http://en.cppreference.com/w/c/numeric/math/logb"><span class="kw663">logb</span></a><span class="br0">(</span>arg<span class="br0">)</span></code>.</p>
<p><a rel="nofollow" class="external text" href="http://pubs.opengroup.org/onlinepubs/9699919799/functions/ilogb.html">POSIX requires</a> that a domain error occurs if <code>arg</code> is zero, infinite, NaN, or if the correct result is outside of the range of <code>int</code>.</p>
<p>POSIX also requires that, on XSI-conformant systems, the value returned when the correct result is greater than <code><a href="../../types/limits" title="c/types/limits">INT_MAX</a></code> is <code><a href="../../types/limits" title="c/types/limits">INT_MAX</a></code> and the value returned when the correct result is less than <code><a href="../../types/limits" title="c/types/limits">INT_MIN</a></code> is <code><a href="../../types/limits" title="c/types/limits">INT_MIN</a></code>.</p>
<p>The correct result can be represented as <code>int</code> on all known implementations. For overflow to occur, <code><a href="../../types/limits" title="c/types/limits">INT_MAX</a></code> must be less than <code><a href="http://en.cppreference.com/w/c/types/limits"><span class="kw397">LDBL_MAX_EXP</span></a><span class="sy2">*</span>log2<span class="br0">(</span><a href="http://en.cppreference.com/w/c/types/limits"><span class="kw372">FLT_RADIX</span></a><span class="br0">)</span></code> or <code><a href="../../types/limits" title="c/types/limits">INT_MIN</a></code> must be greater than <code>LDBL_MIN_EXP<span class="sy2">-</span><a href="http://en.cppreference.com/w/c/types/limits"><span class="kw388">LDBL_MANT_DIG</span></a><span class="br0">)</span><span class="sy2">*</span>log2<span class="br0">(</span><a href="http://en.cppreference.com/w/c/types/limits"><span class="kw372">FLT_RADIX</span></a><span class="br0">)</span></code>.</p>
<p>The value of the exponent returned by <code>ilogb</code> is always 1 less than the exponent retuned by <code><a href="frexp" title="c/numeric/math/frexp">frexp</a></code> because of the different normalization requirements: for the exponent <code>e</code> returned by <code>ilogb</code>, |arg*r<sup class="t-su">-e</sup>| is between 1 and <code>r</code> (typically between <code>1</code> and <code>2</code>), but for the exponent <code>e</code> returned by <code><a href="frexp" title="c/numeric/math/frexp">frexp</a></code>, |arg*2<sup class="t-su">-e</sup>| is between <code>0.5</code> and <code>1</code>.</p>
<h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include &lt;stdio.h&gt;
#include &lt;math.h&gt;
#include &lt;float.h&gt;
#include &lt;fenv.h&gt;
 
#pragma STDC FENV_ACCESS ON
int main(void)
{
    double f = 123.45;
    printf("Given the number %.2f or %a in hex,\n", f, f);
 
    double f3;
    double f2 = modf(f, &amp;f3);
    printf("modf() makes %.0f + %.2f\n", f3, f2);
 
    int i;
    f2 = frexp(f, &amp;i);
    printf("frexp() makes %f * 2^%d\n", f2, i);
 
    i = ilogb(f);
    printf("logb()/ilogb() make %f * %d^%d\n", f/scalbn(1.0, i), FLT_RADIX, i);
 
    // error handling
    feclearexcept(FE_ALL_EXCEPT);
    printf("ilogb(0) = %d\n", ilogb(0));
    if(fetestexcept(FE_INVALID)) puts("    FE_INVALID raised");
}</pre></div> <p>Possible output:</p>
<div class="text source-text"><pre data-language="c">Given the number 123.45 or 0x1.edccccccccccdp+6 in hex,
modf() makes 123 + 0.45
frexp() makes 0.964453 * 2^7
logb()/ilogb() make 1.92891 * 2^6
ilogb(0) = -2147483648
    FE_INVALID raised</pre></div> </div> <h3 id="References"> References</h3>  <ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul>
<li> 7.12/8 Mathematics &lt;math.h&gt; (p: 232) </li>
<li> 7.12.6.5 The ilogb functions (p: 244) </li>
<li> 7.25 Type-generic math &lt;tgmath.h&gt; (p: 373-375) </li>
<li> F.10.3.5 The ilogb functions (p: 521) </li>
</ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul>
<li> 7.12/8 Mathematics &lt;math.h&gt; (p: 213) </li>
<li> 7.12.6.5 The ilogb functions (p: 224-225) </li>
<li> 7.22 Type-generic math &lt;tgmath.h&gt; (p: 335-337) </li>
<li> F.9.3.5 The ilogb functions (p: 458) </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="logb" title="c/numeric/math/logb"> <span class="t-lines"><span>logb</span><span>logbf</span><span>logbl</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></div> </td> <td> extracts exponent of the given number <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/ilogb" title="cpp/numeric/math/ilogb">C++ documentation</a></span> for <code>ilogb</code> </td>
</tr> </table>           <div class="_attribution">
  <p class="_attribution-p">
    &copy; 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/ilogb" class="_attribution-link">https://en.cppreference.com/w/c/numeric/math/ilogb</a>
  </p>
</div>