From 77f058a34c171e3811af534a7fa08dcc411bf5a1 Mon Sep 17 00:00:00 2001 From: bolero-MURAKAMI Date: Thu, 5 Jul 2012 00:09:44 +0900 Subject: [PATCH] add floor, ceil, trunc, round --- sprout/cmath.hpp | 1 + sprout/cstdlib/str_to_float.hpp | 1 - sprout/darkroom/materials/texture_map.hpp | 4 +- sprout/detail/math/float.hpp | 8 +-- sprout/math/ceil.hpp | 53 ++++++++++++++++++++ sprout/math/floor.hpp | 53 ++++++++++++++++++++ sprout/math/functions.hpp | 1 + sprout/math/nearest.hpp | 10 ++++ sprout/math/round.hpp | 61 +++++++++++++++++++++++ sprout/math/trunc.hpp | 45 +++++++++++++++++ sprout/random/binomial_distribution.hpp | 6 +-- sprout/random/geometric_distribution.hpp | 4 +- 12 files changed, 236 insertions(+), 11 deletions(-) create mode 100644 sprout/math/ceil.hpp create mode 100644 sprout/math/floor.hpp create mode 100644 sprout/math/nearest.hpp create mode 100644 sprout/math/round.hpp create mode 100644 sprout/math/trunc.hpp diff --git a/sprout/cmath.hpp b/sprout/cmath.hpp index 61fbcbc4..9791dc11 100644 --- a/sprout/cmath.hpp +++ b/sprout/cmath.hpp @@ -8,5 +8,6 @@ #include #include #include +#include #endif // #ifndef SPROUT_CMATH_HPP diff --git a/sprout/cstdlib/str_to_float.hpp b/sprout/cstdlib/str_to_float.hpp index 6f3c2ea9..1e640860 100644 --- a/sprout/cstdlib/str_to_float.hpp +++ b/sprout/cstdlib/str_to_float.hpp @@ -4,7 +4,6 @@ #include #include #include -#include #include #include #include diff --git a/sprout/darkroom/materials/texture_map.hpp b/sprout/darkroom/materials/texture_map.hpp index 86a76f75..d756b4c7 100644 --- a/sprout/darkroom/materials/texture_map.hpp +++ b/sprout/darkroom/materials/texture_map.hpp @@ -3,8 +3,8 @@ #include #include -#include #include +#include #include #include @@ -121,7 +121,7 @@ namespace sprout { } template SPROUT_CONSTEXPR result_type calc_bilinear(Unit const& x, Unit const& y) const { - using std::floor; + using sprout::floor; return calc_bilinear_1(x, floor(x - 0.5), floor(x + 0.5), y, floor(y - 0.5), floor(y + 0.5)); } template diff --git a/sprout/detail/math/float.hpp b/sprout/detail/math/float.hpp index e3730b5f..0096f58e 100644 --- a/sprout/detail/math/float.hpp +++ b/sprout/detail/math/float.hpp @@ -1,9 +1,11 @@ #ifndef SPROUT_DETAIL_FLOAT_HPP #define SPROUT_DETAIL_FLOAT_HPP -#include #include #include +#include +#include +#include #include namespace sprout { @@ -89,7 +91,7 @@ namespace sprout { template::value>::type = sprout::enabler> inline SPROUT_CONSTEXPR int float_digit_of_impl(FloatType val) { - using std::floor; + using sprout::floor; return static_cast((val - floor(val)) * 10); } template @@ -104,7 +106,7 @@ namespace sprout { template::value>::type = sprout::enabler> inline SPROUT_CONSTEXPR FloatType float_round_impl(FloatType val, FloatType p10) { - using std::round; + using sprout::round; return round(val * p10) / p10; } template diff --git a/sprout/math/ceil.hpp b/sprout/math/ceil.hpp new file mode 100644 index 00000000..26b914e7 --- /dev/null +++ b/sprout/math/ceil.hpp @@ -0,0 +1,53 @@ +#ifndef SPROUT_MATH_CEIL_HPP +#define SPROUT_MATH_CEIL_HPP + +#include +#include +#include +#include +#include +#include +#include + +namespace sprout { + namespace math { + namespace detail { + template + inline SPROUT_CONSTEXPR FloatType + ceil_impl(FloatType x, FloatType x0) { + return x - x0 < std::numeric_limits::epsilon() + ? x0 + : x0 + 1 + ; + } + template< + typename FloatType, + typename sprout::enabler_if::value>::type = sprout::enabler + > + inline SPROUT_CONSTEXPR FloatType + ceil(FloatType x) { + return std::numeric_limits::max() < x || std::numeric_limits::max() < -x + ? throw std::domain_error("ceil: Sorry, not implemented.") + : x < 0 + ? -static_cast(static_cast(-x)) + : sprout::math::detail::ceil_impl(x, static_cast(static_cast(x))) + ; + } + + template< + typename IntType, + typename sprout::enabler_if::value>::type = sprout::enabler + > + inline SPROUT_CONSTEXPR double + ceil(IntType x) { + return sprout::math::detail::ceil(static_cast(x)); + } + } // namespacedetailmath + + using NS_SPROUT_MATH_DETAIL::ceil; + } // namespace math + + using sprout::math::ceil; +} // namespace sprout + +#endif // #ifndef SPROUT_MATH_CEIL_HPP diff --git a/sprout/math/floor.hpp b/sprout/math/floor.hpp new file mode 100644 index 00000000..1267e2ef --- /dev/null +++ b/sprout/math/floor.hpp @@ -0,0 +1,53 @@ +#ifndef SPROUT_MATH_FLOOR_HPP +#define SPROUT_MATH_FLOOR_HPP + +#include +#include +#include +#include +#include +#include +#include + +namespace sprout { + namespace math { + namespace detail { + template + inline SPROUT_CONSTEXPR FloatType + floor_impl(FloatType x, FloatType x0) { + return x0 - x < std::numeric_limits::epsilon() + ? x0 + : x0 - 1 + ; + } + template< + typename FloatType, + typename sprout::enabler_if::value>::type = sprout::enabler + > + inline SPROUT_CONSTEXPR FloatType + floor(FloatType x) { + return std::numeric_limits::max() < x || std::numeric_limits::max() < -x + ? throw std::domain_error("floor: Sorry, not implemented.") + : x < 0 + ? sprout::math::detail::floor_impl(x, -static_cast(static_cast(-x))) + : static_cast(static_cast(x)) + ; + } + + template< + typename IntType, + typename sprout::enabler_if::value>::type = sprout::enabler + > + inline SPROUT_CONSTEXPR double + floor(IntType x) { + return sprout::math::detail::floor(static_cast(x)); + } + } // namespacedetailmath + + using NS_SPROUT_MATH_DETAIL::floor; + } // namespace math + + using sprout::math::floor; +} // namespace sprout + +#endif // #ifndef SPROUT_MATH_FLOOR_HPP diff --git a/sprout/math/functions.hpp b/sprout/math/functions.hpp index 1b6bb004..37ab90d0 100644 --- a/sprout/math/functions.hpp +++ b/sprout/math/functions.hpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include diff --git a/sprout/math/nearest.hpp b/sprout/math/nearest.hpp new file mode 100644 index 00000000..9ac91dde --- /dev/null +++ b/sprout/math/nearest.hpp @@ -0,0 +1,10 @@ +#ifndef SPROUT_MATH_NEAREST_HPP +#define SPROUT_MATH_NEAREST_HPP + +#include +#include +#include +#include +#include + +#endif // #ifndef SPROUT_MATH_NEAREST_HPP diff --git a/sprout/math/round.hpp b/sprout/math/round.hpp new file mode 100644 index 00000000..f981e5c5 --- /dev/null +++ b/sprout/math/round.hpp @@ -0,0 +1,61 @@ +#ifndef SPROUT_MATH_ROUND_HPP +#define SPROUT_MATH_ROUND_HPP + +#include +#include +#include +#include +#include +#include +#include + +namespace sprout { + namespace math { + namespace detail { + template + inline SPROUT_CONSTEXPR FloatType + round_impl_positive(FloatType x, FloatType x0) { + return x - x0 < FloatType(0.5) + ? x0 + : x0 + 1 + ; + } + template + inline SPROUT_CONSTEXPR FloatType + round_impl_nagative(FloatType x, FloatType x0) { + return x0 - x < FloatType(0.5) + ? x0 + : x0 - 1 + ; + } + template< + typename FloatType, + typename sprout::enabler_if::value>::type = sprout::enabler + > + inline SPROUT_CONSTEXPR FloatType + round(FloatType x) { + return std::numeric_limits::max() < x || std::numeric_limits::max() < -x + ? throw std::domain_error("round: Sorry, not implemented.") + : x < 0 + ? sprout::math::detail::round_impl_nagative(x, -static_cast(static_cast(-x))) + : sprout::math::detail::round_impl_positive(x, static_cast(static_cast(x))) + ; + } + + template< + typename IntType, + typename sprout::enabler_if::value>::type = sprout::enabler + > + inline SPROUT_CONSTEXPR double + round(IntType x) { + return sprout::math::detail::round(static_cast(x)); + } + } // namespacedetailmath + + using NS_SPROUT_MATH_DETAIL::round; + } // namespace math + + using sprout::math::round; +} // namespace sprout + +#endif // #ifndef SPROUT_MATH_ROUND_HPP diff --git a/sprout/math/trunc.hpp b/sprout/math/trunc.hpp new file mode 100644 index 00000000..99d3ac98 --- /dev/null +++ b/sprout/math/trunc.hpp @@ -0,0 +1,45 @@ +#ifndef SPROUT_MATH_TRUNC_HPP +#define SPROUT_MATH_TRUNC_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 + trunc(FloatType x) { + return std::numeric_limits::max() < x || std::numeric_limits::max() < -x + ? throw std::domain_error("trunc: Sorry, not implemented.") + : x < 0 + ? -static_cast(static_cast(-x)) + : static_cast(static_cast(x)) + ; + } + + template< + typename IntType, + typename sprout::enabler_if::value>::type = sprout::enabler + > + inline SPROUT_CONSTEXPR double + trunc(IntType x) { + return sprout::math::detail::trunc(static_cast(x)); + } + } // namespacedetailmath + + using NS_SPROUT_MATH_DETAIL::trunc; + } // namespace math + + using sprout::math::trunc; +} // namespace sprout + +#endif // #ifndef SPROUT_MATH_TRUNC_HPP diff --git a/sprout/random/binomial_distribution.hpp b/sprout/random/binomial_distribution.hpp index 066e7e6c..6c7a2ae6 100644 --- a/sprout/random/binomial_distribution.hpp +++ b/sprout/random/binomial_distribution.hpp @@ -1,7 +1,6 @@ #ifndef SPROUT_RANDOM_BINOMIAL_DISTRIBUTION_HPP #define SPROUT_RANDOM_BINOMIAL_DISTRIBUTION_HPP -#include #include #include #include @@ -12,6 +11,7 @@ #include #include #include +#include #include #include @@ -307,7 +307,7 @@ namespace sprout { } template SPROUT_CONSTEXPR sprout::random::random_result generate_4(Engine const& eng, RealType v, RealType u, RealType us) const { - using std::floor; + using sprout::floor; return generate_5(eng, v, u, us, static_cast(floor((2 * btrd_.a / us + btrd_.b) * u + btrd_.c))); } template @@ -332,7 +332,7 @@ namespace sprout { } template SPROUT_CONSTEXPR sprout::random::random_result generate_1_1(Engine const& eng, RealType u) const { - using std::floor; + using sprout::floor; using sprout::abs; return sprout::random::random_result( static_cast(floor((2 * btrd_.a / (RealType(0.5) - abs(u)) + btrd_.b) * u + btrd_.c)), diff --git a/sprout/random/geometric_distribution.hpp b/sprout/random/geometric_distribution.hpp index d769bed2..abcfd189 100644 --- a/sprout/random/geometric_distribution.hpp +++ b/sprout/random/geometric_distribution.hpp @@ -1,12 +1,12 @@ #ifndef SPROUT_RANDOM_GEOMETRIC_DISTRIBUTION_HPP #define SPROUT_RANDOM_GEOMETRIC_DISTRIBUTION_HPP -#include #include #include #include #include #include +#include #include #include @@ -98,7 +98,7 @@ namespace sprout { template SPROUT_CONSTEXPR sprout::random::random_result generate_1(Random const& rnd) const { using sprout::log; - using std::floor; + using sprout::floor; return sprout::random::random_result( static_cast(floor(log(RealType(1) - rnd.result()) / log_1mp_)), rnd.engine(),