Using std::setprecision to get the right precision for double to string conversion. by getvictor · Pull Request #8302 · osquery/osquery

@getvictor

Fixes #8301

boost::lexical_cast was not using the right precision for double-to-string conversion.
Switched to using std::ostringstream.

@getvictor

@getvictor

@getvictor getvictor marked this pull request as ready for review

March 27, 2024 15:09

lucasmrod

std::string operator()(const double& d) const {
std::string s{boost::lexical_cast<std::string>(d)};
std::ostringstream ss;
ss << std::setprecision(std::numeric_limits<double>::digits10 + 1) << d;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it 16 because that's the case for most doubles?

Double values have between 15 and 18 digits of precision, with most double values having at least 16 significant digits. (source)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lucasmrod

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, left one question.

@RachelElysia

directionless

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good enough