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
12 #pragma warning(disable:4702) // Unreachable code (release mode only warning)
15 #include <boost/config/no_tr1/cmath.hpp>
16 #include <math.h> // platform's ::log1p
17 #include <boost/limits.hpp>
18 #include <boost/math/tools/config.hpp>
19 #include <boost/math/tools/series.hpp>
20 #include <boost/math/tools/rational.hpp>
21 #include <boost/math/tools/big_constant.hpp>
22 #include <boost/math/policies/error_handling.hpp>
23 #include <boost/math/special_functions/math_fwd.hpp>
25 #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
26 # include <boost/static_assert.hpp>
28 # include <boost/assert.hpp>
31 namespace boost{ namespace math{
35 // Functor log1p_series returns the next term in the Taylor series
36 // pow(-1, k-1)*pow(x, k) / k
37 // each time that operator() is invoked.
42 typedef T result_type;
45 : k(0), m_mult(-x), m_prod(-1){}
62 log1p_series(const log1p_series&);
63 log1p_series& operator=(const log1p_series&);
66 // Algorithm log1p is part of C99, but is not yet provided by many compilers.
68 // This version uses a Taylor series expansion for 0.5 > x > epsilon, which may
69 // require up to std::numeric_limits<T>::digits+1 terms to be calculated.
70 // It would be much more efficient to use the equivalence:
71 // log(1+x) == (log(1+x) * x) / ((1-x) - 1)
72 // Unfortunately many optimizing compilers make such a mess of this, that
73 // it performs no better than log(1+x): which is to say not very well at all.
75 template <class T, class Policy>
76 T log1p_imp(T const & x, const Policy& pol, const mpl::int_<0>&)
77 { // The function returns the natural logarithm of 1 + x.
78 typedef typename tools::promote_args<T>::type result_type;
81 static const char* function = "boost::math::log1p<%1%>(%1%)";
84 return policies::raise_domain_error<T>(
85 function, "log1p(x) requires x > -1, but got x = %1%.", x, pol);
87 return -policies::raise_overflow_error<T>(
90 result_type a = abs(result_type(x));
91 if(a > result_type(0.5f))
92 return log(1 + result_type(x));
93 // Note that without numeric_limits specialisation support,
94 // epsilon just returns zero, and our "optimisation" will always fail:
95 if(a < tools::epsilon<result_type>())
97 detail::log1p_series<result_type> s(x);
98 boost::uintmax_t max_iter = policies::get_max_series_iterations<Policy>();
99 #if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x582)) && !BOOST_WORKAROUND(__EDG_VERSION__, <= 245)
100 result_type result = tools::sum_series(s, policies::get_epsilon<result_type, Policy>(), max_iter);
102 result_type zero = 0;
103 result_type result = tools::sum_series(s, policies::get_epsilon<result_type, Policy>(), max_iter, zero);
105 policies::check_series_iterations<T>(function, max_iter, pol);
109 template <class T, class Policy>
110 T log1p_imp(T const& x, const Policy& pol, const mpl::int_<53>&)
111 { // The function returns the natural logarithm of 1 + x.
114 static const char* function = "boost::math::log1p<%1%>(%1%)";
117 return policies::raise_domain_error<T>(
118 function, "log1p(x) requires x > -1, but got x = %1%.", x, pol);
120 return -policies::raise_overflow_error<T>(
126 // Note that without numeric_limits specialisation support,
127 // epsilon just returns zero, and our "optimisation" will always fail:
128 if(a < tools::epsilon<T>())
131 // Maximum Deviation Found: 1.846e-017
132 // Expected Error Term: 1.843e-017
133 // Maximum Relative Change in Control Points: 8.138e-004
134 // Max Error found at double precision = 3.250766e-016
135 static const T P[] = {
136 0.15141069795941984e-16L,
137 0.35495104378055055e-15L,
138 0.33333333333332835L,
139 0.99249063543365859L,
141 0.58052937949269651L,
142 0.13703234928513215L,
143 0.011294864812099712L
145 static const T Q[] = {
151 0.31706251443180914L,
152 0.022665554431410243L,
153 -0.29252538135177773e-5L
156 T result = 1 - x / 2 + tools::evaluate_polynomial(P, x) / tools::evaluate_polynomial(Q, x);
162 template <class T, class Policy>
163 T log1p_imp(T const& x, const Policy& pol, const mpl::int_<64>&)
164 { // The function returns the natural logarithm of 1 + x.
167 static const char* function = "boost::math::log1p<%1%>(%1%)";
170 return policies::raise_domain_error<T>(
171 function, "log1p(x) requires x > -1, but got x = %1%.", x, pol);
173 return -policies::raise_overflow_error<T>(
179 // Note that without numeric_limits specialisation support,
180 // epsilon just returns zero, and our "optimisation" will always fail:
181 if(a < tools::epsilon<T>())
184 // Maximum Deviation Found: 8.089e-20
185 // Expected Error Term: 8.088e-20
186 // Maximum Relative Change in Control Points: 9.648e-05
187 // Max Error found at long double precision = 2.242324e-19
188 static const T P[] = {
189 BOOST_MATH_BIG_CONSTANT(T, 64, -0.807533446680736736712e-19),
190 BOOST_MATH_BIG_CONSTANT(T, 64, -0.490881544804798926426e-18),
191 BOOST_MATH_BIG_CONSTANT(T, 64, 0.333333333333333373941),
192 BOOST_MATH_BIG_CONSTANT(T, 64, 1.17141290782087994162),
193 BOOST_MATH_BIG_CONSTANT(T, 64, 1.62790522814926264694),
194 BOOST_MATH_BIG_CONSTANT(T, 64, 1.13156411870766876113),
195 BOOST_MATH_BIG_CONSTANT(T, 64, 0.408087379932853785336),
196 BOOST_MATH_BIG_CONSTANT(T, 64, 0.0706537026422828914622),
197 BOOST_MATH_BIG_CONSTANT(T, 64, 0.00441709903782239229447)
199 static const T Q[] = {
200 BOOST_MATH_BIG_CONSTANT(T, 64, 1.0),
201 BOOST_MATH_BIG_CONSTANT(T, 64, 4.26423872346263928361),
202 BOOST_MATH_BIG_CONSTANT(T, 64, 7.48189472704477708962),
203 BOOST_MATH_BIG_CONSTANT(T, 64, 6.94757016732904280913),
204 BOOST_MATH_BIG_CONSTANT(T, 64, 3.6493508622280767304),
205 BOOST_MATH_BIG_CONSTANT(T, 64, 1.06884863623790638317),
206 BOOST_MATH_BIG_CONSTANT(T, 64, 0.158292216998514145947),
207 BOOST_MATH_BIG_CONSTANT(T, 64, 0.00885295524069924328658),
208 BOOST_MATH_BIG_CONSTANT(T, 64, -0.560026216133415663808e-6)
211 T result = 1 - x / 2 + tools::evaluate_polynomial(P, x) / tools::evaluate_polynomial(Q, x);
217 template <class T, class Policy>
218 T log1p_imp(T const& x, const Policy& pol, const mpl::int_<24>&)
219 { // The function returns the natural logarithm of 1 + x.
222 static const char* function = "boost::math::log1p<%1%>(%1%)";
225 return policies::raise_domain_error<T>(
226 function, "log1p(x) requires x > -1, but got x = %1%.", x, pol);
228 return -policies::raise_overflow_error<T>(
234 // Note that without numeric_limits specialisation support,
235 // epsilon just returns zero, and our "optimisation" will always fail:
236 if(a < tools::epsilon<T>())
239 // Maximum Deviation Found: 6.910e-08
240 // Expected Error Term: 6.910e-08
241 // Maximum Relative Change in Control Points: 2.509e-04
242 // Max Error found at double precision = 6.910422e-08
243 // Max Error found at float precision = 8.357242e-08
244 static const T P[] = {
245 -0.671192866803148236519e-7L,
246 0.119670999140731844725e-6L,
247 0.333339469182083148598L,
248 0.237827183019664122066L
250 static const T Q[] = {
252 1.46348272586988539733L,
253 0.497859871350117338894L,
254 -0.00471666268910169651936L
257 T result = 1 - x / 2 + tools::evaluate_polynomial(P, x) / tools::evaluate_polynomial(Q, x);
263 template <class T, class Policy, class tag>
264 struct log1p_initializer
273 static void do_init(const mpl::int_<N>&){}
274 static void do_init(const mpl::int_<64>&)
276 boost::math::log1p(static_cast<T>(0.25), Policy());
278 void force_instantiate()const{}
280 static const init initializer;
281 static void force_instantiate()
283 initializer.force_instantiate();
287 template <class T, class Policy, class tag>
288 const typename log1p_initializer<T, Policy, tag>::init log1p_initializer<T, Policy, tag>::initializer;
291 } // namespace detail
293 template <class T, class Policy>
294 inline typename tools::promote_args<T>::type log1p(T x, const Policy&)
296 typedef typename tools::promote_args<T>::type result_type;
297 typedef typename policies::evaluation<result_type, Policy>::type value_type;
298 typedef typename policies::precision<result_type, Policy>::type precision_type;
299 typedef typename policies::normalise<
301 policies::promote_float<false>,
302 policies::promote_double<false>,
303 policies::discrete_quantile<>,
304 policies::assert_undefined<> >::type forwarding_policy;
306 typedef typename mpl::if_<
307 mpl::less_equal<precision_type, mpl::int_<0> >,
310 mpl::less_equal<precision_type, mpl::int_<53> >,
311 mpl::int_<53>, // double
313 mpl::less_equal<precision_type, mpl::int_<64> >,
314 mpl::int_<64>, // 80-bit long double
315 mpl::int_<0> // too many bits, use generic version.
320 detail::log1p_initializer<value_type, forwarding_policy, tag_type>::force_instantiate();
322 return policies::checked_narrowing_cast<result_type, forwarding_policy>(
323 detail::log1p_imp(static_cast<value_type>(x), forwarding_policy(), tag_type()), "boost::math::log1p<%1%>(%1%)");
326 #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
327 // These overloads work around a type deduction bug:
328 inline float log1p(float z)
330 return log1p<float>(z);
332 inline double log1p(double z)
334 return log1p<double>(z);
336 #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
337 inline long double log1p(long double z)
339 return log1p<long double>(z);
345 # ifndef BOOST_HAS_LOG1P
346 # define BOOST_HAS_LOG1P
351 #if defined(BOOST_HAS_LOG1P) && !(defined(__osf__) && defined(__DECCXX_VER))
352 # ifdef BOOST_MATH_USE_C99
353 template <class Policy>
354 inline float log1p(float x, const Policy& pol)
357 return policies::raise_domain_error<float>(
358 "log1p<%1%>(%1%)", "log1p(x) requires x > -1, but got x = %1%.", x, pol);
360 return -policies::raise_overflow_error<float>(
361 "log1p<%1%>(%1%)", 0, pol);
364 #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
365 template <class Policy>
366 inline long double log1p(long double x, const Policy& pol)
369 return policies::raise_domain_error<long double>(
370 "log1p<%1%>(%1%)", "log1p(x) requires x > -1, but got x = %1%.", x, pol);
372 return -policies::raise_overflow_error<long double>(
373 "log1p<%1%>(%1%)", 0, pol);
378 template <class Policy>
379 inline float log1p(float x, const Policy& pol)
382 return policies::raise_domain_error<float>(
383 "log1p<%1%>(%1%)", "log1p(x) requires x > -1, but got x = %1%.", x, pol);
385 return -policies::raise_overflow_error<float>(
386 "log1p<%1%>(%1%)", 0, pol);
390 template <class Policy>
391 inline double log1p(double x, const Policy& pol)
394 return policies::raise_domain_error<double>(
395 "log1p<%1%>(%1%)", "log1p(x) requires x > -1, but got x = %1%.", x, pol);
397 return -policies::raise_overflow_error<double>(
398 "log1p<%1%>(%1%)", 0, pol);
401 #elif defined(_MSC_VER) && (BOOST_MSVC >= 1400)
403 // You should only enable this branch if you are absolutely sure
404 // that your compilers optimizer won't mess this code up!!
405 // Currently tested with VC8 and Intel 9.1.
407 template <class Policy>
408 inline double log1p(double x, const Policy& pol)
411 return policies::raise_domain_error<double>(
412 "log1p<%1%>(%1%)", "log1p(x) requires x > -1, but got x = %1%.", x, pol);
414 return -policies::raise_overflow_error<double>(
415 "log1p<%1%>(%1%)", 0, pol);
420 return ::log(u)*(x/(u-1.0));
422 template <class Policy>
423 inline float log1p(float x, const Policy& pol)
425 return static_cast<float>(boost::math::log1p(static_cast<double>(x), pol));
429 // For some reason this fails to compile under WinCE...
430 // Needs more investigation.
432 template <class Policy>
433 inline long double log1p(long double x, const Policy& pol)
436 return policies::raise_domain_error<long double>(
437 "log1p<%1%>(%1%)", "log1p(x) requires x > -1, but got x = %1%.", x, pol);
439 return -policies::raise_overflow_error<long double>(
440 "log1p<%1%>(%1%)", 0, pol);
445 return ::logl(u)*(x/(u-1.0));
451 inline typename tools::promote_args<T>::type log1p(T x)
453 return boost::math::log1p(x, policies::policy<>());
456 // Compute log(1+x)-x:
458 template <class T, class Policy>
459 inline typename tools::promote_args<T>::type
460 log1pmx(T x, const Policy& pol)
462 typedef typename tools::promote_args<T>::type result_type;
464 static const char* function = "boost::math::log1pmx<%1%>(%1%)";
467 return policies::raise_domain_error<T>(
468 function, "log1pmx(x) requires x > -1, but got x = %1%.", x, pol);
470 return -policies::raise_overflow_error<T>(
473 result_type a = abs(result_type(x));
474 if(a > result_type(0.95f))
475 return log(1 + result_type(x)) - result_type(x);
476 // Note that without numeric_limits specialisation support,
477 // epsilon just returns zero, and our "optimisation" will always fail:
478 if(a < tools::epsilon<result_type>())
480 boost::math::detail::log1p_series<T> s(x);
482 boost::uintmax_t max_iter = policies::get_max_series_iterations<Policy>();
483 #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x582))
485 T result = boost::math::tools::sum_series(s, policies::get_epsilon<T, Policy>(), max_iter, zero);
487 T result = boost::math::tools::sum_series(s, policies::get_epsilon<T, Policy>(), max_iter);
489 policies::check_series_iterations<T>(function, max_iter, pol);
494 inline typename tools::promote_args<T>::type log1pmx(T x)
496 return log1pmx(x, policies::policy<>());
506 #endif // BOOST_MATH_LOG1P_INCLUDED