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
|
<h1 id="firstHeading" class="firstHeading">log1p, log1pf, log1pl</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 log1pf( 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 log1p( 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 log1pl( 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 log1p( 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 natural (base <code>e</code>) logarithm of <code>1+arg</code>. This function is more precise than the expression <code><a href="http://en.cppreference.com/w/c/numeric/math/log"><span class="kw660">log</span></a><span class="br0">(</span><span class="nu0">1</span><span class="sy2">+</span>arg<span class="br0">)</span></code> if <code>arg</code> is close to zero.</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>log1pl</code> is called. Otherwise, if <code>arg</code> has integer type or the type <code>double</code>, <code>log1p</code> is called. Otherwise, <code>log1pf</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 ln(1+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>-HUGE_VAL</code>, <code>-HUGE_VALF</code>, or <code>-HUGE_VALL</code> is returned.</p>
<p>If a range error occurs due to underflow, 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">math_errhandling</a>.</p>
<p>Domain error occurs if <code>arg</code> is less than -1.</p>
<p>Pole error may occur if <code>arg</code> is -1.</p>
<p>If the implementation supports IEEE floating-point arithmetic (IEC 60559),</p>
<ul>
<li> If the argument is ±0, it is returned unmodified </li>
<li> If the argument is -1, -∞ 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 less than -1, 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>The functions <code><a href="expm1" title="c/numeric/math/expm1">expm1</a></code> and <code>log1p</code> are useful for financial calculations, for example, when calculating small daily interest rates: (1+x)<sup class="t-su">n</sup>-1 can be expressed as <code><a href="http://en.cppreference.com/w/c/numeric/math/expm1"><span class="kw659">expm1</span></a><span class="br0">(</span>n <span class="sy2">*</span> log1p<span class="br0">(</span>x<span class="br0">)</span><span class="br0">)</span></code>. These functions also simplify writing accurate inverse hyperbolic functions.</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("log1p(0) = %f\n", log1p(0));
printf("Interest earned in 2 days on $100, compounded daily at 1%%\n"
" on a 30/360 calendar = %f\n",
100*expm1(2*log1p(0.01/360)));
printf("log(1+1e-16) = %g, but log1p(1e-16) = %g\n",
log(1+1e-16), log1p(1e-16));
// special values
printf("log1p(-0) = %f\n", log1p(-0.0));
printf("log1p(+Inf) = %f\n", log1p(INFINITY));
//error handling
errno = 0; feclearexcept(FE_ALL_EXCEPT);
printf("log1p(-1) = %f\n", log1p(-1));
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">log1p(0) = 0.000000
Interest earned in 2 days on $100, compounded daily at 1%
on a 30/360 calendar = 0.005556
log(1+1e-16) = 0, but log1p(1e-16) = 1e-16
log1p(-0) = -0.000000
log1p(+Inf) = Inf
log1p(-1) = -Inf
errno == ERANGE: Result too large
FE_DIVBYZERO raised</pre></div> </div> <h3 id="References"> References</h3> <ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul>
<li> 7.12.6.9 The log1p functions (p: 245) </li>
<li> 7.25 Type-generic math <tgmath.h> (p: 373-375) </li>
<li> F.10.3.9 The log1p functions (p: 522) </li>
</ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul>
<li> 7.12.6.9 The log1p functions (p: 226) </li>
<li> 7.22 Type-generic math <tgmath.h> (p: 335-337) </li>
<li> F.9.3.9 The log1p 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="log2" title="c/numeric/math/log2"> <span class="t-lines"><span>log2</span><span>log2f</span><span>log2l</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 base-2 logarithm (\({\small \log_{2}{x} }\)log<sub>2</sub>(x)) <br> <span class="t-mark">(function)</span> </td>
</tr> <tr class="t-dsc"> <td> <div><a href="expm1" title="c/numeric/math/expm1"> <span class="t-lines"><span>expm1</span><span>expm1f</span><span>expm1l</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>e</i> raised to the given power, minus one (\({\small e^x-1}\)e<sup>x</sup>-1) <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/log1p" title="cpp/numeric/math/log1p">C++ documentation</a></span> for <code>log1p</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/log1p" class="_attribution-link">https://en.cppreference.com/w/c/numeric/math/log1p</a>
</p>
</div>
|