add floor, ceil, trunc, round

This commit is contained in:
bolero-MURAKAMI 2012-07-05 00:09:44 +09:00
parent c6fb8af0c5
commit 77f058a34c
12 changed files with 236 additions and 11 deletions

View file

@ -8,5 +8,6 @@
#include <sprout/math/exponential.hpp> #include <sprout/math/exponential.hpp>
#include <sprout/math/power.hpp> #include <sprout/math/power.hpp>
#include <sprout/math/operations.hpp> #include <sprout/math/operations.hpp>
#include <sprout/math/nearest.hpp>
#endif // #ifndef SPROUT_CMATH_HPP #endif // #ifndef SPROUT_CMATH_HPP

View file

@ -4,7 +4,6 @@
#include <cstddef> #include <cstddef>
#include <cstdlib> #include <cstdlib>
#include <limits> #include <limits>
#include <cmath>
#include <type_traits> #include <type_traits>
#include <sprout/config.hpp> #include <sprout/config.hpp>
#include <sprout/iterator/operation.hpp> #include <sprout/iterator/operation.hpp>

View file

@ -3,8 +3,8 @@
#include <cstddef> #include <cstddef>
#include <cstdint> #include <cstdint>
#include <cmath>
#include <sprout/config.hpp> #include <sprout/config.hpp>
#include <sprout/math/floor.hpp>
#include <sprout/utility/value_holder.hpp> #include <sprout/utility/value_holder.hpp>
#include <sprout/darkroom/colors/rgb.hpp> #include <sprout/darkroom/colors/rgb.hpp>
@ -121,7 +121,7 @@ namespace sprout {
} }
template<typename Unit> template<typename Unit>
SPROUT_CONSTEXPR result_type calc_bilinear(Unit const& x, Unit const& y) const { 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)); return calc_bilinear_1(x, floor(x - 0.5), floor(x + 0.5), y, floor(y - 0.5), floor(y + 0.5));
} }
template<typename Unit> template<typename Unit>

View file

@ -1,9 +1,11 @@
#ifndef SPROUT_DETAIL_FLOAT_HPP #ifndef SPROUT_DETAIL_FLOAT_HPP
#define SPROUT_DETAIL_FLOAT_HPP #define SPROUT_DETAIL_FLOAT_HPP
#include <cmath>
#include <type_traits> #include <type_traits>
#include <sprout/config.hpp> #include <sprout/config.hpp>
#include <sprout/math/abs.hpp>
#include <sprout/math/floor.hpp>
#include <sprout/math/round.hpp>
#include <sprout/type_traits/enabler_if.hpp> #include <sprout/type_traits/enabler_if.hpp>
namespace sprout { namespace sprout {
@ -89,7 +91,7 @@ namespace sprout {
template<typename FloatType, typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler> template<typename FloatType, typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler>
inline SPROUT_CONSTEXPR int inline SPROUT_CONSTEXPR int
float_digit_of_impl(FloatType val) { float_digit_of_impl(FloatType val) {
using std::floor; using sprout::floor;
return static_cast<int>((val - floor(val)) * 10); return static_cast<int>((val - floor(val)) * 10);
} }
template<typename FloatType> template<typename FloatType>
@ -104,7 +106,7 @@ namespace sprout {
template<typename FloatType, typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler> template<typename FloatType, typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler>
inline SPROUT_CONSTEXPR FloatType inline SPROUT_CONSTEXPR FloatType
float_round_impl(FloatType val, FloatType p10) { float_round_impl(FloatType val, FloatType p10) {
using std::round; using sprout::round;
return round(val * p10) / p10; return round(val * p10) / p10;
} }
template<typename FloatType> template<typename FloatType>

53
sprout/math/ceil.hpp Normal file
View file

@ -0,0 +1,53 @@
#ifndef SPROUT_MATH_CEIL_HPP
#define SPROUT_MATH_CEIL_HPP
#include <cstdint>
#include <limits>
#include <type_traits>
#include <stdexcept>
#include <sprout/config.hpp>
#include <sprout/math/detail/config.hpp>
#include <sprout/type_traits/enabler_if.hpp>
namespace sprout {
namespace math {
namespace detail {
template<typename FloatType>
inline SPROUT_CONSTEXPR FloatType
ceil_impl(FloatType x, FloatType x0) {
return x - x0 < std::numeric_limits<FloatType>::epsilon()
? x0
: x0 + 1
;
}
template<
typename FloatType,
typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR FloatType
ceil(FloatType x) {
return std::numeric_limits<std::uintmax_t>::max() < x || std::numeric_limits<std::uintmax_t>::max() < -x
? throw std::domain_error("ceil: Sorry, not implemented.")
: x < 0
? -static_cast<FloatType>(static_cast<std::uintmax_t>(-x))
: sprout::math::detail::ceil_impl(x, static_cast<FloatType>(static_cast<std::uintmax_t>(x)))
;
}
template<
typename IntType,
typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR double
ceil(IntType x) {
return sprout::math::detail::ceil(static_cast<double>(x));
}
} // namespacedetailmath
using NS_SPROUT_MATH_DETAIL::ceil;
} // namespace math
using sprout::math::ceil;
} // namespace sprout
#endif // #ifndef SPROUT_MATH_CEIL_HPP

53
sprout/math/floor.hpp Normal file
View file

@ -0,0 +1,53 @@
#ifndef SPROUT_MATH_FLOOR_HPP
#define SPROUT_MATH_FLOOR_HPP
#include <cstdint>
#include <limits>
#include <type_traits>
#include <stdexcept>
#include <sprout/config.hpp>
#include <sprout/math/detail/config.hpp>
#include <sprout/type_traits/enabler_if.hpp>
namespace sprout {
namespace math {
namespace detail {
template<typename FloatType>
inline SPROUT_CONSTEXPR FloatType
floor_impl(FloatType x, FloatType x0) {
return x0 - x < std::numeric_limits<FloatType>::epsilon()
? x0
: x0 - 1
;
}
template<
typename FloatType,
typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR FloatType
floor(FloatType x) {
return std::numeric_limits<std::uintmax_t>::max() < x || std::numeric_limits<std::uintmax_t>::max() < -x
? throw std::domain_error("floor: Sorry, not implemented.")
: x < 0
? sprout::math::detail::floor_impl(x, -static_cast<FloatType>(static_cast<std::uintmax_t>(-x)))
: static_cast<FloatType>(static_cast<std::uintmax_t>(x))
;
}
template<
typename IntType,
typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR double
floor(IntType x) {
return sprout::math::detail::floor(static_cast<double>(x));
}
} // namespacedetailmath
using NS_SPROUT_MATH_DETAIL::floor;
} // namespace math
using sprout::math::floor;
} // namespace sprout
#endif // #ifndef SPROUT_MATH_FLOOR_HPP

View file

@ -8,6 +8,7 @@
#include <sprout/math/exponential.hpp> #include <sprout/math/exponential.hpp>
#include <sprout/math/power.hpp> #include <sprout/math/power.hpp>
#include <sprout/math/operations.hpp> #include <sprout/math/operations.hpp>
#include <sprout/math/nearest.hpp>
#include <sprout/math/common_factor.hpp> #include <sprout/math/common_factor.hpp>
#include <sprout/math/factorial.hpp> #include <sprout/math/factorial.hpp>
#include <sprout/math/bernoulli.hpp> #include <sprout/math/bernoulli.hpp>

10
sprout/math/nearest.hpp Normal file
View file

@ -0,0 +1,10 @@
#ifndef SPROUT_MATH_NEAREST_HPP
#define SPROUT_MATH_NEAREST_HPP
#include <sprout/config.hpp>
#include <sprout/math/ceil.hpp>
#include <sprout/math/floor.hpp>
#include <sprout/math/trunc.hpp>
#include <sprout/math/round.hpp>
#endif // #ifndef SPROUT_MATH_NEAREST_HPP

61
sprout/math/round.hpp Normal file
View file

@ -0,0 +1,61 @@
#ifndef SPROUT_MATH_ROUND_HPP
#define SPROUT_MATH_ROUND_HPP
#include <cstdint>
#include <limits>
#include <type_traits>
#include <stdexcept>
#include <sprout/config.hpp>
#include <sprout/math/detail/config.hpp>
#include <sprout/type_traits/enabler_if.hpp>
namespace sprout {
namespace math {
namespace detail {
template<typename FloatType>
inline SPROUT_CONSTEXPR FloatType
round_impl_positive(FloatType x, FloatType x0) {
return x - x0 < FloatType(0.5)
? x0
: x0 + 1
;
}
template<typename FloatType>
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<std::is_floating_point<FloatType>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR FloatType
round(FloatType x) {
return std::numeric_limits<std::uintmax_t>::max() < x || std::numeric_limits<std::uintmax_t>::max() < -x
? throw std::domain_error("round: Sorry, not implemented.")
: x < 0
? sprout::math::detail::round_impl_nagative(x, -static_cast<FloatType>(static_cast<std::uintmax_t>(-x)))
: sprout::math::detail::round_impl_positive(x, static_cast<FloatType>(static_cast<std::uintmax_t>(x)))
;
}
template<
typename IntType,
typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR double
round(IntType x) {
return sprout::math::detail::round(static_cast<double>(x));
}
} // namespacedetailmath
using NS_SPROUT_MATH_DETAIL::round;
} // namespace math
using sprout::math::round;
} // namespace sprout
#endif // #ifndef SPROUT_MATH_ROUND_HPP

45
sprout/math/trunc.hpp Normal file
View file

@ -0,0 +1,45 @@
#ifndef SPROUT_MATH_TRUNC_HPP
#define SPROUT_MATH_TRUNC_HPP
#include <cstdint>
#include <limits>
#include <type_traits>
#include <stdexcept>
#include <sprout/config.hpp>
#include <sprout/math/detail/config.hpp>
#include <sprout/type_traits/enabler_if.hpp>
namespace sprout {
namespace math {
namespace detail {
template<
typename FloatType,
typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR FloatType
trunc(FloatType x) {
return std::numeric_limits<std::uintmax_t>::max() < x || std::numeric_limits<std::uintmax_t>::max() < -x
? throw std::domain_error("trunc: Sorry, not implemented.")
: x < 0
? -static_cast<FloatType>(static_cast<std::uintmax_t>(-x))
: static_cast<FloatType>(static_cast<std::uintmax_t>(x))
;
}
template<
typename IntType,
typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR double
trunc(IntType x) {
return sprout::math::detail::trunc(static_cast<double>(x));
}
} // namespacedetailmath
using NS_SPROUT_MATH_DETAIL::trunc;
} // namespace math
using sprout::math::trunc;
} // namespace sprout
#endif // #ifndef SPROUT_MATH_TRUNC_HPP

View file

@ -1,7 +1,6 @@
#ifndef SPROUT_RANDOM_BINOMIAL_DISTRIBUTION_HPP #ifndef SPROUT_RANDOM_BINOMIAL_DISTRIBUTION_HPP
#define SPROUT_RANDOM_BINOMIAL_DISTRIBUTION_HPP #define SPROUT_RANDOM_BINOMIAL_DISTRIBUTION_HPP
#include <cmath>
#include <iosfwd> #include <iosfwd>
#include <istream> #include <istream>
#include <stdexcept> #include <stdexcept>
@ -12,6 +11,7 @@
#include <sprout/math/log.hpp> #include <sprout/math/log.hpp>
#include <sprout/math/pow.hpp> #include <sprout/math/pow.hpp>
#include <sprout/math/sqrt.hpp> #include <sprout/math/sqrt.hpp>
#include <sprout/math/floor.hpp>
#include <sprout/random/random_result.hpp> #include <sprout/random/random_result.hpp>
#include <sprout/random/uniform_01.hpp> #include <sprout/random/uniform_01.hpp>
@ -307,7 +307,7 @@ namespace sprout {
} }
template<typename Engine> template<typename Engine>
SPROUT_CONSTEXPR sprout::random::random_result<Engine, binomial_distribution> generate_4(Engine const& eng, RealType v, RealType u, RealType us) const { SPROUT_CONSTEXPR sprout::random::random_result<Engine, binomial_distribution> 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<IntType>(floor((2 * btrd_.a / us + btrd_.b) * u + btrd_.c))); return generate_5(eng, v, u, us, static_cast<IntType>(floor((2 * btrd_.a / us + btrd_.b) * u + btrd_.c)));
} }
template<typename Engine> template<typename Engine>
@ -332,7 +332,7 @@ namespace sprout {
} }
template<typename Engine> template<typename Engine>
SPROUT_CONSTEXPR sprout::random::random_result<Engine, binomial_distribution> generate_1_1(Engine const& eng, RealType u) const { SPROUT_CONSTEXPR sprout::random::random_result<Engine, binomial_distribution> generate_1_1(Engine const& eng, RealType u) const {
using std::floor; using sprout::floor;
using sprout::abs; using sprout::abs;
return sprout::random::random_result<Engine, binomial_distribution>( return sprout::random::random_result<Engine, binomial_distribution>(
static_cast<IntType>(floor((2 * btrd_.a / (RealType(0.5) - abs(u)) + btrd_.b) * u + btrd_.c)), static_cast<IntType>(floor((2 * btrd_.a / (RealType(0.5) - abs(u)) + btrd_.b) * u + btrd_.c)),

View file

@ -1,12 +1,12 @@
#ifndef SPROUT_RANDOM_GEOMETRIC_DISTRIBUTION_HPP #ifndef SPROUT_RANDOM_GEOMETRIC_DISTRIBUTION_HPP
#define SPROUT_RANDOM_GEOMETRIC_DISTRIBUTION_HPP #define SPROUT_RANDOM_GEOMETRIC_DISTRIBUTION_HPP
#include <cmath>
#include <ios> #include <ios>
#include <limits> #include <limits>
#include <stdexcept> #include <stdexcept>
#include <sprout/config.hpp> #include <sprout/config.hpp>
#include <sprout/math/log.hpp> #include <sprout/math/log.hpp>
#include <sprout/math/floor.hpp>
#include <sprout/random/random_result.hpp> #include <sprout/random/random_result.hpp>
#include <sprout/random/uniform_01.hpp> #include <sprout/random/uniform_01.hpp>
@ -98,7 +98,7 @@ namespace sprout {
template<typename Engine, typename Random> template<typename Engine, typename Random>
SPROUT_CONSTEXPR sprout::random::random_result<Engine, geometric_distribution> generate_1(Random const& rnd) const { SPROUT_CONSTEXPR sprout::random::random_result<Engine, geometric_distribution> generate_1(Random const& rnd) const {
using sprout::log; using sprout::log;
using std::floor; using sprout::floor;
return sprout::random::random_result<Engine, geometric_distribution>( return sprout::random::random_result<Engine, geometric_distribution>(
static_cast<IntType>(floor(log(RealType(1) - rnd.result()) / log_1mp_)), static_cast<IntType>(floor(log(RealType(1) - rnd.result()) / log_1mp_)),
rnd.engine(), rnd.engine(),