2013-08-08 09:54:33 +00:00
|
|
|
/*=============================================================================
|
2019-01-07 08:47:17 +00:00
|
|
|
Copyright (c) 2011-2019 Bolero MURAKAMI
|
2013-08-08 09:54:33 +00:00
|
|
|
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-16 10:09:43 +00:00
|
|
|
#ifndef SPROUT_STRING_FLOAT_TO_STRING_HPP
|
|
|
|
#define SPROUT_STRING_FLOAT_TO_STRING_HPP
|
|
|
|
|
|
|
|
#include <type_traits>
|
|
|
|
#include <sprout/config.hpp>
|
2014-04-30 07:30:26 +00:00
|
|
|
#include <sprout/workaround/std/cstddef.hpp>
|
2013-08-06 15:15:09 +00:00
|
|
|
#include <sprout/limits.hpp>
|
2013-04-06 04:06:51 +00:00
|
|
|
#include <sprout/index_tuple/metafunction.hpp>
|
2012-04-16 10:09:43 +00:00
|
|
|
#include <sprout/string/string.hpp>
|
2013-11-22 12:11:08 +00:00
|
|
|
#include <sprout/type_traits/integral_constant.hpp>
|
2012-05-26 15:43:38 +00:00
|
|
|
#include <sprout/type_traits/enabler_if.hpp>
|
2013-08-05 13:18:02 +00:00
|
|
|
#include <sprout/math/isinf.hpp>
|
|
|
|
#include <sprout/math/isnan.hpp>
|
|
|
|
#include <sprout/math/signbit.hpp>
|
2012-12-19 14:06:08 +00:00
|
|
|
#include <sprout/math/floor.hpp>
|
2015-12-09 10:54:35 +00:00
|
|
|
#include <sprout/tpp/algorithm/max_element.hpp>
|
2014-10-27 01:16:49 +00:00
|
|
|
#include <sprout/detail/char_literal.hpp>
|
2012-04-16 10:09:43 +00:00
|
|
|
#include <sprout/detail/char_conversion.hpp>
|
2012-06-23 23:22:12 +00:00
|
|
|
#include <sprout/detail/math/int.hpp>
|
|
|
|
#include <sprout/detail/math/float.hpp>
|
2013-02-19 16:12:56 +00:00
|
|
|
#include HDR_ALGORITHM_MIN_MAX_SSCRISK_CEL_OR_SPROUT
|
2012-04-16 10:09:43 +00:00
|
|
|
|
|
|
|
namespace sprout {
|
2012-04-16 11:19:47 +00:00
|
|
|
namespace detail {
|
|
|
|
SPROUT_STATIC_CONSTEXPR std::size_t decimal_places_length = 6;
|
|
|
|
} // namespace detail
|
|
|
|
|
2012-04-16 10:09:43 +00:00
|
|
|
//
|
|
|
|
// printed_float_digits
|
|
|
|
//
|
|
|
|
template<typename FloatType>
|
|
|
|
struct printed_float_digits
|
2013-11-22 12:11:08 +00:00
|
|
|
: public sprout::integral_constant<
|
2012-04-16 10:09:43 +00:00
|
|
|
std::size_t,
|
2013-08-06 15:15:09 +00:00
|
|
|
sprout::numeric_limits<FloatType>::max_exponent10 + sprout::detail::decimal_places_length + 2
|
2012-04-16 10:09:43 +00:00
|
|
|
>
|
|
|
|
{};
|
|
|
|
|
|
|
|
namespace detail {
|
2012-04-16 17:23:41 +00:00
|
|
|
template<typename Elem, typename FloatType, sprout::index_t... Indexes>
|
2012-04-16 10:09:43 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::basic_string<Elem, sprout::printed_float_digits<FloatType>::value>
|
2012-12-19 14:06:08 +00:00
|
|
|
float_to_string_impl(FloatType val, bool negative, int digits, int v, sprout::index_tuple<Indexes...>) {
|
2013-08-09 10:04:37 +00:00
|
|
|
typedef sprout::detail::string_construct_access<Elem, sprout::printed_float_digits<FloatType>::value> access_type;
|
2012-10-05 15:58:56 +00:00
|
|
|
return negative
|
2013-08-09 10:04:37 +00:00
|
|
|
? access_type::raw_construct(
|
|
|
|
static_cast<std::size_t>(digits + 2 + sprout::detail::decimal_places_length),
|
2014-10-27 01:16:49 +00:00
|
|
|
SPROUT_CHAR_LITERAL('-', Elem),
|
2013-08-09 10:04:37 +00:00
|
|
|
(Indexes < digits ? sprout::detail::int_to_char<Elem>(sprout::detail::float_digit_at(val, digits - 1 - Indexes))
|
2014-10-27 01:16:49 +00:00
|
|
|
: Indexes == digits ? SPROUT_CHAR_LITERAL('.', Elem)
|
2013-08-09 10:04:37 +00:00
|
|
|
: Indexes < digits + 1 + sprout::detail::decimal_places_length
|
|
|
|
? sprout::detail::int_to_char<Elem>(sprout::detail::int_digit_at(v, digits + sprout::detail::decimal_places_length - Indexes))
|
|
|
|
: Elem()
|
|
|
|
)...
|
|
|
|
)
|
|
|
|
: access_type::raw_construct(
|
|
|
|
static_cast<std::size_t>(digits + 1 + sprout::detail::decimal_places_length),
|
|
|
|
(Indexes < digits ? sprout::detail::int_to_char<Elem>(sprout::detail::float_digit_at(val, digits - 1 - Indexes))
|
2014-10-27 01:16:49 +00:00
|
|
|
: Indexes == digits ? SPROUT_CHAR_LITERAL('.', Elem)
|
2013-08-09 10:04:37 +00:00
|
|
|
: Indexes < digits + 1 + sprout::detail::decimal_places_length
|
|
|
|
? sprout::detail::int_to_char<Elem>(sprout::detail::int_digit_at(v, digits + sprout::detail::decimal_places_length - Indexes))
|
|
|
|
: Elem()
|
|
|
|
)...
|
|
|
|
)
|
2012-04-16 10:09:43 +00:00
|
|
|
;
|
|
|
|
}
|
2012-12-19 14:06:08 +00:00
|
|
|
template<typename Elem, typename FloatType>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::basic_string<Elem, sprout::printed_float_digits<FloatType>::value>
|
|
|
|
float_to_string(FloatType val, bool negative, int digits) {
|
|
|
|
return sprout::detail::float_to_string_impl<Elem>(
|
2013-05-06 14:57:38 +00:00
|
|
|
val, negative, digits, static_cast<int>((val - sprout::math::floor(val)) * sprout::detail::int_pow<int>(sprout::detail::decimal_places_length)),
|
2013-03-31 06:14:10 +00:00
|
|
|
sprout::make_index_tuple<sprout::printed_float_digits<FloatType>::value - 1>::make()
|
2012-12-19 14:06:08 +00:00
|
|
|
);
|
|
|
|
}
|
2012-04-16 10:09:43 +00:00
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
//
|
|
|
|
// float_to_string
|
|
|
|
//
|
|
|
|
template<
|
2012-10-05 15:58:56 +00:00
|
|
|
typename Elem, typename FloatType,
|
2012-04-16 10:09:43 +00:00
|
|
|
typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler
|
|
|
|
>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::basic_string<Elem, sprout::printed_float_digits<FloatType>::value>
|
|
|
|
float_to_string(FloatType val) {
|
2013-08-09 10:04:37 +00:00
|
|
|
typedef sprout::detail::string_construct_access<Elem, sprout::printed_float_digits<FloatType>::value> access_type;
|
2013-08-05 13:18:02 +00:00
|
|
|
return sprout::math::isinf(val) ? sprout::math::signbit(val)
|
2014-10-27 01:16:49 +00:00
|
|
|
? access_type::raw_construct(4, SPROUT_CHAR_LITERAL('-', Elem), SPROUT_CHAR_LITERAL('i', Elem), SPROUT_CHAR_LITERAL('n', Elem), SPROUT_CHAR_LITERAL('f', Elem))
|
|
|
|
: access_type::raw_construct(3, SPROUT_CHAR_LITERAL('i', Elem), SPROUT_CHAR_LITERAL('n', Elem), SPROUT_CHAR_LITERAL('f', Elem))
|
2013-08-05 13:18:02 +00:00
|
|
|
: sprout::math::isnan(val) ? sprout::math::signbit(val)
|
2014-10-27 01:16:49 +00:00
|
|
|
? access_type::raw_construct(4, SPROUT_CHAR_LITERAL('-', Elem), SPROUT_CHAR_LITERAL('n', Elem), SPROUT_CHAR_LITERAL('a', Elem), SPROUT_CHAR_LITERAL('n', Elem))
|
|
|
|
: access_type::raw_construct(3, SPROUT_CHAR_LITERAL('n', Elem), SPROUT_CHAR_LITERAL('a', Elem), SPROUT_CHAR_LITERAL('n', Elem))
|
2013-08-05 13:18:02 +00:00
|
|
|
: sprout::detail::float_to_string<Elem>(
|
|
|
|
sprout::detail::float_round_at(val < 0 ? -val : val, sprout::detail::decimal_places_length),
|
|
|
|
sprout::math::signbit(val),
|
2013-08-05 15:05:45 +00:00
|
|
|
sprout::detail::float_digits_checked(val)
|
2013-08-05 13:18:02 +00:00
|
|
|
)
|
|
|
|
;
|
2012-04-19 06:39:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
template<typename FloatType>
|
|
|
|
struct printed_float_exponent10_digits
|
2013-11-22 12:11:08 +00:00
|
|
|
: public sprout::integral_constant<
|
2012-04-19 06:39:01 +00:00
|
|
|
std::size_t,
|
2015-12-09 10:54:35 +00:00
|
|
|
sprout::tpp::max_element_c<int, sprout::detail::int_digits_mf<int, sprout::numeric_limits<FloatType>::max_exponent10>::value, 2>::value
|
2012-04-19 06:39:01 +00:00
|
|
|
>
|
|
|
|
{};
|
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
//
|
|
|
|
// printed_float_exp_digits
|
|
|
|
//
|
|
|
|
template<typename FloatType>
|
|
|
|
struct printed_float_exp_digits
|
2013-11-22 12:11:08 +00:00
|
|
|
: public sprout::integral_constant<
|
2012-04-19 06:39:01 +00:00
|
|
|
std::size_t,
|
|
|
|
5 + sprout::detail::decimal_places_length + sprout::detail::printed_float_exponent10_digits<FloatType>::value
|
|
|
|
>
|
|
|
|
{};
|
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
template<typename Elem, typename FloatType, sprout::index_t... Indexes>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::basic_string<Elem, sprout::printed_float_exp_digits<FloatType>::value>
|
|
|
|
float_to_string_exp(FloatType val, bool negative, int exponent10, int e10_digits, sprout::index_tuple<Indexes...>) {
|
2013-08-09 10:04:37 +00:00
|
|
|
typedef sprout::detail::string_construct_access<Elem, sprout::printed_float_exp_digits<FloatType>::value> access_type;
|
2012-10-05 15:58:56 +00:00
|
|
|
return negative
|
2013-08-09 10:04:37 +00:00
|
|
|
? access_type::raw_construct(
|
|
|
|
static_cast<std::size_t>(5 + sprout::detail::decimal_places_length + e10_digits),
|
2014-10-27 01:16:49 +00:00
|
|
|
SPROUT_CHAR_LITERAL('-', Elem),
|
2013-08-09 10:04:37 +00:00
|
|
|
(Indexes == 0 ? sprout::detail::int_to_char<Elem>(sprout::detail::float_digit_at(val, 0))
|
2014-10-27 01:16:49 +00:00
|
|
|
: Indexes == 1 ? SPROUT_CHAR_LITERAL('.', Elem)
|
2013-08-09 10:04:37 +00:00
|
|
|
: Indexes < 2 + sprout::detail::decimal_places_length
|
|
|
|
? sprout::detail::int_to_char<Elem>(sprout::detail::float_digit_at(val, 1 - Indexes))
|
2014-10-27 01:16:49 +00:00
|
|
|
: Indexes == 2 + sprout::detail::decimal_places_length ? SPROUT_CHAR_LITERAL('e', Elem)
|
|
|
|
: Indexes == 3 + sprout::detail::decimal_places_length ? (exponent10 < 0 ? SPROUT_CHAR_LITERAL('-', Elem) : SPROUT_CHAR_LITERAL('+', Elem))
|
2013-08-09 10:04:37 +00:00
|
|
|
: Indexes < 4 + sprout::detail::decimal_places_length + e10_digits
|
|
|
|
? sprout::detail::int_to_char<Elem>(sprout::detail::int_digit_at(exponent10, 3 + sprout::detail::decimal_places_length + e10_digits - Indexes))
|
|
|
|
: Elem()
|
|
|
|
)...
|
|
|
|
)
|
|
|
|
: access_type::raw_construct(
|
|
|
|
static_cast<std::size_t>(4 + sprout::detail::decimal_places_length + e10_digits),
|
|
|
|
(Indexes == 0 ? sprout::detail::int_to_char<Elem>(sprout::detail::float_digit_at(val, 0))
|
2014-10-27 01:16:49 +00:00
|
|
|
: Indexes == 1 ? SPROUT_CHAR_LITERAL('.', Elem)
|
2013-08-09 10:04:37 +00:00
|
|
|
: Indexes < 2 + sprout::detail::decimal_places_length
|
|
|
|
? sprout::detail::int_to_char<Elem>(sprout::detail::float_digit_at(val, 1 - Indexes))
|
2014-10-27 01:16:49 +00:00
|
|
|
: Indexes == 2 + sprout::detail::decimal_places_length ? SPROUT_CHAR_LITERAL('e', Elem)
|
|
|
|
: Indexes == 3 + sprout::detail::decimal_places_length ? (exponent10 < 0 ? SPROUT_CHAR_LITERAL('-', Elem) : SPROUT_CHAR_LITERAL('+', Elem))
|
2013-08-09 10:04:37 +00:00
|
|
|
: Indexes < 4 + sprout::detail::decimal_places_length + e10_digits
|
|
|
|
? sprout::detail::int_to_char<Elem>(sprout::detail::int_digit_at(exponent10, 3 + sprout::detail::decimal_places_length + e10_digits - Indexes))
|
|
|
|
: Elem()
|
|
|
|
)...
|
|
|
|
)
|
2012-04-19 06:39:01 +00:00
|
|
|
;
|
|
|
|
}
|
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
//
|
|
|
|
// float_to_string_exp
|
|
|
|
//
|
|
|
|
template<
|
2012-10-05 15:58:56 +00:00
|
|
|
typename Elem, typename FloatType,
|
2012-04-19 06:39:01 +00:00
|
|
|
typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler
|
|
|
|
>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::basic_string<Elem, sprout::printed_float_exp_digits<FloatType>::value>
|
|
|
|
float_to_string_exp(FloatType val) {
|
2013-08-09 10:04:37 +00:00
|
|
|
typedef sprout::detail::string_construct_access<Elem, sprout::printed_float_exp_digits<FloatType>::value> access_type;
|
2013-08-05 13:18:02 +00:00
|
|
|
return sprout::math::isinf(val) ? sprout::math::signbit(val)
|
2014-10-27 01:16:49 +00:00
|
|
|
? access_type::raw_construct(4, SPROUT_CHAR_LITERAL('-', Elem), SPROUT_CHAR_LITERAL('i', Elem), SPROUT_CHAR_LITERAL('n', Elem), SPROUT_CHAR_LITERAL('f', Elem))
|
|
|
|
: access_type::raw_construct(3, SPROUT_CHAR_LITERAL('i', Elem), SPROUT_CHAR_LITERAL('n', Elem), SPROUT_CHAR_LITERAL('f', Elem))
|
2013-08-05 13:18:02 +00:00
|
|
|
: sprout::math::isnan(val) ? sprout::math::signbit(val)
|
2014-10-27 01:16:49 +00:00
|
|
|
? access_type::raw_construct(4, SPROUT_CHAR_LITERAL('-', Elem), SPROUT_CHAR_LITERAL('n', Elem), SPROUT_CHAR_LITERAL('a', Elem), SPROUT_CHAR_LITERAL('n', Elem))
|
|
|
|
: access_type::raw_construct(3, SPROUT_CHAR_LITERAL('n', Elem), SPROUT_CHAR_LITERAL('a', Elem), SPROUT_CHAR_LITERAL('n', Elem))
|
2013-08-05 13:18:02 +00:00
|
|
|
: sprout::detail::float_to_string_exp<Elem>(
|
|
|
|
sprout::detail::float_round_at(
|
|
|
|
(val < 0 ? -val : val) / sprout::detail::float_pow10<FloatType>(sprout::detail::float_exponent10(val)),
|
|
|
|
sprout::detail::decimal_places_length
|
|
|
|
),
|
|
|
|
sprout::math::signbit(val),
|
|
|
|
sprout::detail::float_exponent10(val),
|
|
|
|
NS_SSCRISK_CEL_OR_SPROUT::max(sprout::detail::int_digits(sprout::detail::float_exponent10(val)), 2),
|
|
|
|
sprout::make_index_tuple<sprout::printed_float_exp_digits<FloatType>::value - 1>::make()
|
|
|
|
)
|
|
|
|
;
|
2012-04-16 10:09:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// to_string_of
|
|
|
|
//
|
|
|
|
template<typename Elem, typename FloatType, typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::basic_string<Elem, sprout::printed_float_digits<FloatType>::value>
|
|
|
|
to_string_of(FloatType val) {
|
|
|
|
return sprout::float_to_string<Elem>(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// to_string
|
|
|
|
//
|
|
|
|
template<typename FloatType, typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::basic_string<char, sprout::printed_float_digits<FloatType>::value>
|
|
|
|
to_string(FloatType val) {
|
|
|
|
return sprout::to_string_of<char>(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// to_wstring
|
|
|
|
//
|
|
|
|
template<typename FloatType, typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::basic_string<wchar_t, sprout::printed_float_digits<FloatType>::value>
|
|
|
|
to_wstring(FloatType val) {
|
|
|
|
return sprout::to_string_of<wchar_t>(val);
|
|
|
|
}
|
|
|
|
|
2016-03-10 10:17:22 +00:00
|
|
|
#if SPROUT_USE_UNICODE_LITERALS
|
2012-04-16 10:09:43 +00:00
|
|
|
//
|
|
|
|
// to_u16string
|
|
|
|
//
|
|
|
|
template<typename FloatType, typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::basic_string<char16_t, sprout::printed_float_digits<FloatType>::value>
|
|
|
|
to_u16string(FloatType val) {
|
|
|
|
return sprout::to_string_of<char16_t>(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// to_u32string
|
|
|
|
//
|
|
|
|
template<typename FloatType, typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::basic_string<char32_t, sprout::printed_float_digits<FloatType>::value>
|
|
|
|
to_u32string(FloatType val) {
|
|
|
|
return sprout::to_string_of<char32_t>(val);
|
|
|
|
}
|
2016-03-10 10:17:22 +00:00
|
|
|
#endif
|
2012-04-16 10:09:43 +00:00
|
|
|
} // namespace sprout
|
|
|
|
|
|
|
|
#endif // #ifndef SPROUT_STRING_FLOAT_TO_STRING_HPP
|