1 // (C) Copyright John Maddock 2005-2006.
2 // Use, modification and distribution are subject to the
3 // Boost Software License, Version 1.0. (See accompanying file
4 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 #ifndef BOOST_MATH_LOG1P_INCLUDED
7 #define BOOST_MATH_LOG1P_INCLUDED
13 #include <boost/config/no_tr1/cmath.hpp>
14 #include <math.h> // platform's ::log1p
15 #include <boost/limits.hpp>
16 #include <boost/math/tools/config.hpp>
17 #include <boost/math/tools/series.hpp>
18 #include <boost/math/tools/rational.hpp>
19 #include <boost/math/policies/error_handling.hpp>
20 #include <boost/math/special_functions/math_fwd.hpp>
22 #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
23 # include <boost/static_assert.hpp>
25 # include <boost/assert.hpp>
28 namespace boost{ namespace math{
32 // Functor log1p_series returns the next term in the Taylor series
33 // pow(-1, k-1)*pow(x, k) / k
34 // each time that operator() is invoked.
39 typedef T result_type;
42 : k(0), m_mult(-x), m_prod(-1){}
59 log1p_series(const log1p_series&);
60 log1p_series& operator=(const log1p_series&);
63 // Algorithm log1p is part of C99, but is not yet provided by many compilers.
65 // This version uses a Taylor series expansion for 0.5 > x > epsilon, which may
66 // require up to std::numeric_limits<T>::digits+1 terms to be calculated.
67 // It would be much more efficient to use the equivalence:
68 // log(1+x) == (log(1+x) * x) / ((1-x) - 1)
69 // Unfortunately many optimizing compilers make such a mess of this, that
70 // it performs no better than log(1+x): which is to say not very well at all.
72 template <class T, class Policy>
73 T log1p_imp(T const & x, const Policy& pol, const mpl::int_<0>&)
74 { // The function returns the natural logarithm of 1 + x.
75 typedef typename tools::promote_args<T>::type result_type;
78 static const char* function = "boost::math::log1p<%1%>(%1%)";
81 return policies::raise_domain_error<T>(
82 function, "log1p(x) requires x > -1, but got x = %1%.", x, pol);
84 return -policies::raise_overflow_error<T>(
87 result_type a = abs(result_type(x));
88 if(a > result_type(0.5f))
89 return log(1 + result_type(x));
90 // Note that without numeric_limits specialisation support,
91 // epsilon just returns zero, and our "optimisation" will always fail:
92 if(a < tools::epsilon<result_type>())
94 detail::log1p_series<result_type> s(x);
95 boost::uintmax_t max_iter = policies::get_max_series_iterations<Policy>();
96 #if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x582)) && !BOOST_WORKAROUND(__EDG_VERSION__, <= 245)
97 result_type result = tools::sum_series(s, policies::get_epsilon<result_type, Policy>(), max_iter);
100 result_type result = tools::sum_series(s, policies::get_epsilon<result_type, Policy>(), max_iter, zero);
102 policies::check_series_iterations(function, max_iter, pol);
106 template <class T, class Policy>
107 T log1p_imp(T const& x, const Policy& pol, const mpl::int_<53>&)
108 { // The function returns the natural logarithm of 1 + x.
111 static const char* function = "boost::math::log1p<%1%>(%1%)";
114 return policies::raise_domain_error<T>(
115 function, "log1p(x) requires x > -1, but got x = %1%.", x, pol);
117 return -policies::raise_overflow_error<T>(
123 // Note that without numeric_limits specialisation support,
124 // epsilon just returns zero, and our "optimisation" will always fail:
125 if(a < tools::epsilon<T>())
128 // Maximum Deviation Found: 1.846e-017
129 // Expected Error Term: 1.843e-017
130 // Maximum Relative Change in Control Points: 8.138e-004
131 // Max Error found at double precision = 3.250766e-016
132 static const T P[] = {
133 0.15141069795941984e-16L,
134 0.35495104378055055e-15L,
135 0.33333333333332835L,
136 0.99249063543365859L,
138 0.58052937949269651L,
139 0.13703234928513215L,
140 0.011294864812099712L
142 static const T Q[] = {
148 0.31706251443180914L,
149 0.022665554431410243L,
150 -0.29252538135177773e-5L
153 T result = 1 - x / 2 + tools::evaluate_polynomial(P, x) / tools::evaluate_polynomial(Q, x);
159 template <class T, class Policy>
160 T log1p_imp(T const& x, const Policy& pol, const mpl::int_<64>&)
161 { // The function returns the natural logarithm of 1 + x.
164 static const char* function = "boost::math::log1p<%1%>(%1%)";
167 return policies::raise_domain_error<T>(
168 function, "log1p(x) requires x > -1, but got x = %1%.", x, pol);
170 return -policies::raise_overflow_error<T>(
176 // Note that without numeric_limits specialisation support,
177 // epsilon just returns zero, and our "optimisation" will always fail:
178 if(a < tools::epsilon<T>())
181 // Maximum Deviation Found: 8.089e-20
182 // Expected Error Term: 8.088e-20
183 // Maximum Relative Change in Control Points: 9.648e-05
184 // Max Error found at long double precision = 2.242324e-19
185 static const T P[] = {
186 -0.807533446680736736712e-19L,
187 -0.490881544804798926426e-18L,
188 0.333333333333333373941L,
189 1.17141290782087994162L,
190 1.62790522814926264694L,
191 1.13156411870766876113L,
192 0.408087379932853785336L,
193 0.0706537026422828914622L,
194 0.00441709903782239229447L
196 static const T Q[] = {
198 4.26423872346263928361L,
199 7.48189472704477708962L,
200 6.94757016732904280913L,
201 3.6493508622280767304L,
202 1.06884863623790638317L,
203 0.158292216998514145947L,
204 0.00885295524069924328658L,
205 -0.560026216133415663808e-6L
208 T result = 1 - x / 2 + tools::evaluate_polynomial(P, x) / tools::evaluate_polynomial(Q, x);
214 template <class T, class Policy>
215 T log1p_imp(T const& x, const Policy& pol, const mpl::int_<24>&)
216 { // The function returns the natural logarithm of 1 + x.
219 static const char* function = "boost::math::log1p<%1%>(%1%)";
222 return policies::raise_domain_error<T>(
223 function, "log1p(x) requires x > -1, but got x = %1%.", x, pol);
225 return -policies::raise_overflow_error<T>(
231 // Note that without numeric_limits specialisation support,
232 // epsilon just returns zero, and our "optimisation" will always fail:
233 if(a < tools::epsilon<T>())
236 // Maximum Deviation Found: 6.910e-08
237 // Expected Error Term: 6.910e-08
238 // Maximum Relative Change in Control Points: 2.509e-04
239 // Max Error found at double precision = 6.910422e-08
240 // Max Error found at float precision = 8.357242e-08
241 static const T P[] = {
242 -0.671192866803148236519e-7L,
243 0.119670999140731844725e-6L,
244 0.333339469182083148598L,
245 0.237827183019664122066L
247 static const T Q[] = {
249 1.46348272586988539733L,
250 0.497859871350117338894L,
251 -0.00471666268910169651936L
254 T result = 1 - x / 2 + tools::evaluate_polynomial(P, x) / tools::evaluate_polynomial(Q, x);
260 } // namespace detail
262 template <class T, class Policy>
263 inline typename tools::promote_args<T>::type log1p(T x, const Policy&)
265 typedef typename tools::promote_args<T>::type result_type;
266 typedef typename policies::evaluation<result_type, Policy>::type value_type;
267 typedef typename policies::precision<result_type, Policy>::type precision_type;
268 typedef typename policies::normalise<
270 policies::promote_float<false>,
271 policies::promote_double<false>,
272 policies::discrete_quantile<>,
273 policies::assert_undefined<> >::type forwarding_policy;
275 typedef typename mpl::if_<
276 mpl::less_equal<precision_type, mpl::int_<0> >,
279 mpl::less_equal<precision_type, mpl::int_<53> >,
280 mpl::int_<53>, // double
282 mpl::less_equal<precision_type, mpl::int_<64> >,
283 mpl::int_<64>, // 80-bit long double
284 mpl::int_<0> // too many bits, use generic version.
288 return policies::checked_narrowing_cast<result_type, forwarding_policy>(
289 detail::log1p_imp(static_cast<value_type>(x), forwarding_policy(), tag_type()), "boost::math::log1p<%1%>(%1%)");
292 #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
293 // These overloads work around a type deduction bug:
294 inline float log1p(float z)
296 return log1p<float>(z);
298 inline double log1p(double z)
300 return log1p<double>(z);
302 #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
303 inline long double log1p(long double z)
305 return log1p<long double>(z);
311 # ifndef BOOST_HAS_LOG1P
312 # define BOOST_HAS_LOG1P
317 #if defined(BOOST_HAS_LOG1P) && !(defined(__osf__) && defined(__DECCXX_VER))
318 # ifdef BOOST_MATH_USE_C99
319 template <class Policy>
320 inline float log1p(float x, const Policy& pol)
323 return policies::raise_domain_error<float>(
324 "log1p<%1%>(%1%)", "log1p(x) requires x > -1, but got x = %1%.", x, pol);
326 return -policies::raise_overflow_error<float>(
327 "log1p<%1%>(%1%)", 0, pol);
330 #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
331 template <class Policy>
332 inline long double log1p(long double x, const Policy& pol)
335 return policies::raise_domain_error<long double>(
336 "log1p<%1%>(%1%)", "log1p(x) requires x > -1, but got x = %1%.", x, pol);
338 return -policies::raise_overflow_error<long double>(
339 "log1p<%1%>(%1%)", 0, pol);
344 template <class Policy>
345 inline float log1p(float x, const Policy& pol)
348 return policies::raise_domain_error<float>(
349 "log1p<%1%>(%1%)", "log1p(x) requires x > -1, but got x = %1%.", x, pol);
351 return -policies::raise_overflow_error<float>(
352 "log1p<%1%>(%1%)", 0, pol);
356 template <class Policy>
357 inline double log1p(double x, const Policy& pol)
360 return policies::raise_domain_error<double>(
361 "log1p<%1%>(%1%)", "log1p(x) requires x > -1, but got x = %1%.", x, pol);
363 return -policies::raise_overflow_error<double>(
364 "log1p<%1%>(%1%)", 0, pol);
367 #elif defined(_MSC_VER) && (BOOST_MSVC >= 1400)
369 // You should only enable this branch if you are absolutely sure
370 // that your compilers optimizer won't mess this code up!!
371 // Currently tested with VC8 and Intel 9.1.
373 template <class Policy>
374 inline double log1p(double x, const Policy& pol)
377 return policies::raise_domain_error<double>(
378 "log1p<%1%>(%1%)", "log1p(x) requires x > -1, but got x = %1%.", x, pol);
380 return -policies::raise_overflow_error<double>(
381 "log1p<%1%>(%1%)", 0, pol);
386 return ::log(u)*(x/(u-1.0));
388 template <class Policy>
389 inline float log1p(float x, const Policy& pol)
391 return static_cast<float>(boost::math::log1p(static_cast<double>(x), pol));
393 template <class Policy>
394 inline long double log1p(long double x, const Policy& pol)
397 return policies::raise_domain_error<long double>(
398 "log1p<%1%>(%1%)", "log1p(x) requires x > -1, but got x = %1%.", x, pol);
400 return -policies::raise_overflow_error<long double>(
401 "log1p<%1%>(%1%)", 0, pol);
406 return ::logl(u)*(x/(u-1.0));
411 inline typename tools::promote_args<T>::type log1p(T x)
413 return boost::math::log1p(x, policies::policy<>());
416 // Compute log(1+x)-x:
418 template <class T, class Policy>
419 inline typename tools::promote_args<T>::type
420 log1pmx(T x, const Policy& pol)
422 typedef typename tools::promote_args<T>::type result_type;
424 static const char* function = "boost::math::log1pmx<%1%>(%1%)";
427 return policies::raise_domain_error<T>(
428 function, "log1pmx(x) requires x > -1, but got x = %1%.", x, pol);
430 return -policies::raise_overflow_error<T>(
433 result_type a = abs(result_type(x));
434 if(a > result_type(0.95f))
435 return log(1 + result_type(x)) - result_type(x);
436 // Note that without numeric_limits specialisation support,
437 // epsilon just returns zero, and our "optimisation" will always fail:
438 if(a < tools::epsilon<result_type>())
440 boost::math::detail::log1p_series<T> s(x);
442 boost::uintmax_t max_iter = policies::get_max_series_iterations<Policy>();
443 #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x582))
445 T result = boost::math::tools::sum_series(s, policies::get_epsilon<T, Policy>(), max_iter, zero);
447 T result = boost::math::tools::sum_series(s, policies::get_epsilon<T, Policy>(), max_iter);
449 policies::check_series_iterations(function, max_iter, pol);
454 inline typename tools::promote_args<T>::type log1pmx(T x)
456 return log1pmx(x, policies::policy<>());
462 #endif // BOOST_MATH_LOG1P_INCLUDED