Skip to content

Commit

Permalink
Fix sum computation in higher precision
Browse files Browse the repository at this point in the history
This also fixes two warnings from LGTM:

    Multiplication result may overflow 'float'
    before it is converted to 'double'.

Replace also FALSE / TRUE by false / true for bool return value.

Signed-off-by: Stefan Weil <[email protected]>
  • Loading branch information
stweil committed Oct 16, 2018
1 parent 1730b8c commit 7c2af45
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/classify/kdtree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -460,8 +460,8 @@ bool KDTreeSearch::BoxIntersectsSearch(float *lower, float *upper) {
float *query = query_point_;
// Compute the sum in higher precision.
double total_distance = 0.0;
double radius_squared =
results_.max_insertable_key() * results_.max_insertable_key();
double radius_squared = static_cast<double>(results_.max_insertable_key()) *
results_.max_insertable_key();
PARAM_DESC *dim = tree_->KeyDesc;

for (int i = tree_->KeySize; i > 0; i--, dim++, query++, lower++, upper++) {
Expand All @@ -486,11 +486,12 @@ bool KDTreeSearch::BoxIntersectsSearch(float *lower, float *upper) {
dimension_distance = std::min(dimension_distance, wrap_distance);
}

total_distance += dimension_distance * dimension_distance;
total_distance +=
static_cast<double>(dimension_distance) * dimension_distance;
if (total_distance >= radius_squared)
return FALSE;
return false;
}
return TRUE;
return true;
}


Expand Down

0 comments on commit 7c2af45

Please sign in to comment.