diff --git a/include/etl/private/to_string_helper.h b/include/etl/private/to_string_helper.h index c1b630ba..60869664 100644 --- a/include/etl/private/to_string_helper.h +++ b/include/etl/private/to_string_helper.h @@ -218,7 +218,7 @@ namespace etl /// Helper function for floating point nan and inf. //*************************************************************************** template - void add_nan_inf(const bool not_a_number, const bool infinity, TIString& str, const etl::basic_format_spec& format) + void add_nan_inf(const bool not_a_number, const bool infinity, const bool is_negative, TIString& str, const etl::basic_format_spec& format) { typedef typename TIString::value_type type; @@ -240,6 +240,11 @@ namespace etl } else if (infinity) { + if (is_negative) + { + str.push_back(type('-')); + } + if (format.is_upper_case()) { str.insert(str.end(), ETL_OR_STD11::begin(inf_upper), ETL_OR_STD11::end(inf_upper)); @@ -433,7 +438,7 @@ namespace etl if (isnan(value) || isinf(value)) { - etl::private_to_string::add_nan_inf(isnan(value), isinf(value), str, format); + etl::private_to_string::add_nan_inf(isnan(value), isinf(value), etl::is_negative(value), str, format); } else { diff --git a/test/test_to_string.cpp b/test/test_to_string.cpp index 8dc05b72..17a9d982 100644 --- a/test/test_to_string.cpp +++ b/test/test_to_string.cpp @@ -28,7 +28,7 @@ SOFTWARE. #include "unit_test_framework.h" -#include +#include #include #include @@ -714,5 +714,36 @@ namespace CHECK(etl::string<64>(STR(" 0")) == s6); #endif } + + //************************************************************************* + TEST(test_issue_1436_case_support_for_nan_inf_in_to_string) + { + if (std::numeric_limits::is_iec559) + { + etl::string<64> s1; + etl::to_string(std::numeric_limits::quiet_NaN(), s1, Format().precision(5).width(15).right().scientific(true).upper_case(false)); + CHECK(etl::string<64>(STR(" nan")) == s1); + + etl::string<64> s2; + etl::to_string(std::numeric_limits::infinity(), s2, Format().precision(5).width(15).right().scientific(true).upper_case(false)); + CHECK(etl::string<64>(STR(" inf")) == s2); + + etl::string<64> s3; + etl::to_string(-std::numeric_limits::infinity(), s3, Format().precision(5).width(15).right().scientific(true).upper_case(false)); + CHECK(etl::string<64>(STR(" -inf")) == s3); + + etl::string<64> s4; + etl::to_string(std::numeric_limits::quiet_NaN(), s4, Format().precision(5).width(15).right().scientific(true).upper_case(true)); + CHECK(etl::string<64>(STR(" NAN")) == s4); + + etl::string<64> s5; + etl::to_string(std::numeric_limits::infinity(), s5, Format().precision(5).width(15).right().scientific(true).upper_case(true)); + CHECK(etl::string<64>(STR(" INF")) == s5); + + etl::string<64> s6; + etl::to_string(-std::numeric_limits::infinity(), s6, Format().precision(5).width(15).right().scientific(true).upper_case(true)); + CHECK(etl::string<64>(STR(" -INF")) == s6); + } + } } } // namespace