From 63ef07d124c1c703dfd764c408a2fe20587d9d4d Mon Sep 17 00:00:00 2001 From: bolero-MURAKAMI Date: Thu, 7 Feb 2013 19:26:24 +0900 Subject: [PATCH] add floating point functions: float_pair, float_exponent, float_mantissa --- sprout/math/float_exponent.hpp | 38 +++++++++++++++++++++++++++ sprout/math/float_mantissa.hpp | 40 +++++++++++++++++++++++++++++ sprout/math/float_pair.hpp | 47 ++++++++++++++++++++++++++++++++++ sprout/math/floating_point.hpp | 3 +++ 4 files changed, 128 insertions(+) create mode 100644 sprout/math/float_exponent.hpp create mode 100644 sprout/math/float_mantissa.hpp create mode 100644 sprout/math/float_pair.hpp diff --git a/sprout/math/float_exponent.hpp b/sprout/math/float_exponent.hpp new file mode 100644 index 00000000..5dbe9455 --- /dev/null +++ b/sprout/math/float_exponent.hpp @@ -0,0 +1,38 @@ +#ifndef SPROUT_MATH_FLOAT_EXPONENT_HPP +#define SPROUT_MATH_FLOAT_EXPONENT_HPP + +#include +#include +#include +#include +#include + +namespace sprout { + namespace math { + namespace detail { + template< + typename FloatType, + typename sprout::enabler_if::value>::type = sprout::enabler + > + inline SPROUT_CONSTEXPR int + float_exponent(FloatType x) { + return sprout::math::ilogb(x) + 1; + } + + template< + typename IntType, + typename sprout::enabler_if::value>::type = sprout::enabler + > + inline SPROUT_CONSTEXPR int + float_exponent(IntType x) { + return sprout::math::detail::float_exponent(static_cast(x)); + } + } // namespace detail + + using sprout::math::detail::float_exponent; + } // namespace math + + using sprout::math::float_exponent; +} // namespace sprout + +#endif // #ifndef SPROUT_MATH_FLOAT_EXPONENT_HPP diff --git a/sprout/math/float_mantissa.hpp b/sprout/math/float_mantissa.hpp new file mode 100644 index 00000000..d4f692ff --- /dev/null +++ b/sprout/math/float_mantissa.hpp @@ -0,0 +1,40 @@ +#ifndef SPROUT_MATH_FLOAT_MANTISSA_HPP +#define SPROUT_MATH_FLOAT_MANTISSA_HPP + +#include +#include +#include +#include +#include +#include +#include + +namespace sprout { + namespace math { + namespace detail { + template< + typename FloatType, + typename sprout::enabler_if::value>::type = sprout::enabler + > + inline SPROUT_CONSTEXPR FloatType + float_mantissa(FloatType x) { + return x / sprout::detail::pow_n(FloatType(FLT_RADIX), sprout::math::float_exponent(x)); + } + + template< + typename IntType, + typename sprout::enabler_if::value>::type = sprout::enabler + > + inline SPROUT_CONSTEXPR double + float_mantissa(IntType x) { + return sprout::math::detail::float_mantissa(static_cast(x)); + } + } // namespace detail + + using sprout::math::detail::float_mantissa; + } // namespace math + + using sprout::math::float_mantissa; +} // namespace sprout + +#endif // #ifndef SPROUT_MATH_FLOAT_MANTISSA_HPP diff --git a/sprout/math/float_pair.hpp b/sprout/math/float_pair.hpp new file mode 100644 index 00000000..cb57cb1e --- /dev/null +++ b/sprout/math/float_pair.hpp @@ -0,0 +1,47 @@ +#ifndef SPROUT_MATH_FLOAT_PAIR_HPP +#define SPROUT_MATH_FLOAT_PAIR_HPP + +#include +#include +#include +#include +#include +#include +#include +#include + +namespace sprout { + namespace math { + namespace detail { + template + inline SPROUT_CONSTEXPR sprout::pair + float_pair_impl(T x, int exp) { + return sprout::pair(x / sprout::detail::pow_n(T(FLT_RADIX), exp), exp); + } + + template< + typename FloatType, + typename sprout::enabler_if::value>::type = sprout::enabler + > + inline SPROUT_CONSTEXPR sprout::pair + float_pair(FloatType x) { + return sprout::math::detail::float_pair_impl(x, sprout::math::float_exponent(x)); + } + + template< + typename IntType, + typename sprout::enabler_if::value>::type = sprout::enabler + > + inline SPROUT_CONSTEXPR sprout::pair + float_pair(IntType x) { + return sprout::math::detail::float_pair(static_cast(x)); + } + } // namespace detail + + using sprout::math::detail::float_pair; + } // namespace math + + using sprout::math::float_pair; +} // namespace sprout + +#endif // #ifndef SPROUT_MATH_FLOAT_PAIR_HPP diff --git a/sprout/math/floating_point.hpp b/sprout/math/floating_point.hpp index 72e97d35..524e7552 100644 --- a/sprout/math/floating_point.hpp +++ b/sprout/math/floating_point.hpp @@ -7,5 +7,8 @@ #include #include #include +#include +#include +#include #endif // #ifndef SPROUT_MATH_FLOATING_POINT_HPP