]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | <html> |
2 | <head> | |
3 | <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> | |
4 | <title>log1p</title> | |
5 | <link rel="stylesheet" href="../../math.css" type="text/css"> | |
6 | <meta name="generator" content="DocBook XSL Stylesheets V1.77.1"> | |
7 | <link rel="home" href="../../index.html" title="Math Toolkit 2.5.1"> | |
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"><</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">></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"><</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">></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"><</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 15. Policies: Controlling Precision, Error Handling etc">Policy</a><span class="special">></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 15. Policies: Controlling Precision, Error Handling etc">Policy</a><span class="special">&);</span> | |
38 | ||
39 | <span class="special">}}</span> <span class="comment">// namespaces</span> | |
40 | </pre> | |
41 | <p> | |
42 | Returns the natural logarithm of <code class="computeroutput"><span class="identifier">x</span><span class="special">+</span><span class="number">1</span></code>. | |
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 15. 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 15. 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 <code class="computeroutput"><span class="identifier">x</span></code> then | |
58 | <code class="computeroutput"><span class="identifier">x</span><span class="special">+</span><span class="number">1</span></code> suffers from catastrophic cancellation errors | |
59 | so that <code class="computeroutput"><span class="identifier">x</span><span class="special">+</span><span class="number">1</span> <span class="special">==</span> <span class="number">1</span></code> | |
60 | and <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> <span class="special">==</span> <span class="number">0</span></code>, | |
61 | when in fact for very small x, the best approximation to <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> would be | |
62 | <code class="computeroutput"><span class="identifier">x</span></code>. <code class="computeroutput"><span class="identifier">log1p</span></code> | |
63 | calculates the best 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 | |
64 | a Taylor series expansion for accuracy (less than 2ɛ). Alternatively note that | |
65 | there are faster methods available, for example using the equivalence: | |
66 | </p> | |
67 | <pre class="programlisting"><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> <span class="special">==</span> <span class="special">(</span><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> <span class="special">*</span> <span class="identifier">x</span><span class="special">)</span> <span class="special">/</span> <span class="special">((</span><span class="number">1</span><span class="special">+</span><span class="identifier">x</span><span class="special">)</span> <span class="special">-</span> <span class="number">1</span><span class="special">)</span> | |
68 | </pre> | |
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 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 | <p> | |
85 | <span class="inlinemediaobject"><img src="../../../graphs/log1p.svg" align="middle"></span> | |
86 | </p> | |
87 | <h5> | |
88 | <a name="math_toolkit.powers.log1p.h0"></a> | |
89 | <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> | |
90 | </h5> | |
91 | <p> | |
92 | For built in floating point types <code class="computeroutput"><span class="identifier">log1p</span></code> | |
93 | should have approximately 1 epsilon accuracy. | |
94 | </p> | |
95 | <div class="table"> | |
96 | <a name="math_toolkit.powers.log1p.table_log1p"></a><p class="title"><b>Table 6.78. Error rates for log1p</b></p> | |
97 | <div class="table-contents"><table class="table" summary="Error rates for log1p"> | |
98 | <colgroup> | |
99 | <col> | |
100 | <col> | |
101 | <col> | |
102 | <col> | |
103 | <col> | |
104 | </colgroup> | |
105 | <thead><tr> | |
106 | <th> | |
107 | </th> | |
108 | <th> | |
109 | <p> | |
110 | Microsoft Visual C++ version 12.0<br> Win32<br> double | |
111 | </p> | |
112 | </th> | |
113 | <th> | |
114 | <p> | |
115 | GNU C++ version 5.1.0<br> linux<br> double | |
116 | </p> | |
117 | </th> | |
118 | <th> | |
119 | <p> | |
120 | GNU C++ version 5.1.0<br> linux<br> long double | |
121 | </p> | |
122 | </th> | |
123 | <th> | |
124 | <p> | |
125 | Sun compiler version 0x5130<br> Sun Solaris<br> long double | |
126 | </p> | |
127 | </th> | |
128 | </tr></thead> | |
129 | <tbody><tr> | |
130 | <td> | |
131 | <p> | |
132 | Random test data | |
133 | </p> | |
134 | </td> | |
135 | <td> | |
136 | <p> | |
137 | <span class="blue">Max = 0.509ε (Mean = 0.057ε)</span><br> <br> | |
138 | (<span class="emphasis"><em><math.h>:</em></span> Max = 0.509ε (Mean = 0.057ε)) | |
139 | </p> | |
140 | </td> | |
141 | <td> | |
142 | <p> | |
143 | <span class="blue">Max = 0.846ε (Mean = 0.153ε)</span><br> <br> | |
144 | (<span class="emphasis"><em>Rmath 3.0.2:</em></span> Max = 0.846ε (Mean = 0.153ε))<br> | |
145 | (<span class="emphasis"><em>Cephes:</em></span> Max = 0.799ε (Mean = 0.122ε)) | |
146 | </p> | |
147 | </td> | |
148 | <td> | |
149 | <p> | |
150 | <span class="blue">Max = 0.818ε (Mean = 0.227ε)</span><br> <br> | |
151 | (<span class="emphasis"><em><tr1/cmath>:</em></span> Max = 0.818ε (Mean = 0.227ε))<br> | |
152 | (<span class="emphasis"><em><math.h>:</em></span> Max = 0.818ε (Mean = 0.227ε)) | |
153 | </p> | |
154 | </td> | |
155 | <td> | |
156 | <p> | |
157 | <span class="blue">Max = 1.53ε (Mean = 0.627ε)</span><br> <br> | |
158 | (<span class="emphasis"><em><math.h>:</em></span> Max = 0.818ε (Mean = 0.249ε)) | |
159 | </p> | |
160 | </td> | |
161 | </tr></tbody> | |
162 | </table></div> | |
163 | </div> | |
164 | <br class="table-break"><h5> | |
165 | <a name="math_toolkit.powers.log1p.h1"></a> | |
166 | <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> | |
167 | </h5> | |
168 | <p> | |
169 | A mixture of spot test sanity checks, and random high precision test values | |
170 | calculated using NTL::RR at 1000-bit precision. | |
171 | </p> | |
172 | </div> | |
173 | <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> | |
174 | <td align="left"></td> | |
175 | <td align="right"><div class="copyright-footer">Copyright © 2006-2010, 2012-2014 Nikhar Agrawal, | |
176 | Anton Bikineev, Paul A. Bristow, Marco Guazzone, Christopher Kormanyos, Hubert | |
177 | Holin, Bruno Lalande, John Maddock, Jeremy Murphy, Johan Råde, Gautam Sewani, | |
178 | Benjamin Sobotta, Thijs van den Berg, Daryle Walker and Xiaogang Zhang<p> | |
179 | Distributed under the Boost Software License, Version 1.0. (See accompanying | |
180 | 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>) | |
181 | </p> | |
182 | </div></td> | |
183 | </tr></table> | |
184 | <hr> | |
185 | <div class="spirit-nav"> | |
186 | <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> | |
187 | </div> | |
188 | </body> | |
189 | </html> |