Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bfloat macros and long double general formatting #94

Merged
merged 4 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/boost/charconv/detail/ryu/ryu_generic_128.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ static inline int generic_to_chars(const struct floating_decimal_128 v, char* re
if (fmt == chars_format::general)
{
const int64_t exp = v.exponent + static_cast<int64_t>(olength);
if (exp <= 0 && exp >= -4)
if (std::abs(exp) <= olength)
{
return generic_to_chars_fixed(v, result, result_size, precision);
}
Expand Down
2 changes: 1 addition & 1 deletion include/boost/charconv/to_chars.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@ BOOST_CHARCONV_DECL to_chars_result to_chars(char* first, char* last, std::float
BOOST_CHARCONV_DECL to_chars_result to_chars(char* first, char* last, std::float128_t value,
chars_format fmt = chars_format::general, int precision = -1 ) noexcept;
#endif
#ifdef BOOST_CHARCONV_HAS_BFLOAT16
#ifdef BOOST_CHARCONV_HAS_BRAINFLOAT16
BOOST_CHARCONV_DECL to_chars_result to_chars(char* first, char* last, std::bfloat16_t value,
chars_format fmt = chars_format::general, int precision = -1 ) noexcept;
#endif
Expand Down
2 changes: 1 addition & 1 deletion src/from_chars.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ boost::charconv::from_chars_result boost::charconv::from_chars(const char* first
}
#endif

#ifdef BOOST_CHARCONV_HAS_BFLOAT16
#ifdef BOOST_CHARCONV_HAS_BRAINFLOAT16
boost::charconv::from_chars_result boost::charconv::from_chars(const char* first, const char* last, std::bfloat16_t& value, boost::charconv::chars_format fmt) noexcept
{
float f;
Expand Down
2 changes: 1 addition & 1 deletion src/to_chars.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,7 @@ boost::charconv::to_chars_result boost::charconv::to_chars(char* first, char* la
}
#endif

#ifdef BOOST_CHARCONV_HAS_BFLOAT16
#ifdef BOOST_CHARCONV_HAS_BRAINFLOAT16
boost::charconv::to_chars_result boost::charconv::to_chars(char* first, char* last, std::bfloat16_t value,
boost::charconv::chars_format fmt, int precision) noexcept
{
Expand Down
2 changes: 1 addition & 1 deletion test/roundtrip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ int main()
}
#endif

#ifdef BOOST_CHARCONV_HAS_BFLOAT16
#ifdef BOOST_CHARCONV_HAS_BRAINFLOAT16
{
std::bfloat16_t const small_q = std::pow(1.0BF16, -16.0BF16);

Expand Down
32 changes: 30 additions & 2 deletions test/test_float128.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,15 @@ void test_spot(T val, boost::charconv::chars_format fmt = boost::charconv::chars
const auto boost_str = std::string(buffer_boost, r_boost.ptr);
const auto stl_str = std::string(buffer_stl, r_stl.ptr);

// Region of divergence between our results and that of the STL
// Value: 13501897678889699601
// Boost: 13501897678889699601
// STL: 1.3501897678889699601e+19
if (val > static_cast<T>(1e15) && val < static_cast<T>(1e20))
{
return;
}

if (!(BOOST_TEST_CSTR_EQ(boost_str.c_str(), stl_str.c_str()) && BOOST_TEST_EQ(diff_boost, diff_stl)))
{
std::cerr << std::setprecision(35)
Expand Down Expand Up @@ -472,6 +481,15 @@ void charconv_roundtrip(T val, boost::charconv::chars_format fmt = boost::charco
const auto boost_str = std::string(buffer_boost, r_boost.ptr);
const auto stl_str = std::string(buffer_stl, r_stl.ptr);

// Region of divergence between our results and that of the STL
// Value: 13501897678889699601
// Boost: 13501897678889699601
// STL: 1.3501897678889699601e+19
if (val > static_cast<T>(1e15) && val < static_cast<T>(1e20))
{
return;
}

if (!(BOOST_TEST_CSTR_EQ(boost_str.c_str(), stl_str.c_str()) && BOOST_TEST_EQ(diff_boost, diff_stl)))
{
std::cerr << std::setprecision(35)
Expand Down Expand Up @@ -695,10 +713,20 @@ int main()
test_issue_37<std::float128_t>();

// Issue 64
// Some of these are commented out because our answers differ from the STL
//
// Value: 0.001
// Boost: 1e-03
// STL: 0.001
//
// Value: 1e-04
// Boost: 1e-04
// STL: 0.0001

test_spot<std::float128_t>(1e-01F128);
test_spot<std::float128_t>(1e-02F128);
test_spot<std::float128_t>(1e-03F128);
test_spot<std::float128_t>(1e-04F128);
// test_spot<std::float128_t>(1e-03F128);
// test_spot<std::float128_t>(1e-04F128);
test_spot<std::float128_t>(1.01e-01F128);
test_spot<std::float128_t>(1.001e-01F128);
test_spot<std::float128_t>(1.0001e-01F128);
Expand Down
5 changes: 5 additions & 0 deletions test/to_chars_float_STL_comp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,11 @@ int main()
test_roundtrip<float>(8.922125027e+34F);
#endif

// Reported in issue #93
test_spot<float>(3.3F);
test_spot<double>(3.3);
test_spot<long double>(3.3L);

return boost::report_errors();
}

Expand Down