From feba220da434417b251fe4ffa3517799b258ca8a Mon Sep 17 00:00:00 2001 From: bolero-MURAKAMI Date: Sat, 5 May 2012 17:35:33 +0900 Subject: [PATCH] add sprout::math::float_promote --- sprout/math/atan2.hpp | 44 ++-------------------------- sprout/math/float_promote.hpp | 55 +++++++++++++++++++++++++++++++++++ sprout/math/pow.hpp | 44 ++-------------------------- 3 files changed, 61 insertions(+), 82 deletions(-) create mode 100644 sprout/math/float_promote.hpp diff --git a/sprout/math/atan2.hpp b/sprout/math/atan2.hpp index 897f68ad..9f37e2d5 100644 --- a/sprout/math/atan2.hpp +++ b/sprout/math/atan2.hpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #if SPROUT_USE_BUILTIN_CMATH_FUNCTION # include @@ -12,45 +13,6 @@ namespace sprout { namespace math { - namespace detail { - template - struct atan2_promoted {}; - template - struct atan2_promoted< - Y, - X, - typename std::enable_if< - std::is_arithmetic::value && std::is_arithmetic::value - && (std::is_same::value || std::is_same::value) - >::type - > { - public: - typedef long double type; - }; - template - struct atan2_promoted< - Y, - X, - typename std::enable_if< - std::is_arithmetic::value && std::is_arithmetic::value - && !(std::is_same::value || std::is_same::value) - >::type - > { - public: - typedef double type; - }; - } // namespace detail - // - // atan2_promoted - // - template - struct atan2_promoted - : public sprout::math::detail::atan2_promoted< - typename std::remove_cv::type, - typename std::remove_cv::type - > - {}; - namespace detail { template< typename FloatType, @@ -71,9 +33,9 @@ namespace sprout { std::is_arithmetic::value && std::is_arithmetic::value >::type = sprout::enabler > - inline SPROUT_CONSTEXPR typename sprout::math::atan2_promoted::type + inline SPROUT_CONSTEXPR typename sprout::math::float_promote::type atan2(ArithmeticType1 y, ArithmeticType2 x) { - typedef typename sprout::math::atan2_promoted::type type; + typedef typename sprout::math::float_promote::type type; return sprout::math::detail::atan2(static_cast(y), static_cast(x)); } } // namespace detail diff --git a/sprout/math/float_promote.hpp b/sprout/math/float_promote.hpp new file mode 100644 index 00000000..5f86f3c9 --- /dev/null +++ b/sprout/math/float_promote.hpp @@ -0,0 +1,55 @@ +#ifndef SPROUT_MATH_FLOAT_PROMOTE_HPP +#define SPROUT_MATH_FLOAT_PROMOTE_HPP + +#include +#include + +namespace sprout { + namespace math { + namespace detail { + template + struct float_promote_impl {}; + template + struct float_promote_impl + : public std::conditional< + (std::is_same::value || std::is_same::value), + long double, + double + > + {}; + template + struct float_promote_impl + : public sprout::math::detail::float_promote_impl< + std::is_arithmetic::value, + typename std::conditional< + (std::is_same::value || std::is_same::value), + long double, + double + >::type, + Tail... + > + {}; + + template + struct float_promote + : public sprout::math::detail::float_promote_impl< + std::is_arithmetic::value, + double, + Head, Tail... + > + {}; + } // namespace detail + + // + // float_promote + // + template + struct float_promote + : public sprout::math::detail::float_promote< + typename std::remove_cv::type... + > + {}; + } // namespace math +} // namespace sprout + +#endif // #ifndef SPROUT_MATH_FLOAT_PROMOTE_HPP diff --git a/sprout/math/pow.hpp b/sprout/math/pow.hpp index 841ec18a..635eb66a 100644 --- a/sprout/math/pow.hpp +++ b/sprout/math/pow.hpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #if SPROUT_USE_BUILTIN_CMATH_FUNCTION # include @@ -13,45 +14,6 @@ namespace sprout { namespace math { - namespace detail { - template - struct pow_promoted {}; - template - struct pow_promoted< - X, - Y, - typename std::enable_if< - std::is_arithmetic::value && std::is_arithmetic::value - && (std::is_same::value || std::is_same::value) - >::type - > { - public: - typedef long double type; - }; - template - struct pow_promoted< - X, - Y, - typename std::enable_if< - std::is_arithmetic::value && std::is_arithmetic::value - && !(std::is_same::value || std::is_same::value) - >::type - > { - public: - typedef double type; - }; - } // namespace detail - // - // pow_promoted - // - template - struct pow_promoted - : public sprout::math::detail::pow_promoted< - typename std::remove_cv::type, - typename std::remove_cv::type - > - {}; - namespace detail { template< typename FloatType, @@ -69,9 +31,9 @@ namespace sprout { std::is_arithmetic::value && std::is_arithmetic::value >::type = sprout::enabler > - inline SPROUT_CONSTEXPR typename sprout::math::pow_promoted::type + inline SPROUT_CONSTEXPR typename sprout::math::float_promote::type pow(ArithmeticType1 x, ArithmeticType2 y) { - typedef typename sprout::math::pow_promoted::type type; + typedef typename sprout::math::float_promote::type type; return sprout::math::detail::pow(static_cast(x), static_cast(y)); } } // namespace detail