mirror of
https://github.com/boostorg/math.git
synced 2026-01-19 04:22:09 +00:00
Modernise log1p/expm1 to mostly use their std:: equivalents.
Add support for <cstdfloat> types as well. Extend tests for better coverage.
This commit is contained in:
@@ -15,6 +15,14 @@
|
||||
|
||||
#ifndef BOOST_MATH_HAS_NVRTC
|
||||
|
||||
#if defined __has_include
|
||||
# if __cplusplus > 202002L || _MSVC_LANG > 202002L
|
||||
# if __has_include (<stdfloat>)
|
||||
# include <stdfloat>
|
||||
# endif
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#include <boost/math/tools/series.hpp>
|
||||
#include <boost/math/tools/precision.hpp>
|
||||
#include <boost/math/tools/big_constant.hpp>
|
||||
@@ -37,7 +45,8 @@
|
||||
#pragma GCC system_header
|
||||
#endif
|
||||
|
||||
namespace boost{ namespace math{
|
||||
namespace boost {
|
||||
namespace math {
|
||||
|
||||
namespace detail
|
||||
{
|
||||
@@ -51,7 +60,8 @@ namespace detail
|
||||
typedef T result_type;
|
||||
|
||||
BOOST_MATH_GPU_ENABLED expm1_series(T x)
|
||||
: k(0), m_x(x), m_term(1) {}
|
||||
: k(0), m_x(x), m_term(1) {
|
||||
}
|
||||
|
||||
BOOST_MATH_GPU_ENABLED T operator()()
|
||||
{
|
||||
@@ -273,15 +283,9 @@ BOOST_MATH_GPU_ENABLED inline typename tools::promote_args<T>::type expm1(T x, c
|
||||
tag_type(), forwarding_policy()), "boost::math::expm1<%1%>(%1%)");
|
||||
}
|
||||
|
||||
#ifdef expm1
|
||||
# ifndef BOOST_HAS_expm1
|
||||
# define BOOST_HAS_expm1
|
||||
# endif
|
||||
# undef expm1
|
||||
#endif
|
||||
|
||||
#if defined(BOOST_HAS_EXPM1) && !(defined(__osf__) && defined(__DECCXX_VER))
|
||||
# ifdef BOOST_MATH_USE_C99
|
||||
//
|
||||
// Since we now live in a post C++11 world, we can always defer to std::expm1 when appropriate:
|
||||
//
|
||||
template <class Policy>
|
||||
BOOST_MATH_GPU_ENABLED inline float expm1(float x, const Policy&)
|
||||
{
|
||||
@@ -295,7 +299,7 @@ BOOST_MATH_GPU_ENABLED inline float expm1(float x, const Policy&)
|
||||
if (x >= tools::log_max_value<float>())
|
||||
return policies::raise_overflow_error<float>("boost::math::expm1<%1%>(%1%)", nullptr, Policy());
|
||||
}
|
||||
return ::expm1f(x);
|
||||
return std::expm1(x);
|
||||
}
|
||||
#ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
|
||||
template <class Policy>
|
||||
@@ -311,42 +315,9 @@ inline long double expm1(long double x, const Policy&)
|
||||
if (x >= tools::log_max_value<long double>())
|
||||
return policies::raise_overflow_error<long double>("boost::math::expm1<%1%>(%1%)", nullptr, Policy());
|
||||
}
|
||||
return ::expm1l(x);
|
||||
return std::expm1(x);
|
||||
}
|
||||
#endif
|
||||
# else
|
||||
template <class Policy>
|
||||
inline float expm1(float x, const Policy&)
|
||||
{
|
||||
BOOST_MATH_IF_CONSTEXPR(Policy::domain_error_type::value != boost::math::policies::ignore_error && Policy::domain_error_type::value != boost::math::policies::errno_on_error)
|
||||
{
|
||||
if ((boost::math::isnan)(x))
|
||||
return policies::raise_domain_error<float>("boost::math::expm1<%1%>(%1%)", "expm1 requires a finite argument, but got %1%", x, Policy());
|
||||
}
|
||||
BOOST_MATH_IF_CONSTEXPR(Policy::overflow_error_type::value != boost::math::policies::ignore_error && Policy::overflow_error_type::value != boost::math::policies::errno_on_error)
|
||||
{
|
||||
if (x >= tools::log_max_value<float>())
|
||||
return policies::raise_overflow_error<float>("boost::math::expm1<%1%>(%1%)", nullptr, Policy());
|
||||
}
|
||||
return static_cast<float>(::expm1(x));
|
||||
}
|
||||
template <class Policy>
|
||||
inline typename std::enable_if<sizeof(double) == sizeof(long double), long double>::type expm1(long double x, const Policy&)
|
||||
{
|
||||
BOOST_MATH_IF_CONSTEXPR(Policy::domain_error_type::value != boost::math::policies::ignore_error && Policy::domain_error_type::value != boost::math::policies::errno_on_error)
|
||||
{
|
||||
if ((boost::math::isnan)(x))
|
||||
return policies::raise_domain_error<long double>("boost::math::expm1<%1%>(%1%)", "expm1 requires a finite argument, but got %1%", x, Policy());
|
||||
}
|
||||
BOOST_MATH_IF_CONSTEXPR(Policy::overflow_error_type::value != boost::math::policies::ignore_error && Policy::overflow_error_type::value != boost::math::policies::errno_on_error)
|
||||
{
|
||||
if (x >= tools::log_max_value<float>())
|
||||
return policies::raise_overflow_error<long double>("boost::math::expm1<%1%>(%1%)", nullptr, Policy());
|
||||
}
|
||||
return ::expm1(x);
|
||||
}
|
||||
# endif
|
||||
|
||||
template <class Policy>
|
||||
BOOST_MATH_GPU_ENABLED inline double expm1(double x, const Policy&)
|
||||
{
|
||||
@@ -360,16 +331,45 @@ BOOST_MATH_GPU_ENABLED inline double expm1(double x, const Policy&)
|
||||
if (x >= tools::log_max_value<double>())
|
||||
return policies::raise_overflow_error<double>("boost::math::expm1<%1%>(%1%)", nullptr, Policy());
|
||||
}
|
||||
return ::expm1(x);
|
||||
return std::expm1(x);
|
||||
}
|
||||
#endif
|
||||
|
||||
template <class T>
|
||||
BOOST_MATH_GPU_ENABLED inline typename tools::promote_args<T>::type expm1(T x)
|
||||
{
|
||||
return expm1(x, policies::policy<>());
|
||||
}
|
||||
|
||||
//
|
||||
// Specific width floating point types:
|
||||
//
|
||||
#ifdef __STDCPP_FLOAT32_T__
|
||||
template <class Policy>
|
||||
BOOST_MATH_GPU_ENABLED inline std::float32_t expm1(std::float32_t x, const Policy& pol)
|
||||
{
|
||||
return boost::math::expm1(static_cast<float>(x), pol);
|
||||
}
|
||||
#endif
|
||||
#ifdef __STDCPP_FLOAT64_T__
|
||||
template <class Policy>
|
||||
BOOST_MATH_GPU_ENABLED inline std::float64_t expm1(std::float64_t x, const Policy& pol)
|
||||
{
|
||||
return boost::math::expm1(static_cast<double>(x), pol);
|
||||
}
|
||||
#endif
|
||||
#ifdef __STDCPP_FLOAT128_T__
|
||||
template <class Policy>
|
||||
BOOST_MATH_GPU_ENABLED inline std::float128_t expm1(std::float128_t x, const Policy& pol)
|
||||
{
|
||||
if constexpr (std::numeric_limits<long double>::digits == std::numeric_limits<std::float128_t>::digits)
|
||||
{
|
||||
return boost::math::expm1(static_cast<long double>(x), pol);
|
||||
}
|
||||
else
|
||||
{
|
||||
return boost::math::detail::expm1_imp(x, boost::math::integral_constant<int, 113>(), pol);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
} // namespace math
|
||||
} // namespace boost
|
||||
|
||||
|
||||
@@ -12,6 +12,14 @@
|
||||
#pragma warning(disable:4702) // Unreachable code (release mode only warning)
|
||||
#endif
|
||||
|
||||
#if defined __has_include
|
||||
# if __cplusplus > 202002L || _MSVC_LANG > 202002L
|
||||
# if __has_include (<stdfloat>)
|
||||
# include <stdfloat>
|
||||
# endif
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#include <boost/math/tools/config.hpp>
|
||||
#include <boost/math/tools/series.hpp>
|
||||
#include <boost/math/tools/rational.hpp>
|
||||
@@ -289,15 +297,6 @@ BOOST_MATH_GPU_ENABLED inline typename tools::promote_args<T>::type log1p(T x, c
|
||||
detail::log1p_imp(static_cast<value_type>(x), forwarding_policy(), tag_type()), "boost::math::log1p<%1%>(%1%)");
|
||||
}
|
||||
|
||||
#ifdef log1p
|
||||
# ifndef BOOST_HAS_LOG1P
|
||||
# define BOOST_HAS_LOG1P
|
||||
# endif
|
||||
# undef log1p
|
||||
#endif
|
||||
|
||||
#if defined(BOOST_HAS_LOG1P) && !(defined(__osf__) && defined(__DECCXX_VER))
|
||||
# ifdef BOOST_MATH_USE_C99
|
||||
template <class Policy>
|
||||
BOOST_MATH_GPU_ENABLED inline float log1p(float x, const Policy& pol)
|
||||
{
|
||||
@@ -307,7 +306,7 @@ BOOST_MATH_GPU_ENABLED inline float log1p(float x, const Policy& pol)
|
||||
if(x == -1)
|
||||
return -policies::raise_overflow_error<float>(
|
||||
"log1p<%1%>(%1%)", nullptr, pol);
|
||||
return ::log1pf(x);
|
||||
return std::log1p(x);
|
||||
}
|
||||
#ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
|
||||
template <class Policy>
|
||||
@@ -319,20 +318,7 @@ BOOST_MATH_GPU_ENABLED inline long double log1p(long double x, const Policy& pol
|
||||
if(x == -1)
|
||||
return -policies::raise_overflow_error<long double>(
|
||||
"log1p<%1%>(%1%)", nullptr, pol);
|
||||
return ::log1pl(x);
|
||||
}
|
||||
#endif
|
||||
#else
|
||||
template <class Policy>
|
||||
inline float log1p(float x, const Policy& pol)
|
||||
{
|
||||
if(x < -1)
|
||||
return policies::raise_domain_error<float>(
|
||||
"log1p<%1%>(%1%)", "log1p(x) requires x > -1, but got x = %1%.", x, pol);
|
||||
if(x == -1)
|
||||
return -policies::raise_overflow_error<float>(
|
||||
"log1p<%1%>(%1%)", nullptr, pol);
|
||||
return ::log1p(x);
|
||||
return std::log1p(x);
|
||||
}
|
||||
#endif
|
||||
template <class Policy>
|
||||
@@ -344,56 +330,8 @@ BOOST_MATH_GPU_ENABLED inline double log1p(double x, const Policy& pol)
|
||||
if(x == -1)
|
||||
return -policies::raise_overflow_error<double>(
|
||||
"log1p<%1%>(%1%)", nullptr, pol);
|
||||
return ::log1p(x);
|
||||
return std::log1p(x);
|
||||
}
|
||||
#elif defined(_MSC_VER) && (BOOST_MSVC >= 1400)
|
||||
//
|
||||
// You should only enable this branch if you are absolutely sure
|
||||
// that your compilers optimizer won't mess this code up!!
|
||||
// Currently tested with VC8 and Intel 9.1.
|
||||
//
|
||||
template <class Policy>
|
||||
inline double log1p(double x, const Policy& pol)
|
||||
{
|
||||
if(x < -1)
|
||||
return policies::raise_domain_error<double>(
|
||||
"log1p<%1%>(%1%)", "log1p(x) requires x > -1, but got x = %1%.", x, pol);
|
||||
if(x == -1)
|
||||
return -policies::raise_overflow_error<double>(
|
||||
"log1p<%1%>(%1%)", nullptr, pol);
|
||||
double u = 1+x;
|
||||
if(u == 1.0)
|
||||
return x;
|
||||
else
|
||||
return ::log(u)*(x/(u-1.0));
|
||||
}
|
||||
template <class Policy>
|
||||
inline float log1p(float x, const Policy& pol)
|
||||
{
|
||||
return static_cast<float>(boost::math::log1p(static_cast<double>(x), pol));
|
||||
}
|
||||
#ifndef _WIN32_WCE
|
||||
//
|
||||
// For some reason this fails to compile under WinCE...
|
||||
// Needs more investigation.
|
||||
//
|
||||
template <class Policy>
|
||||
inline long double log1p(long double x, const Policy& pol)
|
||||
{
|
||||
if(x < -1)
|
||||
return policies::raise_domain_error<long double>(
|
||||
"log1p<%1%>(%1%)", "log1p(x) requires x > -1, but got x = %1%.", x, pol);
|
||||
if(x == -1)
|
||||
return -policies::raise_overflow_error<long double>(
|
||||
"log1p<%1%>(%1%)", nullptr, pol);
|
||||
long double u = 1+x;
|
||||
if(u == 1.0)
|
||||
return x;
|
||||
else
|
||||
return ::logl(u)*(x/(u-1.0));
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
template <class T>
|
||||
BOOST_MATH_GPU_ENABLED inline typename tools::promote_args<T>::type log1p(T x)
|
||||
@@ -441,6 +379,37 @@ BOOST_MATH_GPU_ENABLED inline typename tools::promote_args<T>::type log1pmx(T x)
|
||||
return log1pmx(x, policies::policy<>());
|
||||
}
|
||||
|
||||
//
|
||||
// Specific width floating point types:
|
||||
//
|
||||
#ifdef __STDCPP_FLOAT32_T__
|
||||
template <class Policy>
|
||||
BOOST_MATH_GPU_ENABLED inline std::float32_t log1p(std::float32_t x, const Policy& pol)
|
||||
{
|
||||
return boost::math::log1p(static_cast<float>(x), pol);
|
||||
}
|
||||
#endif
|
||||
#ifdef __STDCPP_FLOAT64_T__
|
||||
template <class Policy>
|
||||
BOOST_MATH_GPU_ENABLED inline std::float64_t log1p(std::float64_t x, const Policy& pol)
|
||||
{
|
||||
return boost::math::log1p(static_cast<double>(x), pol);
|
||||
}
|
||||
#endif
|
||||
#ifdef __STDCPP_FLOAT128_T__
|
||||
template <class Policy>
|
||||
BOOST_MATH_GPU_ENABLED inline std::float128_t log1p(std::float128_t x, const Policy& pol)
|
||||
{
|
||||
if constexpr (std::numeric_limits<long double>::digits == std::numeric_limits<std::float128_t>::digits)
|
||||
{
|
||||
return boost::math::log1p(static_cast<long double>(x), pol);
|
||||
}
|
||||
else
|
||||
{
|
||||
return boost::math::detail::log1p_imp(x, pol, boost::math::integral_constant<int, 113>());
|
||||
}
|
||||
}
|
||||
#endif
|
||||
} // namespace math
|
||||
} // namespace boost
|
||||
|
||||
|
||||
@@ -167,7 +167,7 @@ test_result<typename calculate_result_type<A>::value_type> test(const A& a, F1 t
|
||||
print_row(row, std::cerr);
|
||||
BOOST_ERROR("Unexpected non-finite result");
|
||||
}
|
||||
if(err > 0.5)
|
||||
if(err > 0.5f)
|
||||
{
|
||||
std::cerr << "CAUTION: Gross error found at entry " << i << ".\n";
|
||||
std::cerr << "Found: " << point << " Expected " << expected << " Error: " << err << std::endl;
|
||||
@@ -240,7 +240,7 @@ test_result<Real> test_hetero(const A& a, F1 test_func, F2 expect_func)
|
||||
print_row(row, std::cerr);
|
||||
BOOST_ERROR("Unexpected non-finite result");
|
||||
}
|
||||
if(err > 0.5)
|
||||
if(err > 0.5f)
|
||||
{
|
||||
std::cerr << "CAUTION: Gross error found at entry " << i << ".\n";
|
||||
std::cerr << "Found: " << point << " Expected " << expected << " Error: " << err << std::endl;
|
||||
|
||||
@@ -178,6 +178,8 @@ test-suite special_fun :
|
||||
[ run ccmath_fma_test.cpp /boost/test//boost_unit_test_framework : : : [ requires cxx17_if_constexpr ] $(float128_type) ]
|
||||
[ run ccmath_signbit_test.cpp /boost/test//boost_unit_test_framework : : : [ requires cxx17_if_constexpr ] $(float128_type) ]
|
||||
[ run log1p_expm1_test.cpp test_instances//test_instances pch_light /boost/test//boost_unit_test_framework ]
|
||||
[ run log1p_expm1_extra_test.cpp pch_light /boost/test//boost_unit_test_framework ]
|
||||
[ run log1p_expm1_stdfloat_test.cpp pch_light /boost/test//boost_unit_test_framework ]
|
||||
[ run powm1_sqrtp1m1_test.cpp test_instances//test_instances pch_light /boost/test//boost_unit_test_framework ]
|
||||
[ run git_issue_705.cpp /boost/test//boost_unit_test_framework ]
|
||||
[ run git_issue_810.cpp /boost/test//boost_unit_test_framework ]
|
||||
|
||||
73
test/log1p_expm1_extra_test.cpp
Normal file
73
test/log1p_expm1_extra_test.cpp
Normal file
@@ -0,0 +1,73 @@
|
||||
// Copyright John Maddock 2005.
|
||||
// Copyright Paul A. Bristow 2010
|
||||
// Use, modification and distribution are subject to the
|
||||
// Boost Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#include <pch_light.hpp>
|
||||
|
||||
#define SC_(x) static_cast<T>(BOOST_STRINGIZE(x))
|
||||
|
||||
#define BOOST_MATH_OVERFLOW_ERROR_POLICY ignore_error
|
||||
|
||||
#define BOOST_TEST_MAIN
|
||||
#include <boost/test/unit_test.hpp>
|
||||
#include <boost/test/tools/floating_point_comparison.hpp>
|
||||
#include <boost/math/special_functions/log1p.hpp>
|
||||
#include <boost/math/special_functions/expm1.hpp>
|
||||
#include "log1p_expm1_test.hpp"
|
||||
#include <boost/multiprecision/cpp_bin_float.hpp>
|
||||
|
||||
//
|
||||
// DESCRIPTION:
|
||||
// ~~~~~~~~~~~~
|
||||
//
|
||||
// This file tests the functions log1p and expm1. The accuracy tests
|
||||
// use values generated with NTL::RR at 1000-bit precision
|
||||
// and our generic versions of these functions.
|
||||
//
|
||||
// Note that this tests the generic versions of our functions using
|
||||
// software emulated types. This ensures these still get tested
|
||||
// even though we mostly defer to std::expm1 and std::log1p at
|
||||
// these precisions nowadays
|
||||
//
|
||||
|
||||
void expected_results()
|
||||
{
|
||||
//
|
||||
// Define the max and mean errors expected for
|
||||
// various compilers and platforms.
|
||||
//
|
||||
|
||||
//
|
||||
// Catch all cases come last:
|
||||
//
|
||||
add_expected_result(
|
||||
".*", // compiler
|
||||
".*", // stdlib
|
||||
".*", // platform
|
||||
".*", // test type(s)
|
||||
".*", // test data group
|
||||
".*", // test function
|
||||
4, // Max Peek error
|
||||
3); // Max mean error
|
||||
|
||||
//
|
||||
// Finish off by printing out the compiler/stdlib/platform names,
|
||||
// we do this to make it easier to mark up expected error rates.
|
||||
//
|
||||
std::cout << "Tests run with " << BOOST_COMPILER << ", "
|
||||
<< BOOST_STDLIB << ", " << BOOST_PLATFORM << std::endl;
|
||||
}
|
||||
|
||||
|
||||
BOOST_AUTO_TEST_CASE( test_main )
|
||||
{
|
||||
expected_results();
|
||||
BOOST_MATH_CONTROL_FP;
|
||||
test(boost::multiprecision::cpp_bin_float_single(0), "cpp_bin_float_single");
|
||||
test(boost::multiprecision::cpp_bin_float_double(0), "cpp_bin_float_double");
|
||||
test(boost::multiprecision::cpp_bin_float_double_extended(0), "cpp_bin_float_double_extended");
|
||||
test(boost::multiprecision::cpp_bin_float_quad(0), "cpp_bin_float_quad");
|
||||
}
|
||||
|
||||
87
test/log1p_expm1_stdfloat_test.cpp
Normal file
87
test/log1p_expm1_stdfloat_test.cpp
Normal file
@@ -0,0 +1,87 @@
|
||||
// Copyright John Maddock 2005.
|
||||
// Copyright Paul A. Bristow 2010
|
||||
// Use, modification and distribution are subject to the
|
||||
// Boost Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#include <pch_light.hpp>
|
||||
|
||||
#if defined __has_include
|
||||
# if __cplusplus > 202002L || _MSVC_LANG > 202002L
|
||||
# if __has_include (<stdfloat>)
|
||||
# include <stdfloat>
|
||||
# endif
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#define SC_(x) static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 128, x))
|
||||
|
||||
#define BOOST_MATH_OVERFLOW_ERROR_POLICY ignore_error
|
||||
|
||||
#define BOOST_TEST_MAIN
|
||||
#include <boost/test/unit_test.hpp>
|
||||
#include <boost/test/tools/floating_point_comparison.hpp>
|
||||
#include <boost/math/special_functions/log1p.hpp>
|
||||
#include <boost/math/special_functions/expm1.hpp>
|
||||
#include <boost/math/tools/big_constant.hpp>
|
||||
#include "log1p_expm1_test.hpp"
|
||||
|
||||
//
|
||||
// DESCRIPTION:
|
||||
// ~~~~~~~~~~~~
|
||||
//
|
||||
// This file tests the functions log1p and expm1. The accuracy tests
|
||||
// use values generated with NTL::RR at 1000-bit precision
|
||||
// and our generic versions of these functions.
|
||||
//
|
||||
// Note that this tests the generic versions of our functions using
|
||||
// software emulated types. This ensures these still get tested
|
||||
// even though we mostly defer to std::expm1 and std::log1p at
|
||||
// these precisions nowadays
|
||||
//
|
||||
|
||||
void expected_results()
|
||||
{
|
||||
//
|
||||
// Define the max and mean errors expected for
|
||||
// various compilers and platforms.
|
||||
//
|
||||
|
||||
//
|
||||
// Catch all cases come last:
|
||||
//
|
||||
add_expected_result(
|
||||
".*", // compiler
|
||||
".*", // stdlib
|
||||
".*", // platform
|
||||
".*", // test type(s)
|
||||
".*", // test data group
|
||||
".*", // test function
|
||||
4, // Max Peek error
|
||||
3); // Max mean error
|
||||
|
||||
//
|
||||
// Finish off by printing out the compiler/stdlib/platform names,
|
||||
// we do this to make it easier to mark up expected error rates.
|
||||
//
|
||||
std::cout << "Tests run with " << BOOST_COMPILER << ", "
|
||||
<< BOOST_STDLIB << ", " << BOOST_PLATFORM << std::endl;
|
||||
}
|
||||
|
||||
|
||||
BOOST_AUTO_TEST_CASE( test_main )
|
||||
{
|
||||
expected_results();
|
||||
BOOST_MATH_CONTROL_FP;
|
||||
#ifdef __STDCPP_FLOAT32_T__
|
||||
test(std::float32_t(0), "std::float32_t");
|
||||
#endif
|
||||
#ifdef __STDCPP_FLOAT64_T__
|
||||
test(std::float64_t(0), "std::float64_t");
|
||||
#endif
|
||||
#ifdef __STDCPP_FLOAT128_T__
|
||||
// As of gcc-13 this does not work as float128_t has no std::math function overloads
|
||||
//test(std::float128_t(0), "std::float128_t");
|
||||
#endif
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user