1
2// Copyright (c) 2011 John Maddock
3// Use, modification and distribution are subject to the
4// Boost Software License, Version 1.0. (See accompanying file
5// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6
7#ifndef BOOST_MATH_TOOLS_BIG_CONSTANT_HPP
8#define BOOST_MATH_TOOLS_BIG_CONSTANT_HPP
9
10#include <boost/math/tools/config.hpp>
11#ifndef BOOST_MATH_STANDALONE
12#include <boost/lexical_cast.hpp>
13#endif
14
15#include <cstdlib>
16#include <type_traits>
17#include <limits>
18
19namespace boost{ namespace math{
20
21namespace tools{
22
23template <class T>
24struct numeric_traits : public std::numeric_limits< T > {};
25
26#ifdef BOOST_MATH_USE_FLOAT128
27typedef __float128 largest_float;
28#define BOOST_MATH_LARGEST_FLOAT_C(x) x##Q
29template <>
30struct numeric_traits<__float128>
31{
32 static const int digits = 113;
33 static const int digits10 = 33;
34 static const int max_exponent = 16384;
35 static const bool is_specialized = true;
36};
37#elif LDBL_DIG > DBL_DIG
38typedef long double largest_float;
39#define BOOST_MATH_LARGEST_FLOAT_C(x) x##L
40#else
41typedef double largest_float;
42#define BOOST_MATH_LARGEST_FLOAT_C(x) x
43#endif
44
45template <class T>
46inline constexpr T make_big_value(largest_float v, const char*, std::true_type const&, std::false_type const&) BOOST_MATH_NOEXCEPT(T)
47{
48 return static_cast<T>(v);
49}
50template <class T>
51inline constexpr T make_big_value(largest_float v, const char*, std::true_type const&, std::true_type const&) BOOST_MATH_NOEXCEPT(T)
52{
53 return static_cast<T>(v);
54}
55#ifndef BOOST_MATH_NO_LEXICAL_CAST
56template <class T>
57inline T make_big_value(largest_float, const char* s, std::false_type const&, std::false_type const&)
58{
59 return boost::lexical_cast<T>(s);
60}
61#else
62template <typename T>
63inline T make_big_value(largest_float, const char* s, std::false_type const&, std::false_type const&)
64{
65 static_assert(sizeof(T) == 0, "Type is unsupported in standalone mode. Please disable and try again.");
66}
67#endif
68template <class T>
69inline constexpr T make_big_value(largest_float, const char* s, std::false_type const&, std::true_type const&) BOOST_MATH_NOEXCEPT(T)
70{
71 return T(s);
72}
73
74//
75// For constants which might fit in a long double (if it's big enough):
76//
77#define BOOST_MATH_BIG_CONSTANT(T, D, x)\
78 boost::math::tools::make_big_value<T>(\
79 BOOST_MATH_LARGEST_FLOAT_C(x), \
80 BOOST_STRINGIZE(x), \
81 std::integral_constant<bool, (std::is_convertible<boost::math::tools::largest_float, T>::value) && \
82 ((D <= boost::math::tools::numeric_traits<boost::math::tools::largest_float>::digits) \
83 || std::is_floating_point<T>::value \
84 || (boost::math::tools::numeric_traits<T>::is_specialized && \
85 (boost::math::tools::numeric_traits<T>::digits10 <= boost::math::tools::numeric_traits<boost::math::tools::largest_float>::digits10))) >(), \
86 std::is_constructible<T, const char*>())
87//
88// For constants too huge for any conceivable long double (and which generate compiler errors if we try and declare them as such):
89//
90#define BOOST_MATH_HUGE_CONSTANT(T, D, x)\
91 boost::math::tools::make_big_value<T>(0.0L, BOOST_STRINGIZE(x), \
92 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)>(), \
93 std::is_constructible<T, const char*>())
94
95}}} // namespaces
96
97#endif
98
99

source code of include/boost/math/tools/big_constant.hpp