]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/math/include/boost/math/special_functions/ulp.hpp
bump version to 12.2.2-pve1
[ceph.git] / ceph / src / boost / libs / math / include / boost / math / special_functions / ulp.hpp
CommitLineData
7c673cae
FG
1// (C) Copyright John Maddock 2015.
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)
5
6#ifndef BOOST_MATH_SPECIAL_ULP_HPP
7#define BOOST_MATH_SPECIAL_ULP_HPP
8
9#ifdef _MSC_VER
10#pragma once
11#endif
12
13#include <boost/math/special_functions/math_fwd.hpp>
14#include <boost/math/policies/error_handling.hpp>
15#include <boost/math/special_functions/fpclassify.hpp>
16#include <boost/math/special_functions/next.hpp>
17
18namespace boost{ namespace math{ namespace detail{
19
20template <class T, class Policy>
21T ulp_imp(const T& val, const Policy& pol)
22{
23 BOOST_MATH_STD_USING
24 int expon;
25 static const char* function = "ulp<%1%>(%1%)";
26
27 int fpclass = (boost::math::fpclassify)(val);
28
29 if(fpclass == (int)FP_NAN)
30 {
31 return policies::raise_domain_error<T>(
32 function,
33 "Argument must be finite, but got %1%", val, pol);
34 }
35 else if((fpclass == (int)FP_INFINITE) || (fabs(val) >= tools::max_value<T>()))
36 {
37 return (val < 0 ? -1 : 1) * policies::raise_overflow_error<T>(function, 0, pol);
38 }
39 else if(fpclass == FP_ZERO)
40 return detail::get_smallest_value<T>();
41 //
42 // This code is almost the same as that for float_next, except for negative integers,
43 // where we preserve the relation ulp(x) == ulp(-x) as does Java:
44 //
45 frexp(fabs(val), &expon);
46 T diff = ldexp(T(1), expon - tools::digits<T>());
47 if(diff == 0)
48 diff = detail::get_smallest_value<T>();
49 return diff;
50}
51
52}
53
54template <class T, class Policy>
55inline typename tools::promote_args<T>::type ulp(const T& val, const Policy& pol)
56{
57 typedef typename tools::promote_args<T>::type result_type;
58 return detail::ulp_imp(static_cast<result_type>(val), pol);
59}
60
61template <class T>
62inline typename tools::promote_args<T>::type ulp(const T& val)
63{
64 return ulp(val, policies::policy<>());
65}
66
67
68}} // namespaces
69
70#endif // BOOST_MATH_SPECIAL_ULP_HPP
71