From 887dba18496cefd33b69e52a343a3ac61c3af23c Mon Sep 17 00:00:00 2001 From: bolero-MURAKAMI Date: Sun, 10 Feb 2013 17:12:31 +0900 Subject: [PATCH] add math: reminder, rem_quo(same as remquo) --- sprout/math/floating_point.hpp | 6 +- sprout/math/fmod.hpp | 1 - .../math/{float_frac_int.hpp => frac_int.hpp} | 22 ++++---- ...t_integer_part.hpp => fractional_part.hpp} | 20 +++---- ...t_fractional_part.hpp => integer_part.hpp} | 20 +++---- sprout/math/operations.hpp | 3 + sprout/math/quotient.hpp | 49 ++++++++++++++++ sprout/math/rem_quo.hpp | 56 +++++++++++++++++++ sprout/math/reminder.hpp | 47 ++++++++++++++++ 9 files changed, 189 insertions(+), 35 deletions(-) rename sprout/math/{float_frac_int.hpp => frac_int.hpp} (59%) rename sprout/math/{float_integer_part.hpp => fractional_part.hpp} (56%) rename sprout/math/{float_fractional_part.hpp => integer_part.hpp} (53%) create mode 100644 sprout/math/quotient.hpp create mode 100644 sprout/math/rem_quo.hpp create mode 100644 sprout/math/reminder.hpp diff --git a/sprout/math/floating_point.hpp b/sprout/math/floating_point.hpp index b0c30602..ece1b32b 100644 --- a/sprout/math/floating_point.hpp +++ b/sprout/math/floating_point.hpp @@ -15,8 +15,8 @@ #include #include #include -#include -#include -#include +#include +#include +#include #endif // #ifndef SPROUT_MATH_FLOATING_POINT_HPP diff --git a/sprout/math/fmod.hpp b/sprout/math/fmod.hpp index 2fe0c4b3..93ce53d5 100644 --- a/sprout/math/fmod.hpp +++ b/sprout/math/fmod.hpp @@ -5,7 +5,6 @@ #include #include #include -#include #include #include #include diff --git a/sprout/math/float_frac_int.hpp b/sprout/math/frac_int.hpp similarity index 59% rename from sprout/math/float_frac_int.hpp rename to sprout/math/frac_int.hpp index 6e0d23f8..b923d196 100644 --- a/sprout/math/float_frac_int.hpp +++ b/sprout/math/frac_int.hpp @@ -1,10 +1,10 @@ -#ifndef SPROUT_MATH_FLOAT_FRAC_INT_HPP -#define SPROUT_MATH_FLOAT_FRAC_INT_HPP +#ifndef SPROUT_MATH_FRAC_INT_HPP +#define SPROUT_MATH_FRAC_INT_HPP #include #include #include -#include +#include #include #include @@ -13,7 +13,7 @@ namespace sprout { namespace detail { template inline SPROUT_CONSTEXPR sprout::pair - float_frac_int_impl(T x, T ipart) { + frac_int_impl(T x, T ipart) { return sprout::pair(x - ipart, ipart); } @@ -22,8 +22,8 @@ namespace sprout { typename sprout::enabler_if::value>::type = sprout::enabler > inline SPROUT_CONSTEXPR sprout::pair - float_frac_int(FloatType x) { - return sprout::math::detail::float_frac_int_impl(x, sprout::math::float_integer_part(x)); + frac_int(FloatType x) { + return sprout::math::detail::frac_int_impl(x, sprout::math::integer_part(x)); } template< @@ -31,15 +31,15 @@ namespace sprout { typename sprout::enabler_if::value>::type = sprout::enabler > inline SPROUT_CONSTEXPR sprout::pair - float_frac_int(IntType x) { - return sprout::math::detail::float_frac_int(static_cast(x)); + frac_int(IntType x) { + return sprout::math::detail::frac_int(static_cast(x)); } } // namespace detail - using sprout::math::detail::float_frac_int; + using sprout::math::detail::frac_int; } // namespace math - using sprout::math::float_frac_int; + using sprout::math::frac_int; } // namespace sprout -#endif // #ifndef SPROUT_MATH_FLOAT_FRAC_INT_HPP +#endif // #ifndef SPROUT_MATH_FRAC_INT_HPP diff --git a/sprout/math/float_integer_part.hpp b/sprout/math/fractional_part.hpp similarity index 56% rename from sprout/math/float_integer_part.hpp rename to sprout/math/fractional_part.hpp index efd08471..f966e0e3 100644 --- a/sprout/math/float_integer_part.hpp +++ b/sprout/math/fractional_part.hpp @@ -1,10 +1,10 @@ -#ifndef SPROUT_MATH_FLOAT_INTEGER_PART_HPP -#define SPROUT_MATH_FLOAT_INTEGER_PART_HPP +#ifndef SPROUT_MATH_FRACTIONAL_PART_HPP +#define SPROUT_MATH_FRACTIONAL_PART_HPP #include #include #include -#include +#include #include namespace sprout { @@ -15,8 +15,8 @@ namespace sprout { typename sprout::enabler_if::value>::type = sprout::enabler > inline SPROUT_CONSTEXPR FloatType - float_integer_part(FloatType x) { - return sprout::math::floor(x); + fractional_part(FloatType x) { + return x - sprout::math::integer_part(x); } template< @@ -24,15 +24,15 @@ namespace sprout { typename sprout::enabler_if::value>::type = sprout::enabler > inline SPROUT_CONSTEXPR double - float_integer_part(IntType x) { - return sprout::math::detail::float_integer_part(static_cast(x)); + fractional_part(IntType x) { + return sprout::math::detail::fractional_part(static_cast(x)); } } // namespace detail - using sprout::math::detail::float_integer_part; + using sprout::math::detail::fractional_part; } // namespace math - using sprout::math::float_integer_part; + using sprout::math::fractional_part; } // namespace sprout -#endif // #ifndef SPROUT_MATH_FLOAT_INTEGER_PART_HPP +#endif // #ifndef SPROUT_MATH_FRACTIONAL_PART_HPP diff --git a/sprout/math/float_fractional_part.hpp b/sprout/math/integer_part.hpp similarity index 53% rename from sprout/math/float_fractional_part.hpp rename to sprout/math/integer_part.hpp index f1596cd7..f8bf9962 100644 --- a/sprout/math/float_fractional_part.hpp +++ b/sprout/math/integer_part.hpp @@ -1,10 +1,10 @@ -#ifndef SPROUT_MATH_FLOAT_FRACTIONAL_PART_HPP -#define SPROUT_MATH_FLOAT_FRACTIONAL_PART_HPP +#ifndef SPROUT_MATH_INTEGER_PART_HPP +#define SPROUT_MATH_INTEGER_PART_HPP #include #include #include -#include +#include #include namespace sprout { @@ -15,8 +15,8 @@ namespace sprout { typename sprout::enabler_if::value>::type = sprout::enabler > inline SPROUT_CONSTEXPR FloatType - float_fractional_part(FloatType x) { - return x - sprout::math::float_integer_part(x); + integer_part(FloatType x) { + return sprout::math::trunc(x); } template< @@ -24,15 +24,15 @@ namespace sprout { typename sprout::enabler_if::value>::type = sprout::enabler > inline SPROUT_CONSTEXPR double - float_fractional_part(IntType x) { - return sprout::math::detail::float_fractional_part(static_cast(x)); + integer_part(IntType x) { + return sprout::math::detail::integer_part(static_cast(x)); } } // namespace detail - using sprout::math::detail::float_fractional_part; + using sprout::math::detail::integer_part; } // namespace math - using sprout::math::float_fractional_part; + using sprout::math::integer_part; } // namespace sprout -#endif // #ifndef SPROUT_MATH_FLOAT_FRACTIONAL_PART_HPP +#endif // #ifndef SPROUT_MATH_INTEGER_PART_HPP diff --git a/sprout/math/operations.hpp b/sprout/math/operations.hpp index dd2c4e2e..085233ac 100644 --- a/sprout/math/operations.hpp +++ b/sprout/math/operations.hpp @@ -9,5 +9,8 @@ #include #include #include +#include +#include +#include #endif // #ifndef SPROUT_MATH_OPERATIONS_HPP diff --git a/sprout/math/quotient.hpp b/sprout/math/quotient.hpp new file mode 100644 index 00000000..5cfe4358 --- /dev/null +++ b/sprout/math/quotient.hpp @@ -0,0 +1,49 @@ +#ifndef SPROUT_MATH_QUOTIENT_HPP +#define SPROUT_MATH_QUOTIENT_HPP + +#include +#include +#include +#include +#include +#include +#include +#include + +namespace sprout { + namespace math { + namespace detail { + template< + typename R = int, + typename FloatType, + typename sprout::enabler_if::value && std::is_integral::value>::type = sprout::enabler + > + inline SPROUT_CONSTEXPR R + quotient(FloatType x, FloatType y) { + return y == 0 ? throw std::domain_error("quotient: divide by zero.") + : sprout::math::iround(x / y) + ; + } + + template< + typename R = int, + typename ArithmeticType1, + typename ArithmeticType2, + typename sprout::enabler_if< + std::is_arithmetic::value && std::is_arithmetic::value && std::is_integral::value + >::type = sprout::enabler + > + inline SPROUT_CONSTEXPR R + quotient(ArithmeticType1 x, ArithmeticType2 y) { + typedef typename sprout::float_promote::type type; + return sprout::math::detail::quotient(static_cast(x), static_cast(y)); + } + } // namespace detail + + using sprout::math::detail::quotient; + } // namespace math + + using sprout::math::quotient; +} // namespace sprout + +#endif // #ifndef SPROUT_MATH_QUOTIENT_HPP diff --git a/sprout/math/rem_quo.hpp b/sprout/math/rem_quo.hpp new file mode 100644 index 00000000..63381ccd --- /dev/null +++ b/sprout/math/rem_quo.hpp @@ -0,0 +1,56 @@ +#ifndef SPROUT_MATH_REM_QUO_HPP +#define SPROUT_MATH_REM_QUO_HPP + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace sprout { + namespace math { + namespace detail { + template + inline SPROUT_CONSTEXPR sprout::pair + rem_quo_impl(T x, T y, R quo) { + return sprout::pair(x - quo * y, quo); + } + template< + typename R = int, + typename FloatType, + typename sprout::enabler_if::value && std::is_integral::value>::type = sprout::enabler + > + inline SPROUT_CONSTEXPR sprout::pair + rem_quo(FloatType x, FloatType y) { + return sprout::math::detail::rem_quo_impl(x, y, sprout::math::quotient(x, y)); + } + + template< + typename R = int, + typename ArithmeticType1, + typename ArithmeticType2, + typename sprout::enabler_if< + std::is_arithmetic::value && std::is_arithmetic::value && std::is_integral::value + >::type = sprout::enabler + > + inline SPROUT_CONSTEXPR sprout::pair< + typename sprout::float_promote::type, + R + > + rem_quo(ArithmeticType1 x, ArithmeticType2 y) { + typedef typename sprout::float_promote::type type; + return sprout::math::detail::rem_quo(static_cast(x), static_cast(y)); + } + } // namespace detail + + using sprout::math::detail::rem_quo; + } // namespace math + + using sprout::math::rem_quo; +} // namespace sprout + +#endif // #ifndef SPROUT_MATH_REM_QUO_HPP diff --git a/sprout/math/reminder.hpp b/sprout/math/reminder.hpp new file mode 100644 index 00000000..56f177f4 --- /dev/null +++ b/sprout/math/reminder.hpp @@ -0,0 +1,47 @@ +#ifndef SPROUT_MATH_REMAINDER_HPP +#define SPROUT_MATH_REMAINDER_HPP + +#include +#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 + remainder(FloatType x, FloatType y) { + return y == 0 ? throw std::domain_error("remainder: divide by zero.") + : x - sprout::math::round(x / y) * y + ; + } + + template< + typename ArithmeticType1, + typename ArithmeticType2, + typename sprout::enabler_if< + std::is_arithmetic::value && std::is_arithmetic::value + >::type = sprout::enabler + > + inline SPROUT_CONSTEXPR typename sprout::float_promote::type + remainder(ArithmeticType1 x, ArithmeticType2 y) { + typedef typename sprout::float_promote::type type; + return sprout::math::detail::remainder(static_cast(x), static_cast(y)); + } + } // namespace detail + + using NS_SPROUT_MATH_DETAIL::remainder; + } // namespace math + + using sprout::math::remainder; +} // namespace sprout + +#endif // #ifndef SPROUT_MATH_REMAINDER_HPP