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
|
<h1 id="firstHeading" class="firstHeading">log2, log2f, log2l</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 log2f( 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 log2( 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 log2l( 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><tgmath.h></code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl t-since-c99"> <td> <pre data-language="c">#define log2( 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> Computes the base <code>2</code> logarithm of <code>arg</code>.</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>log2l</code> is called. Otherwise, if <code>arg</code> has integer type or the type <code>double</code>, <code>log2</code> is called. Otherwise, <code>log2f</code> is called.</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>
</table> <h3 id="Return_value"> Return value</h3> <p>If no errors occur, the base-<i>2</i> logarithm of <code>arg</code> (log<sub class="t-su t-su-b">2</sub>(arg) or lb(arg)) is returned.</p>
<p>If a domain error occurs, an implementation-defined value is returned (NaN where supported).</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 error occurs if <code>arg</code> is less than zero.</p>
<p>Pole 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 the argument 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 the argument is 1, +0 is returned </li>
<li> If the argument is negative, NaN is returned and <code><a href="../fenv/fe_exceptions" title="c/numeric/fenv/FE exceptions">FE_INVALID</a></code> is raised. </li>
<li> If the argument is +∞, +∞ is returned </li>
<li> If the argument is NaN, NaN is returned </li>
</ul> <h3 id="Notes"> Notes</h3> <p>For integer <code>arg</code>, the binary logarithm can be interpreted as the zero-based index of the most significant 1 bit in the input.</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("log2(65536) = %f\n", log2(65536));
printf("log2(0.125) = %f\n", log2(0.125));
printf("log2(0x020f) = %f (highest set bit is in position 9)\n", log2(0x020f));
printf("base-5 logarithm of 125 = %f\n", log2(125)/log2(5));
// special values
printf("log2(1) = %f\n", log2(1));
printf("log2(+Inf) = %f\n", log2(INFINITY));
//error handling
errno = 0; feclearexcept(FE_ALL_EXCEPT);
printf("log2(0) = %f\n", log2(0));
if(errno == ERANGE) perror(" errno == ERANGE");
if(fetestexcept(FE_DIVBYZERO)) puts(" FE_DIVBYZERO raised");
}</pre></div> <p>Possible output:</p>
<div class="text source-text"><pre data-language="c">log2(65536) = 16.000000
log2(0.125) = -3.000000
log2(0x020f) = 9.041659 (highest set bit is in position 9)
base-5 logarithm of 125 = 3.000000
log2(1) = 0.000000
log2(+Inf) = inf
log2(0) = -inf
errno == ERANGE: Numerical result out of range
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.10 The log2 functions (p: 179) </li>
<li> 7.25 Type-generic math <tgmath.h> (p: 272-273) </li>
<li> F.10.3.10 The log2 functions (p: 381) </li>
</ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul>
<li> 7.12.6.10 The log2 functions (p: 246) </li>
<li> 7.25 Type-generic math <tgmath.h> (p: 373-375) </li>
<li> F.10.3.10 The log2 functions (p: 522) </li>
</ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul>
<li> 7.12.6.10 The log2 functions (p: 226) </li>
<li> 7.22 Type-generic math <tgmath.h> (p: 335-337) </li>
<li> F.9.3.10 The log2 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="log" title="c/numeric/math/log"> <span class="t-lines"><span>log</span><span>logf</span><span>logl</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> computes natural (base-<i>e</i>) logarithm (\({\small \ln{x} }\)ln(x)) <br> <span class="t-mark">(function)</span> </td>
</tr> <tr class="t-dsc"> <td> <div><a href="log10" title="c/numeric/math/log10"> <span class="t-lines"><span>log10</span><span>log10f</span><span>log10l</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> computes common (base-<i>10</i>) logarithm (\({\small \log_{10}{x} }\)log<sub>10</sub>(x)) <br> <span class="t-mark">(function)</span> </td>
</tr> <tr class="t-dsc"> <td> <div><a href="log1p" title="c/numeric/math/log1p"> <span class="t-lines"><span>log1p</span><span>log1pf</span><span>log1pl</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> computes natural (base-<i>e</i>) logarithm of 1 plus the given number (\({\small \ln{(1+x)} }\)ln(1+x)) <br> <span class="t-mark">(function)</span> </td>
</tr> <tr class="t-dsc"> <td> <div><a href="exp2" title="c/numeric/math/exp2"> <span class="t-lines"><span>exp2</span><span>exp2f</span><span>exp2l</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> computes <i>2</i> raised to the given power (\({\small 2^x}\)2<sup>x</sup>) <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/log2" title="cpp/numeric/math/log2">C++ documentation</a></span> for <code>log2</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/log2" class="_attribution-link">https://en.cppreference.com/w/c/numeric/math/log2</a>
</p>
</div>
|