Skip to content

Commit

Permalink
chop: Use more efficient float calculations for sqrt
Browse files Browse the repository at this point in the history
This fixes warnings from LGTM:

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

While the sqrt function always calculates with double, here the
overloaded std::sqrt can be used to handle the float arguments
more efficiently.

Replace also an old C++ type cast by a static_cast.

Signed-off-by: Stefan Weil <[email protected]>
  • Loading branch information
stweil committed Oct 6, 2018
1 parent f264464 commit 819c43d
Showing 1 changed file with 1 addition and 2 deletions.
3 changes: 1 addition & 2 deletions src/wordrec/chop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,14 @@ int Wordrec::angle_change(EDGEPT *point1, EDGEPT *point2, EDGEPT *point3) {
VECTOR vector2;

int angle;
float length;

/* Compute angle */
vector1.x = point2->pos.x - point1->pos.x;
vector1.y = point2->pos.y - point1->pos.y;
vector2.x = point3->pos.x - point2->pos.x;
vector2.y = point3->pos.y - point2->pos.y;
/* Use cross product */
length = (float)sqrt((float)LENGTH(vector1) * LENGTH(vector2));
float length = std::sqrt(static_cast<float>(LENGTH(vector1)) * LENGTH(vector2));
if ((int) length == 0)
return (0);
angle = static_cast<int>(floor(asin(CROSS (vector1, vector2) /
Expand Down

0 comments on commit 819c43d

Please sign in to comment.