From 9ff67fe5823e110a1e2677f6153b3ae29689daad Mon Sep 17 00:00:00 2001 From: bolero-MURAKAMI Date: Thu, 19 Apr 2012 15:39:01 +0900 Subject: [PATCH] add float_to_string_exp --- sprout/string/float_to_string.hpp | 85 +++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) diff --git a/sprout/string/float_to_string.hpp b/sprout/string/float_to_string.hpp index b4e2be41..9e7c33ad 100644 --- a/sprout/string/float_to_string.hpp +++ b/sprout/string/float_to_string.hpp @@ -6,10 +6,13 @@ #include #include #include +#include #include #include #include +#include #include +#include HDR_ALGORITHM_SSCRISK_CEL_OR_SPROUT namespace sprout { namespace detail { @@ -74,6 +77,88 @@ namespace sprout { ); } + namespace detail { + template + struct printed_float_exponent10_digits + : public std::integral_constant< + std::size_t, + NS_SSCRISK_CEL_OR_SPROUT::max(sprout::detail::int_digits(std::numeric_limits::max_exponent10), 2) + > + {}; + } // namespace detail + + // + // printed_float_exp_digits + // + template + struct printed_float_exp_digits + : public std::integral_constant< + std::size_t, + 5 + sprout::detail::decimal_places_length + sprout::detail::printed_float_exponent10_digits::value + > + {}; + + namespace detail { + template + inline SPROUT_CONSTEXPR sprout::basic_string::value> + float_to_string_exp(FloatType val, bool negative, int exponent10, int e10_digits, sprout::index_tuple) { + return negative ? sprout::basic_string::value>{ + { + static_cast('-'), + (Indexes == 0 ? sprout::detail::int_to_char(sprout::detail::float_digit_at(val, 0)) + : Indexes == 1 ? static_cast('.') + : Indexes < 2 + sprout::detail::decimal_places_length + ? sprout::detail::int_to_char(sprout::detail::float_digit_at(val, 1 - Indexes)) + : Indexes == 2 + sprout::detail::decimal_places_length ? static_cast('e') + : Indexes == 3 + sprout::detail::decimal_places_length ? static_cast(exponent10 < 0 ? '-' : '+') + : Indexes < 4 + sprout::detail::decimal_places_length + e10_digits + ? sprout::detail::int_to_char(sprout::detail::int_digit_at(exponent10, 3 + sprout::detail::decimal_places_length + e10_digits - Indexes)) + : Elem() + )... + }, + static_cast(5 + sprout::detail::decimal_places_length + e10_digits) + } + : sprout::basic_string::value>{ + { + (Indexes == 0 ? sprout::detail::int_to_char(sprout::detail::float_digit_at(val, 0)) + : Indexes == 1 ? static_cast('.') + : Indexes < 2 + sprout::detail::decimal_places_length + ? sprout::detail::int_to_char(sprout::detail::float_digit_at(val, 1 - Indexes)) + : Indexes == 2 + sprout::detail::decimal_places_length ? static_cast('e') + : Indexes == 3 + sprout::detail::decimal_places_length ? static_cast(exponent10 < 0 ? '-' : '+') + : Indexes < 4 + sprout::detail::decimal_places_length + e10_digits + ? sprout::detail::int_to_char(sprout::detail::int_digit_at(exponent10, 3 + sprout::detail::decimal_places_length + e10_digits - Indexes)) + : Elem() + )... + }, + static_cast(4 + sprout::detail::decimal_places_length + e10_digits) + } + ; + } + } // namespace detail + + // + // float_to_string_exp + // + template< + typename Elem, + typename FloatType, + typename sprout::enabler_if::value>::type = sprout::enabler + > + inline SPROUT_CONSTEXPR sprout::basic_string::value> + float_to_string_exp(FloatType val) { + return sprout::detail::float_to_string_exp( + sprout::detail::float_round_at( + (val < 0 ? -val : val) / sprout::detail::float_pow10(sprout::detail::float_exponent10(val)), + sprout::detail::decimal_places_length + ), + val < 0, + sprout::detail::float_exponent10(val), + NS_SSCRISK_CEL_OR_SPROUT::max(sprout::detail::int_digits(sprout::detail::float_exponent10(val)), 2), + sprout::index_range<0, sprout::printed_float_exp_digits::value - 1>::make() + ); + } + // // to_string_of //