summaryrefslogtreecommitdiff
path: root/devdocs/c/numeric%2Fmath%2Flogb.html
blob: 79b5b473fe5f4cb2464ce1c7a6c0a79271568bd3 (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
    <h1 id="firstHeading" class="firstHeading">logb, logbf, logbl</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">float       logbf( 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">double      logb( 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">long double logbl( 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 logb( arg )</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> Extracts the value of the unbiased radix-independent exponent from the floating-point argument <code>arg</code>, and returns it as a floating-point 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>logbl</code> is called. Otherwise, if <code>arg</code> has integer type or the type <code>double</code>, <code>logb</code> is called. Otherwise, <code>logbf</code> is called.</div> <p>Formally, the unbiased exponent is the signed integral part of log<sub class="t-su t-su-b">r</sub>|arg| (returned by this function as a floating-point value), for non-zero arg, where <code>r</code> is <code><a href="../../types/limits" title="c/types/limits">FLT_RADIX</a></code>. If <code>arg</code> is subnormal, it is treated as though it was normalized.</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 floating-point value.</p>
<p>If a domain error occurs, an implementation-defined value is returned</p>
<p>If a pole error 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>
<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>Domain or range error may occur if <code>arg</code> is zero.</p>
<p>If the implementation supports IEEE floating-point arithmetic (IEC 60559),</p>
<ul>
<li> If <code>arg</code> is ±0, -∞ is returned and <code><a href="../fenv/fe_exceptions" title="c/numeric/fenv/FE exceptions">FE_DIVBYZERO</a></code> is raised. </li>
<li> If <code>arg</code> is ±∞, +∞ is returned </li>
<li> If <code>arg</code> is NaN, NaN is returned. </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><a rel="nofollow" class="external text" href="http://pubs.opengroup.org/onlinepubs/9699919799/functions/logb.html">POSIX requires</a> that a pole error occurs if <code>arg</code> is ±0.</p>
<p>The value of the exponent returned by <code>logb</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>logb</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">
<p>Compares different floating-point decomposition functions.</p>
<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 = logb(f);
    printf("logb()/logb() make %f * %d^%d\n", f/scalbn(1.0, i), FLT_RADIX, i);
 
    // error handling
    feclearexcept(FE_ALL_EXCEPT);
    printf("logb(0) = %f\n", logb(0));
    if(fetestexcept(FE_DIVBYZERO)) puts("    FE_DIVBYZERO 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()/logb() make 1.928906 * 2^6
logb(0) = -Inf
    FE_DIVBYZERO raised</pre></div> </div> <h3 id="References"> References</h3>  <ul>
<li> C17 standard (ISO/IEC 9899:2018): </li>
<ul>
<li> 7.12.6.11 The logb functions (p: 179-180) </li>
<li> 7.25 Type-generic math &lt;tgmath.h&gt; (p: 373-375) </li>
<li> F.10.3.11 The logb functions (p: 381) </li>
</ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul>
<li> 7.12.6.11 The logb functions (p: 246) </li>
<li> 7.25 Type-generic math &lt;tgmath.h&gt; (p: 373-375) </li>
<li> F.10.3.11 The logb functions (p: 522) </li>
</ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul>
<li> 7.12.6.11 The logb functions (p: 227) </li>
<li> 7.22 Type-generic math &lt;tgmath.h&gt; (p: 335-337) </li>
<li> F.9.3.11 The logb functions (p: 459) </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="ilogb" title="c/numeric/math/ilogb"> <span class="t-lines"><span>ilogb</span><span>ilogbf</span><span>ilogbl</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/logb" title="cpp/numeric/math/logb">C++ documentation</a></span> for <code>logb</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/logb" class="_attribution-link">https://en.cppreference.com/w/c/numeric/math/logb</a>
  </p>
</div>