2013-08-08 09:54:33 +00:00
|
|
|
/*=============================================================================
|
|
|
|
Copyright (c) 2011-2013 Bolero MURAKAMI
|
|
|
|
https://github.com/bolero-MURAKAMI/Sprout
|
|
|
|
|
|
|
|
Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
=============================================================================*/
|
2012-04-18 08:46:27 +00:00
|
|
|
#ifndef SPROUT_DETAIL_FLOAT_HPP
|
|
|
|
#define SPROUT_DETAIL_FLOAT_HPP
|
|
|
|
|
|
|
|
#include <type_traits>
|
|
|
|
#include <sprout/config.hpp>
|
2012-07-04 15:09:44 +00:00
|
|
|
#include <sprout/math/abs.hpp>
|
|
|
|
#include <sprout/math/floor.hpp>
|
|
|
|
#include <sprout/math/round.hpp>
|
2012-05-26 15:43:38 +00:00
|
|
|
#include <sprout/type_traits/enabler_if.hpp>
|
2013-08-05 16:00:57 +00:00
|
|
|
#include <sprout/utility/pair/pair.hpp>
|
2013-08-05 15:05:45 +00:00
|
|
|
#include <sprout/detail/pow.hpp>
|
2012-04-18 08:46:27 +00:00
|
|
|
|
|
|
|
namespace sprout {
|
|
|
|
namespace detail {
|
|
|
|
//
|
|
|
|
// float_pow10
|
|
|
|
//
|
2012-10-05 15:58:56 +00:00
|
|
|
template<
|
|
|
|
typename FloatType,
|
|
|
|
typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler
|
|
|
|
>
|
2012-04-18 08:46:27 +00:00
|
|
|
inline SPROUT_CONSTEXPR FloatType
|
|
|
|
float_pow10(int exponent) {
|
2013-08-05 15:05:45 +00:00
|
|
|
return sprout::detail::pow_n(FloatType(10), exponent);
|
2012-04-18 08:46:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// float_exponent10
|
|
|
|
//
|
2013-08-09 10:04:37 +00:00
|
|
|
// !!! TODO: O(logN)
|
2012-04-18 08:46:27 +00:00
|
|
|
template<typename FloatType>
|
|
|
|
inline SPROUT_CONSTEXPR int
|
|
|
|
float_exponent10_positive(FloatType val) {
|
|
|
|
return val < 10 ? 0
|
|
|
|
: 1 + sprout::detail::float_exponent10_positive(val / 10)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
template<typename FloatType>
|
|
|
|
inline SPROUT_CONSTEXPR int
|
|
|
|
float_exponent10_negative(FloatType val) {
|
|
|
|
return val < 1 ? 1 + sprout::detail::float_exponent10_negative(val * 10)
|
|
|
|
: 0
|
|
|
|
;
|
|
|
|
}
|
2012-10-05 15:58:56 +00:00
|
|
|
template<
|
|
|
|
typename FloatType,
|
|
|
|
typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler
|
|
|
|
>
|
2012-04-18 08:46:27 +00:00
|
|
|
inline SPROUT_CONSTEXPR int
|
|
|
|
float_exponent10(FloatType val) {
|
|
|
|
return val < 0
|
|
|
|
? val > -1
|
|
|
|
? sprout::detail::float_exponent10_negative(-val)
|
|
|
|
: sprout::detail::float_exponent10_positive(-val)
|
|
|
|
: val < 1
|
|
|
|
? sprout::detail::float_exponent10_negative(val)
|
|
|
|
: sprout::detail::float_exponent10_positive(val)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2013-08-05 15:05:45 +00:00
|
|
|
// float_digit_at
|
2012-04-18 08:46:27 +00:00
|
|
|
//
|
2013-08-05 13:18:02 +00:00
|
|
|
template<typename FloatType>
|
|
|
|
inline SPROUT_CONSTEXPR int
|
2013-08-05 15:05:45 +00:00
|
|
|
float_digit_of_impl(FloatType val) {
|
|
|
|
return static_cast<int>((val - sprout::math::floor(val)) * 10);
|
|
|
|
}
|
|
|
|
template<
|
|
|
|
typename FloatType,
|
|
|
|
typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler
|
|
|
|
>
|
|
|
|
inline SPROUT_CONSTEXPR int
|
|
|
|
float_digit_at(FloatType val, int digits) {
|
|
|
|
return sprout::detail::float_digit_of_impl(val / sprout::detail::float_pow10<FloatType>(digits + 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// float_digits
|
|
|
|
//
|
|
|
|
template<typename FloatType>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::pair<int, FloatType>
|
|
|
|
float_digits_impl_1(sprout::pair<int, FloatType> const& current, FloatType val, int n) {
|
|
|
|
typedef sprout::pair<int, FloatType> type;
|
|
|
|
return (val / current.second) < 1 ? current
|
|
|
|
: n == 1 ? type(current.first + 1, current.second * FloatType(10))
|
|
|
|
: sprout::detail::float_digits_impl_1(
|
|
|
|
sprout::detail::float_digits_impl_1(
|
|
|
|
current,
|
|
|
|
val, n / 2
|
|
|
|
),
|
|
|
|
val, n - n / 2
|
|
|
|
)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
template<typename FloatType>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::pair<int, FloatType>
|
|
|
|
float_digits_impl(sprout::pair<int, FloatType> const& current, FloatType val, int n) {
|
|
|
|
return (val / current.second) < 1 ? current
|
|
|
|
: sprout::detail::float_digits_impl(
|
|
|
|
sprout::detail::float_digits_impl_1(
|
|
|
|
current,
|
|
|
|
val, n
|
|
|
|
),
|
|
|
|
val, n * 2
|
|
|
|
)
|
2013-08-05 13:18:02 +00:00
|
|
|
;
|
|
|
|
}
|
2012-10-05 15:58:56 +00:00
|
|
|
template<
|
|
|
|
typename FloatType,
|
|
|
|
typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler
|
|
|
|
>
|
2012-04-18 08:46:27 +00:00
|
|
|
inline SPROUT_CONSTEXPR int
|
|
|
|
float_digits(FloatType val) {
|
2013-08-05 15:05:45 +00:00
|
|
|
typedef sprout::pair<int, FloatType> type;
|
2012-04-18 08:46:27 +00:00
|
|
|
return val < 0
|
2013-08-05 15:05:45 +00:00
|
|
|
? val > -1 ? 1 : sprout::detail::float_digits_impl(type(1, FloatType(10)), -val, 1).first
|
|
|
|
: val < 1 ? 1 : sprout::detail::float_digits_impl(type(1, FloatType(10)), val, 1).first
|
2012-04-18 08:46:27 +00:00
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2013-08-05 15:05:45 +00:00
|
|
|
// float_digits_checked
|
2012-04-18 08:46:27 +00:00
|
|
|
//
|
2013-08-05 13:18:02 +00:00
|
|
|
template<typename FloatType>
|
2012-04-18 08:46:27 +00:00
|
|
|
inline SPROUT_CONSTEXPR int
|
2013-08-05 15:05:45 +00:00
|
|
|
float_digits_checked_impl(FloatType val, int digits) {
|
|
|
|
return val == 0 ? digits
|
|
|
|
: sprout::detail::float_digit_at(val, digits - 1) == 0 ? digits - 1 : digits
|
|
|
|
;
|
2012-04-18 08:46:27 +00:00
|
|
|
}
|
2013-08-05 13:18:02 +00:00
|
|
|
template<
|
|
|
|
typename FloatType,
|
|
|
|
typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler
|
|
|
|
>
|
2012-04-18 08:46:27 +00:00
|
|
|
inline SPROUT_CONSTEXPR int
|
2013-08-05 15:05:45 +00:00
|
|
|
float_digits_checked(FloatType val) {
|
|
|
|
return sprout::detail::float_digits_checked_impl(val, sprout::detail::float_digits(val));
|
2012-04-18 08:46:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// float_round_at
|
|
|
|
//
|
2013-08-05 13:18:02 +00:00
|
|
|
template<typename FloatType>
|
2012-04-18 08:46:27 +00:00
|
|
|
inline SPROUT_CONSTEXPR FloatType
|
|
|
|
float_round_impl(FloatType val, FloatType p10) {
|
2013-05-06 14:57:38 +00:00
|
|
|
return sprout::math::round(val * p10) / p10;
|
2012-04-18 08:46:27 +00:00
|
|
|
}
|
2013-08-05 13:18:02 +00:00
|
|
|
template<
|
|
|
|
typename FloatType,
|
|
|
|
typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler
|
|
|
|
>
|
2012-04-18 08:46:27 +00:00
|
|
|
inline SPROUT_CONSTEXPR FloatType
|
|
|
|
float_round_at(FloatType val, int digits) {
|
|
|
|
return sprout::detail::float_round_impl(val, sprout::detail::float_pow10<FloatType>(digits));
|
|
|
|
}
|
|
|
|
} // namespace detail
|
|
|
|
} // namespace sprout
|
|
|
|
|
|
|
|
#endif // #ifndef SPROUT_DETAIL_FLOAT_HPP
|