• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4<title>log1p</title>
5<link rel="stylesheet" href="../../math.css" type="text/css">
6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
7<link rel="home" href="../../index.html" title="Math Toolkit 2.12.0">
8<link rel="up" href="../powers.html" title="Basic Functions">
9<link rel="prev" href="cos_pi.html" title="cos_pi">
10<link rel="next" href="expm1.html" title="expm1">
11</head>
12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
13<table cellpadding="2" width="100%"><tr>
14<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td>
15<td align="center"><a href="../../../../../../index.html">Home</a></td>
16<td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td>
17<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
18<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
19<td align="center"><a href="../../../../../../more/index.htm">More</a></td>
20</tr></table>
21<hr>
22<div class="spirit-nav">
23<a accesskey="p" href="cos_pi.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../powers.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="expm1.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
24</div>
25<div class="section">
26<div class="titlepage"><div><div><h3 class="title">
27<a name="math_toolkit.powers.log1p"></a><a class="link" href="log1p.html" title="log1p">log1p</a>
28</h3></div></div></div>
29<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">math</span><span class="special">/</span><span class="identifier">special_functions</span><span class="special">/</span><span class="identifier">log1p</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
30</pre>
31<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">math</span><span class="special">{</span>
32
33<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">&gt;</span>
34<a class="link" href="../result_type.html" title="Calculation of the Type of the Result"><span class="emphasis"><em>calculated-result-type</em></span></a> <span class="identifier">log1p</span><span class="special">(</span><span class="identifier">T</span> <span class="identifier">x</span><span class="special">);</span>
35
36<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">,</span> <span class="keyword">class</span> <a class="link" href="../../policy.html" title="Chapter 21. Policies: Controlling Precision, Error Handling etc">Policy</a><span class="special">&gt;</span>
37<a class="link" href="../result_type.html" title="Calculation of the Type of the Result"><span class="emphasis"><em>calculated-result-type</em></span></a> <span class="identifier">log1p</span><span class="special">(</span><span class="identifier">T</span> <span class="identifier">x</span><span class="special">,</span> <span class="keyword">const</span> <a class="link" href="../../policy.html" title="Chapter 21. Policies: Controlling Precision, Error Handling etc">Policy</a><span class="special">&amp;);</span>
38
39<span class="special">}}</span> <span class="comment">// namespaces</span>
40</pre>
41<p>
42        Returns the natural logarithm of <span class="emphasis"><em>x+1</em></span>.
43      </p>
44<p>
45        The return type of this function is computed using the <a class="link" href="../result_type.html" title="Calculation of the Type of the Result"><span class="emphasis"><em>result
46        type calculation rules</em></span></a>: the return is <code class="computeroutput"><span class="keyword">double</span></code>
47        when <span class="emphasis"><em>x</em></span> is an integer type and T otherwise.
48      </p>
49<p>
50        The final <a class="link" href="../../policy.html" title="Chapter 21. Policies: Controlling Precision, Error Handling etc">Policy</a> argument is optional and can
51        be used to control the behaviour of the function: how it handles errors,
52        what level of precision to use etc. Refer to the <a class="link" href="../../policy.html" title="Chapter 21. Policies: Controlling Precision, Error Handling etc">policy
53        documentation for more details</a>.
54      </p>
55<p>
56        There are many situations where it is desirable to compute <code class="computeroutput"><span class="identifier">log</span><span class="special">(</span><span class="identifier">x</span><span class="special">+</span><span class="number">1</span><span class="special">)</span></code>.
57        However, for small <span class="emphasis"><em>x</em></span> then <span class="emphasis"><em>x+1</em></span> suffers
58        from catastrophic cancellation errors so that <span class="emphasis"><em>x+1 == 1</em></span>
59        and <span class="emphasis"><em>log(x+1) == 0</em></span>, when in fact for very small x, the
60        best approximation to <span class="emphasis"><em>log(x+1)</em></span> would be <span class="emphasis"><em>x</em></span>.
61        <code class="computeroutput"><span class="identifier">log1p</span></code> calculates the best
62        approximation to <code class="computeroutput"><span class="identifier">log</span><span class="special">(</span><span class="number">1</span><span class="special">+</span><span class="identifier">x</span><span class="special">)</span></code> using a Taylor series expansion for accuracy
63        (less than 2ɛ). Alternatively note that there are faster methods available,
64        for example using the equivalence:
65      </p>
66<div class="blockquote"><blockquote class="blockquote"><p>
67          <span class="emphasis"><em>log(1+x) == (log(1+x) * x) / ((1+x) - 1)</em></span>
68        </p></blockquote></div>
69<p>
70        However, experience has shown that these methods tend to fail quite spectacularly
71        once the compiler's optimizations are turned on, consequently they are used
72        only when known not to break with a particular compiler. In contrast, the
73        series expansion method seems to be reasonably immune to optimizer-induced
74        errors.
75      </p>
76<p>
77        Finally when macro BOOST_HAS_LOG1P is defined then the <code class="computeroutput"><span class="keyword">float</span><span class="special">/</span><span class="keyword">double</span><span class="special">/</span><span class="keyword">long</span> <span class="keyword">double</span></code>
78        specializations of this template simply forward to the platform's native
79        (POSIX) implementation of this function.
80      </p>
81<p>
82        The following graph illustrates the behaviour of log1p:
83      </p>
84<div class="blockquote"><blockquote class="blockquote"><p>
85          <span class="inlinemediaobject"><img src="../../../graphs/log1p.svg" align="middle"></span>
86
87        </p></blockquote></div>
88<h5>
89<a name="math_toolkit.powers.log1p.h0"></a>
90        <span class="phrase"><a name="math_toolkit.powers.log1p.accuracy"></a></span><a class="link" href="log1p.html#math_toolkit.powers.log1p.accuracy">Accuracy</a>
91      </h5>
92<p>
93        For built in floating point types <code class="computeroutput"><span class="identifier">log1p</span></code>
94        should have approximately 1 <a href="http://en.wikipedia.org/wiki/Machine_epsilon" target="_top">machine
95        epsilon</a> accuracy.
96      </p>
97<div class="table">
98<a name="math_toolkit.powers.log1p.table_log1p"></a><p class="title"><b>Table 8.81. Error rates for log1p</b></p>
99<div class="table-contents"><table class="table" summary="Error rates for log1p">
100<colgroup>
101<col>
102<col>
103<col>
104<col>
105<col>
106</colgroup>
107<thead><tr>
108<th>
109              </th>
110<th>
111                <p>
112                  GNU C++ version 7.1.0<br> linux<br> long double
113                </p>
114              </th>
115<th>
116                <p>
117                  GNU C++ version 7.1.0<br> linux<br> double
118                </p>
119              </th>
120<th>
121                <p>
122                  Sun compiler version 0x5150<br> Sun Solaris<br> long double
123                </p>
124              </th>
125<th>
126                <p>
127                  Microsoft Visual C++ version 14.1<br> Win32<br> double
128                </p>
129              </th>
130</tr></thead>
131<tbody><tr>
132<td>
133                <p>
134                  Random test data
135                </p>
136              </td>
137<td>
138                <p>
139                  <span class="blue">Max = 0.818ε (Mean = 0.227ε)</span><br> <br>
140                  (<span class="emphasis"><em>&lt;cmath&gt;:</em></span> Max = 0.818ε (Mean = 0.227ε))<br>
141                  (<span class="emphasis"><em>&lt;math.h&gt;:</em></span> Max = 0.818ε (Mean = 0.227ε))
142                </p>
143              </td>
144<td>
145                <p>
146                  <span class="blue">Max = 0.846ε (Mean = 0.153ε)</span><br> <br>
147                  (<span class="emphasis"><em>Rmath 3.2.3:</em></span> Max = 0.846ε (Mean = 0.153ε))
148                </p>
149              </td>
150<td>
151                <p>
152                  <span class="blue">Max = 2.3ε (Mean = 0.66ε)</span><br> <br>
153                  (<span class="emphasis"><em>&lt;math.h&gt;:</em></span> Max = 0.818ε (Mean = 0.249ε))
154                </p>
155              </td>
156<td>
157                <p>
158                  <span class="blue">Max = 0.509ε (Mean = 0.057ε)</span><br> <br>
159                  (<span class="emphasis"><em>&lt;math.h&gt;:</em></span> Max = 0.509ε (Mean = 0.057ε))
160                </p>
161              </td>
162</tr></tbody>
163</table></div>
164</div>
165<br class="table-break"><h5>
166<a name="math_toolkit.powers.log1p.h1"></a>
167        <span class="phrase"><a name="math_toolkit.powers.log1p.testing"></a></span><a class="link" href="log1p.html#math_toolkit.powers.log1p.testing">Testing</a>
168      </h5>
169<p>
170        A mixture of spot test sanity checks, and random high precision test values
171        calculated using NTL::RR at 1000-bit precision.
172      </p>
173</div>
174<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
175<td align="left"></td>
176<td align="right"><div class="copyright-footer">Copyright © 2006-2019 Nikhar
177      Agrawal, Anton Bikineev, Paul A. Bristow, Marco Guazzone, Christopher Kormanyos,
178      Hubert Holin, Bruno Lalande, John Maddock, Jeremy Murphy, Matthew Pulver, Johan
179      Råde, Gautam Sewani, Benjamin Sobotta, Nicholas Thompson, Thijs van den Berg,
180      Daryle Walker and Xiaogang Zhang<p>
181        Distributed under the Boost Software License, Version 1.0. (See accompanying
182        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
183      </p>
184</div></td>
185</tr></table>
186<hr>
187<div class="spirit-nav">
188<a accesskey="p" href="cos_pi.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../powers.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="expm1.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
189</div>
190</body>
191</html>
192