42 lines · plain
1// (C) Copyright Matt Borland 2022.2// Use, modification and distribution are subject to the3// Boost Software License, Version 1.0. (See accompanying file4// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)5 6#include <cmath>7#include <limits>8#include <boost/math/special_functions/fpclassify.hpp>9#include <boost/math/constants/constants.hpp>10 11namespace boost { namespace math {12 13// Calculates log(exp(x1) + exp(x2))14template <typename Real>15Real logaddexp(Real x1, Real x2) noexcept16{17 using std::log1p;18 using std::exp;19 using std::abs;20 21 // Validate inputs first22 if (!(boost::math::isfinite)(x1))23 {24 return x1;25 }26 else if (!(boost::math::isfinite)(x2))27 {28 return x2;29 }30 31 const Real temp = x1 - x2;32 33 if (temp > 0)34 {35 return x1 + log1p(exp(-temp));36 }37 38 return x2 + log1p(exp(temp));39}40 41}} // Namespace boost::math42