mirror of
https://github.com/bolero-MURAKAMI/Sprout.git
synced 2024-11-14 10:39:05 +00:00
add detail/float.hpp
This commit is contained in:
parent
22f13db2a0
commit
80763fd432
2 changed files with 124 additions and 54 deletions
118
sprout/detail/float.hpp
Normal file
118
sprout/detail/float.hpp
Normal file
|
@ -0,0 +1,118 @@
|
|||
#ifndef SPROUT_DETAIL_FLOAT_HPP
|
||||
#define SPROUT_DETAIL_FLOAT_HPP
|
||||
|
||||
#include <cmath>
|
||||
#include <type_traits>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/utility/enabler_if.hpp>
|
||||
|
||||
namespace sprout {
|
||||
namespace detail {
|
||||
//
|
||||
// float_pow10
|
||||
//
|
||||
template<typename FloatType>
|
||||
inline SPROUT_CONSTEXPR FloatType
|
||||
float_pow10_positive(int exponent) {
|
||||
return exponent ? sprout::detail::float_pow10_positive<FloatType>(exponent - 1) * 10
|
||||
: FloatType(1)
|
||||
;
|
||||
}
|
||||
template<typename FloatType>
|
||||
inline SPROUT_CONSTEXPR FloatType
|
||||
float_pow10_negative(int exponent) {
|
||||
return exponent ? sprout::detail::float_pow10_negative<FloatType>(exponent + 1) / 10
|
||||
: FloatType(1)
|
||||
;
|
||||
}
|
||||
template<typename FloatType, typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler>
|
||||
inline SPROUT_CONSTEXPR FloatType
|
||||
float_pow10(int exponent) {
|
||||
return exponent < 0
|
||||
? sprout::detail::float_pow10_negative<FloatType>(exponent)
|
||||
: sprout::detail::float_pow10_positive<FloatType>(exponent)
|
||||
;
|
||||
}
|
||||
|
||||
//
|
||||
// float_exponent10
|
||||
//
|
||||
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
|
||||
;
|
||||
}
|
||||
template<typename FloatType, typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler>
|
||||
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)
|
||||
;
|
||||
}
|
||||
|
||||
//
|
||||
// float_digits
|
||||
//
|
||||
template<typename FloatType, typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler>
|
||||
inline SPROUT_CONSTEXPR int
|
||||
float_digits_impl(FloatType val) {
|
||||
return val < 1 ? 0
|
||||
: 1 + sprout::detail::float_digits_impl(val / 10)
|
||||
;
|
||||
}
|
||||
template<typename FloatType>
|
||||
inline SPROUT_CONSTEXPR int
|
||||
float_digits(FloatType val) {
|
||||
return val < 0
|
||||
? val > -1 ? 1 : 1 + sprout::detail::float_digits_impl(-val / 10)
|
||||
: val < 1 ? 1 : 1 + sprout::detail::float_digits_impl(val / 10)
|
||||
;
|
||||
}
|
||||
|
||||
//
|
||||
// float_digit_at
|
||||
//
|
||||
template<typename FloatType, typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler>
|
||||
inline SPROUT_CONSTEXPR int
|
||||
float_digit_of_impl(FloatType val) {
|
||||
using std::floor;
|
||||
return static_cast<int>((val - floor(val)) * 10);
|
||||
}
|
||||
template<typename FloatType>
|
||||
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_round_at
|
||||
//
|
||||
template<typename FloatType, typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler>
|
||||
inline SPROUT_CONSTEXPR FloatType
|
||||
float_round_impl(FloatType val, FloatType p10) {
|
||||
using std::round;
|
||||
return round(val * p10) / p10;
|
||||
}
|
||||
template<typename FloatType>
|
||||
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
|
|
@ -9,6 +9,7 @@
|
|||
#include <sprout/string/string.hpp>
|
||||
#include <sprout/utility/enabler_if.hpp>
|
||||
#include <sprout/detail/char_conversion.hpp>
|
||||
#include <sprout/detail/float.hpp>
|
||||
|
||||
namespace sprout {
|
||||
namespace detail {
|
||||
|
@ -27,64 +28,15 @@ namespace sprout {
|
|||
{};
|
||||
|
||||
namespace detail {
|
||||
template<typename FloatType>
|
||||
inline SPROUT_CONSTEXPR FloatType
|
||||
float_pow10(int exponent) {
|
||||
return exponent ? FloatType(10) * sprout::detail::float_pow10<FloatType>(exponent - 1)
|
||||
: FloatType(1)
|
||||
;
|
||||
}
|
||||
|
||||
template<typename FloatType>
|
||||
inline SPROUT_CONSTEXPR int
|
||||
float_digits_impl(FloatType val) {
|
||||
return !(val < 1 && val > -1) ? 1 + sprout::detail::float_digits_impl(val / 10)
|
||||
: 0
|
||||
;
|
||||
}
|
||||
template<typename FloatType>
|
||||
inline SPROUT_CONSTEXPR int
|
||||
float_digits(FloatType val) {
|
||||
return !(val < 1 && val > -1) ? 1 + sprout::detail::float_digits_impl(val / 10)
|
||||
: 1
|
||||
;
|
||||
}
|
||||
|
||||
template<typename FloatType>
|
||||
inline SPROUT_CONSTEXPR int
|
||||
float_digit_of_impl(FloatType val) {
|
||||
using std::floor;
|
||||
return static_cast<int>((val - floor(val)) * 10);
|
||||
}
|
||||
template<typename FloatType>
|
||||
inline SPROUT_CONSTEXPR int
|
||||
float_digit_of(FloatType val, int digits) {
|
||||
return digits < 0 ? sprout::detail::float_digit_of_impl(val * sprout::detail::float_pow10<FloatType>(-digits - 1))
|
||||
: sprout::detail::float_digit_of_impl(val / sprout::detail::float_pow10<FloatType>(digits + 1))
|
||||
;
|
||||
}
|
||||
|
||||
template<typename FloatType>
|
||||
inline SPROUT_CONSTEXPR FloatType
|
||||
float_round_impl(FloatType val, FloatType p10) {
|
||||
using std::round;
|
||||
return round(val * p10) / p10;
|
||||
}
|
||||
template<typename FloatType>
|
||||
inline SPROUT_CONSTEXPR FloatType
|
||||
float_round(FloatType val, int digits) {
|
||||
return sprout::detail::float_round_impl(val, sprout::detail::float_pow10<FloatType>(digits));
|
||||
}
|
||||
|
||||
template<typename Elem, typename FloatType, sprout::index_t... Indexes>
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<Elem, sprout::printed_float_digits<FloatType>::value>
|
||||
float_to_string(FloatType val, bool negative, int digits, sprout::index_tuple<Indexes...>) {
|
||||
return negative ? sprout::basic_string<Elem, sprout::printed_float_digits<FloatType>::value>{
|
||||
{
|
||||
static_cast<Elem>('-'),
|
||||
(Indexes < digits ? sprout::detail::int_to_char<Elem>(sprout::detail::float_digit_of(val, digits - 1 - Indexes))
|
||||
(Indexes < digits ? sprout::detail::int_to_char<Elem>(sprout::detail::float_digit_at(val, digits - 1 - Indexes))
|
||||
: Indexes == digits ? static_cast<Elem>('.')
|
||||
: Indexes < digits + 1 + sprout::detail::decimal_places_length ? sprout::detail::int_to_char<Elem>(sprout::detail::float_digit_of(val, digits - Indexes))
|
||||
: Indexes < digits + 1 + sprout::detail::decimal_places_length ? sprout::detail::int_to_char<Elem>(sprout::detail::float_digit_at(val, digits - Indexes))
|
||||
: Elem()
|
||||
)...
|
||||
},
|
||||
|
@ -92,9 +44,9 @@ namespace sprout {
|
|||
}
|
||||
: sprout::basic_string<Elem, sprout::printed_float_digits<FloatType>::value>{
|
||||
{
|
||||
(Indexes < digits ? sprout::detail::int_to_char<Elem>(sprout::detail::float_digit_of(val, digits - 1 - Indexes))
|
||||
(Indexes < digits ? sprout::detail::int_to_char<Elem>(sprout::detail::float_digit_at(val, digits - 1 - Indexes))
|
||||
: Indexes == digits ? static_cast<Elem>('.')
|
||||
: Indexes < digits + 1 + sprout::detail::decimal_places_length ? sprout::detail::int_to_char<Elem>(sprout::detail::float_digit_of(val, digits - Indexes))
|
||||
: Indexes < digits + 1 + sprout::detail::decimal_places_length ? sprout::detail::int_to_char<Elem>(sprout::detail::float_digit_at(val, digits - Indexes))
|
||||
: Elem()
|
||||
)...
|
||||
},
|
||||
|
@ -115,7 +67,7 @@ namespace sprout {
|
|||
inline SPROUT_CONSTEXPR sprout::basic_string<Elem, sprout::printed_float_digits<FloatType>::value>
|
||||
float_to_string(FloatType val) {
|
||||
return sprout::detail::float_to_string<Elem>(
|
||||
sprout::detail::float_round(val < 0 ? -val : val, sprout::detail::decimal_places_length),
|
||||
sprout::detail::float_round_at(val < 0 ? -val : val, sprout::detail::decimal_places_length),
|
||||
val < 0,
|
||||
sprout::detail::float_digits(val),
|
||||
sprout::index_range<0, sprout::printed_float_digits<FloatType>::value - 1>::make()
|
||||
|
|
Loading…
Reference in a new issue