1
0
Fork 0
mirror of https://github.com/bolero-MURAKAMI/Sprout synced 2024-11-12 21:09:01 +00:00
Sprout/sprout/detail/math/int.hpp

53 lines
1.5 KiB
C++
Raw Normal View History

2012-04-19 03:29:48 +00:00
#ifndef SPROUT_DETAIL_INT_HPP
#define SPROUT_DETAIL_INT_HPP
#include <type_traits>
#include <sprout/config.hpp>
2012-05-26 15:43:38 +00:00
#include <sprout/type_traits/enabler_if.hpp>
2012-04-19 03:29:48 +00:00
namespace sprout {
namespace detail {
//
// int_pow
//
template<typename IntType, int Base = 10, typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler>
inline SPROUT_CONSTEXPR IntType
int_pow(int exponent) {
return exponent ? Base * sprout::detail::int_pow<IntType, Base>(exponent - 1)
: 1
;
}
//
// int_digits
//
template<int Base = 10, typename IntType>
inline SPROUT_CONSTEXPR int
int_digits_impl(IntType val) {
return val ? 1 + sprout::detail::int_digits_impl<Base>(val / Base)
: 0
;
}
template<int Base = 10, typename IntType, typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler>
inline SPROUT_CONSTEXPR int
int_digits(IntType val) {
return val ? 1 + sprout::detail::int_digits_impl<Base>(val / Base)
: 1
;
}
//
// int_digit_at
//
template<int Base = 10, typename IntType, typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler>
inline SPROUT_CONSTEXPR int
int_digit_at(IntType val, int digits) {
return val < 0 ? -((val / sprout::detail::int_pow<IntType, Base>(digits)) % Base)
: (val / sprout::detail::int_pow<IntType, Base>(digits)) % Base
;
}
} // namespace detail
} // namespace sprout
#endif // #ifndef SPROUT_DETAIL_INT_HPP