Skip to content

Conversation

@getvictor
Copy link
Contributor

Fixes #8301

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

@getvictor getvictor requested review from a team as code owners March 26, 2024 21:01
@getvictor getvictor marked this pull request as draft March 27, 2024 13:31
@getvictor getvictor marked this pull request as ready for review March 27, 2024 15:09
@directionless directionless added this to the 5.13 milestone May 1, 2024
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;
Copy link
Contributor

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)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

SQLite supports 15 significant digits. So we set precision to 15 significant digits plus 1 insignificant (estimate). The previous approach was using more digits, leading to bad results in some cases.

This pattern is used in many other places:
https://github.com/search?q=%22std%3A%3Asetprecision%28std%3A%3Anumeric_limits%3Cdouble%3E%3A%3Adigits10+%2B+1%29%22&type=code

Copy link
Contributor

@lucasmrod lucasmrod left a comment

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
Copy link

Maybe @directionless , could you review this fix?

Copy link
Member

@directionless directionless left a comment

Choose a reason for hiding this comment

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

Good enough

@directionless directionless merged commit fae29d0 into osquery:master Jun 10, 2024
@getvictor getvictor deleted the 8301-real-precision branch June 11, 2024 17:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SQL real precision incorrect

4 participants