fix float to_string (rounding)

This commit is contained in:
bolero-MURAKAMI 2012-04-16 19:49:23 +09:00
parent d8353c9bf7
commit 18014daa68

View file

@ -14,6 +14,7 @@
#include <sprout/detail/char_conversion.hpp> #include <sprout/detail/char_conversion.hpp>
namespace sprout { namespace sprout {
SPROUT_STATIC_CONSTEXPR std::size_t decimal_places_length = 6;
// //
// printed_float_digits // printed_float_digits
// //
@ -21,12 +22,24 @@ namespace sprout {
struct printed_float_digits struct printed_float_digits
: public std::integral_constant< : public std::integral_constant<
std::size_t, std::size_t,
/*std::numeric_limits<floatFloatType>::max_exponent10 + 6 + 2*/ /*std::numeric_limits<floatFloatType>::max_exponent10 + decimal_places_length + 3*/
sprout::integer_digits<std::intmax_t>::value + 6 + 3 sprout::integer_digits<std::intmax_t>::value + decimal_places_length + 3
> >
{}; {};
namespace detail { namespace detail {
template<typename FloatType>
inline SPROUT_CONSTEXPR unsigned float_extract_rounded_impl(FloatType val) {
return static_cast<unsigned>(val * 1000000)
+ (static_cast<unsigned>(val * 10000000) % 10 >= 5 ? 1 : 0)
;
}
template<typename FloatType>
inline SPROUT_CONSTEXPR unsigned float_extract_rounded(FloatType val) {
using std::floor;
return sprout::detail::float_extract_rounded_impl(val - floor(val));
}
template< template<
typename Elem, typename Elem,
typename FloatType, typename FloatType,
@ -47,7 +60,6 @@ namespace sprout {
> >
inline SPROUT_CONSTEXPR sprout::basic_string<Elem, sprout::printed_float_digits<FloatType>::value> inline SPROUT_CONSTEXPR sprout::basic_string<Elem, sprout::printed_float_digits<FloatType>::value>
float_to_string_impl_1(FloatType val, bool negative, Args... args) { float_to_string_impl_1(FloatType val, bool negative, Args... args) {
using std::floor;
return !(val < 1) ? sprout::detail::float_to_string_impl_1<Elem>( return !(val < 1) ? sprout::detail::float_to_string_impl_1<Elem>(
val / 10, val / 10,
negative, negative,
@ -105,8 +117,18 @@ namespace sprout {
inline SPROUT_CONSTEXPR sprout::basic_string<Elem, sprout::printed_float_digits<FloatType>::value> inline SPROUT_CONSTEXPR sprout::basic_string<Elem, sprout::printed_float_digits<FloatType>::value>
float_to_string(FloatType val) { float_to_string(FloatType val) {
using std::floor; using std::floor;
return val < 0 ? sprout::detail::float_to_string_impl<Elem>(-val, true, static_cast<unsigned>((-val - floor(-val)) * 1000000), 6) return val < 0 ? sprout::detail::float_to_string_impl<Elem>(
: sprout::detail::float_to_string_impl<Elem>(val, false, static_cast<unsigned>((val - floor(val)) * 1000000), 6) -val,
true,
sprout::detail::float_extract_rounded(-val),
decimal_places_length
)
: sprout::detail::float_to_string_impl<Elem>(
val,
false,
sprout::detail::float_extract_rounded(val),
decimal_places_length
)
; ;
} }
} // namespace detail } // namespace detail