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 rounding to zero error in stod on very small float numbers #10672

Merged
merged 4 commits into from
Apr 23, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 9 additions & 2 deletions cpp/src/strings/convert/convert_floats.cu
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,17 @@ __device__ inline double stod(string_view const& d_str)
else if (exp_ten < std::numeric_limits<double>::min_exponent10)
return double{0};

double base = sign * static_cast<double>(digits);

exp_ten += 1 - num_digits;
// exp10() is faster than pow(10.0,exp_ten)
// extra floating-point division needed only in extreme range (e-287 - e-307)
davidwendt marked this conversation as resolved.
Show resolved Hide resolved
// where num_digits may push exp_ten below min_exponent10 (e-307)
if (exp_ten < std::numeric_limits<double>::min_exponent10) {
base = base / exp10(static_cast<double>(num_digits - 1));
exp_ten += num_digits - 1; // adjust exponent
}

double const exponent = exp10(static_cast<double>(std::abs(exp_ten)));
double const base = sign * static_cast<double>(digits);
return exp_ten < 0 ? base / exponent : base * exponent;
}

Expand Down
4 changes: 3 additions & 1 deletion cpp/tests/strings/floats_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,13 @@ TEST_F(StringsConvertTest, FromFloats32)

TEST_F(StringsConvertTest, ToFloats64)
{
// clang-format off
std::vector<const char*> h_strings{
"1234", nullptr, "-876", "543.2", "-0.12", ".25",
"-.002", "", "-0.0", "1.28e256", "NaN", "abc123",
"123abc", "456e", "-1.78e+5", "-122.33644782", "12e+309", "1.7976931348623159E308",
"-Inf", "-INFINITY"};
"-Inf", "-INFINITY", "1.0", "1.7976931348623157e+308", "1.7976931348623157e-307"};
// clang-format on
cudf::test::strings_column_wrapper strings(
h_strings.begin(),
h_strings.end(),
Expand Down