90 lines
3.3 KiB
C++
90 lines
3.3 KiB
C++
|
|
// Copyright (c) 2011 John Maddock
|
|
// 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)
|
|
|
|
#ifndef ACTIV_BOOST_MATH_TOOLS_BIG_CONSTANT_HPP
|
|
#define ACTIV_BOOST_MATH_TOOLS_BIG_CONSTANT_HPP
|
|
|
|
#include <ActivBoost/math/tools/config.hpp>
|
|
#include <ActivBoost/math/tools/lexical_cast.hpp>
|
|
|
|
#include <type_traits>
|
|
#include <limits>
|
|
|
|
namespace Boost{ namespace math{
|
|
|
|
namespace tools{
|
|
|
|
template <class T>
|
|
struct numeric_traits : public std::numeric_limits< T > {};
|
|
|
|
#ifdef ACTIV_BOOST_MATH_USE_FLOAT128
|
|
typedef __float128 largest_float;
|
|
#define ACTIV_BOOST_MATH_LARGEST_FLOAT_C(x) x##Q
|
|
template <>
|
|
struct numeric_traits<__float128>
|
|
{
|
|
static const int digits = 113;
|
|
static const int digits10 = 33;
|
|
static const int max_exponent = 16384;
|
|
static const bool is_specialized = true;
|
|
};
|
|
#elif LDBL_DIG > DBL_DIG
|
|
typedef long double largest_float;
|
|
#define ACTIV_BOOST_MATH_LARGEST_FLOAT_C(x) x##L
|
|
#else
|
|
typedef double largest_float;
|
|
#define ACTIV_BOOST_MATH_LARGEST_FLOAT_C(x) x
|
|
#endif
|
|
|
|
template <class T>
|
|
inline constexpr T make_big_value(largest_float v, const char*, std::true_type const&, std::false_type const&) ACTIV_BOOST_MATH_NOEXCEPT(T)
|
|
{
|
|
return static_cast<T>(v);
|
|
}
|
|
template <class T>
|
|
inline constexpr T make_big_value(largest_float v, const char*, std::true_type const&, std::true_type const&) ACTIV_BOOST_MATH_NOEXCEPT(T)
|
|
{
|
|
return static_cast<T>(v);
|
|
}
|
|
#ifndef ACTIV_BOOST_MATH_NO_LEXICAL_CAST
|
|
template <class T>
|
|
inline T make_big_value(largest_float, const char* s, std::false_type const&, std::false_type const&)
|
|
{
|
|
return Boost::lexical_cast<T>(s);
|
|
}
|
|
#endif
|
|
template <class T>
|
|
inline constexpr T make_big_value(largest_float, const char* s, std::false_type const&, std::true_type const&) ACTIV_BOOST_MATH_NOEXCEPT(T)
|
|
{
|
|
return T(s);
|
|
}
|
|
|
|
//
|
|
// For constants which might fit in a long double (if it's big enough):
|
|
//
|
|
#define ACTIV_BOOST_MATH_BIG_CONSTANT(T, D, x)\
|
|
Boost::math::tools::make_big_value<T>(\
|
|
ACTIV_BOOST_MATH_LARGEST_FLOAT_C(x), \
|
|
ACTIV_BOOST_STRINGIZE(x), \
|
|
std::integral_constant<bool, (std::is_convertible<Boost::math::tools::largest_float, T>::value) && \
|
|
((D <= Boost::math::tools::numeric_traits<Boost::math::tools::largest_float>::digits) \
|
|
|| std::is_floating_point<T>::value \
|
|
|| (Boost::math::tools::numeric_traits<T>::is_specialized && \
|
|
(Boost::math::tools::numeric_traits<T>::digits10 <= Boost::math::tools::numeric_traits<Boost::math::tools::largest_float>::digits10))) >(), \
|
|
std::is_constructible<T, const char*>())
|
|
//
|
|
// For constants too huge for any conceivable long double (and which generate compiler errors if we try and declare them as such):
|
|
//
|
|
#define ACTIV_BOOST_MATH_HUGE_CONSTANT(T, D, x)\
|
|
Boost::math::tools::make_big_value<T>(0.0L, ACTIV_BOOST_STRINGIZE(x), \
|
|
std::integral_constant<bool, std::is_floating_point<T>::value || (Boost::math::tools::numeric_traits<T>::is_specialized && Boost::math::tools::numeric_traits<T>::max_exponent <= Boost::math::tools::numeric_traits<Boost::math::tools::largest_float>::max_exponent && Boost::math::tools::numeric_traits<T>::digits <= Boost::math::tools::numeric_traits<Boost::math::tools::largest_float>::digits)>(), \
|
|
std::is_constructible<T, const char*>())
|
|
|
|
}}} // namespaces
|
|
|
|
#endif
|
|
|