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 to_chars_hex exp for Float128 types #151

Merged
merged 3 commits into from
Feb 2, 2024
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
9 changes: 8 additions & 1 deletion src/to_chars_float_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -333,13 +333,20 @@ to_chars_result to_chars_hex(char* first, char* last, Real value, int precision)
unbiased_exponent -= 2048;
}
}
else
else BOOST_IF_CONSTEXPR (std::is_same<Real, long double>::value)
{
if (unbiased_exponent > 16383)
{
unbiased_exponent -= 32768;
}
}
else
{
while (unbiased_exponent > 16383)
{
unbiased_exponent -= 32768;
}
}

const std::uint32_t abs_unbiased_exponent = unbiased_exponent < 0 ? static_cast<std::uint32_t>(-unbiased_exponent) :
static_cast<std::uint32_t>(unbiased_exponent);
Expand Down
22 changes: 15 additions & 7 deletions test/test_float128.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ void overflow_spot_value(const std::string& buffer, T expected_value, boost::cha
auto v = static_cast<T>(42.Q);
auto r = boost::charconv::from_chars_erange(buffer.c_str(), buffer.c_str() + std::strlen(buffer.c_str()), v, fmt);

if (!(BOOST_TEST_EQ(v, expected_value) && BOOST_TEST(r.ec == std::errc::result_out_of_range)))
if (!(BOOST_TEST(v == expected_value) && BOOST_TEST(r.ec == std::errc::result_out_of_range)))
{
std::cerr << "Test failure for: " << buffer << " got: " << v << std::endl;
}
Expand Down Expand Up @@ -282,6 +282,10 @@ void test_sprintf_float( T value, boost::charconv::chars_format fmt = boost::cha
}
}
}
else if (fmt == boost::charconv::chars_format::hex)
{
printf_string.erase(0, 2); // Remove 0x that printf appends
}

// Same issues that arise in to_chars_snprintf.cpp so abort if in range
//
Expand All @@ -297,7 +301,7 @@ void test_sprintf_float( T value, boost::charconv::chars_format fmt = boost::cha
// To chars: 1.0600979293241972185e-109
// Snprintf: 1.0601e-109
//
if ((value > static_cast<T>(1e16Q) && value < static_cast<T>(1e20Q)) ||
if ((value > static_cast<T>(1e15Q) && value < static_cast<T>(1e20Q)) ||
(value > static_cast<T>(1e4912Q) || value < static_cast<T>(1e-4912Q)) ||
(value > static_cast<T>(1e-115Q) && value < static_cast<T>(2e-109Q)))
{
Expand Down Expand Up @@ -584,7 +588,6 @@ void test_nans()

int main()
{
/*
#if BOOST_CHARCONV_LDBL_BITS == 128
test_signaling_nan<long double>();

Expand Down Expand Up @@ -662,13 +665,18 @@ int main()
test_sprintf_float( w3, boost::charconv::chars_format::scientific );
test_sprintf_float( w3, boost::charconv::chars_format::fixed );
test_sprintf_float( w3, boost::charconv::chars_format::hex );

__float128 w5 = -static_cast<__float128>( rng() ); // -0 .. 2^128
test_roundtrip( w5 );
test_sprintf_float( w5, boost::charconv::chars_format::general );
test_sprintf_float( w5, boost::charconv::chars_format::scientific );
test_sprintf_float( w5, boost::charconv::chars_format::fixed );
test_sprintf_float( w5, boost::charconv::chars_format::hex );
}

test_roundtrip_bv<__float128>();
}

test_spot<__float128>(-3.589653987658756543653653365436e+04Q, boost::charconv::chars_format::hex);


#ifdef BOOST_CHARCONV_HAS_STDFLOAT128
test_signaling_nan<std::float128_t>();

Expand Down Expand Up @@ -806,7 +814,7 @@ int main()
}

#endif
*/

spot_check_nan("nan", boost::charconv::chars_format::general);
spot_check_nan("-nan", boost::charconv::chars_format::general);
spot_check_inf("inf", boost::charconv::chars_format::general);
Expand Down
Loading