rect: Use more efficient float calculations for ceil, floor

This fixes warnings from LGTM:

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

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

Replace also old C++ type casts by static_cast.

Signed-off-by: Stefan Weil <sw@weilnetz.de>
This commit is contained in:
Stefan Weil 2018-10-06 18:50:56 +02:00
parent 1e4768c1f5
commit f264464ec6

View File

@ -21,7 +21,7 @@
#define RECT_H #define RECT_H
#include <algorithm> // for std::max, std::min #include <algorithm> // for std::max, std::min
#include <cmath> // for ceil, floor #include <cmath> // for std::ceil, std::floor
#include <cstdint> // for INT16_MAX #include <cstdint> // for INT16_MAX
#include <cstdio> // for FILE #include <cstdio> // for FILE
#include "platform.h" // for DLLSYM #include "platform.h" // for DLLSYM
@ -162,29 +162,33 @@ class DLLSYM TBOX { // bounding box
void move( // move box void move( // move box
const FCOORD vec) { // by float vector const FCOORD vec) { // by float vector
bot_left.set_x ((int16_t) floor (bot_left.x () + vec.x ())); bot_left.set_x(static_cast<int16_t>(std::floor(bot_left.x() + vec.x())));
// round left // round left
bot_left.set_y ((int16_t) floor (bot_left.y () + vec.y ())); bot_left.set_y(static_cast<int16_t>(std::floor(bot_left.y() + vec.y())));
// round down // round down
top_right.set_x ((int16_t) ceil (top_right.x () + vec.x ())); top_right.set_x(static_cast<int16_t>(std::ceil(top_right.x() + vec.x())));
// round right // round right
top_right.set_y ((int16_t) ceil (top_right.y () + vec.y ())); top_right.set_y(static_cast<int16_t>(std::ceil(top_right.y() + vec.y())));
// round up // round up
} }
void scale( // scale box void scale( // scale box
const float f) { // by multiplier const float f) { // by multiplier
bot_left.set_x ((int16_t) floor (bot_left.x () * f)); // round left // round left
bot_left.set_y ((int16_t) floor (bot_left.y () * f)); // round down bot_left.set_x(static_cast<int16_t>(std::floor(bot_left.x() * f)));
top_right.set_x ((int16_t) ceil (top_right.x () * f)); // round right // round down
top_right.set_y ((int16_t) ceil (top_right.y () * f)); // round up bot_left.set_y(static_cast<int16_t>(std::floor(bot_left.y() * f)));
// round right
top_right.set_x(static_cast<int16_t>(std::ceil(top_right.x() * f)));
// round up
top_right.set_y(static_cast<int16_t>(std::ceil(top_right.y() * f)));
} }
void scale( // scale box void scale( // scale box
const FCOORD vec) { // by float vector const FCOORD vec) { // by float vector
bot_left.set_x ((int16_t) floor (bot_left.x () * vec.x ())); bot_left.set_x(static_cast<int16_t>(std::floor(bot_left.x() * vec.x())));
bot_left.set_y ((int16_t) floor (bot_left.y () * vec.y ())); bot_left.set_y(static_cast<int16_t>(std::floor(bot_left.y() * vec.y())));
top_right.set_x ((int16_t) ceil (top_right.x () * vec.x ())); top_right.set_x(static_cast<int16_t>(std::ceil(top_right.x() * vec.x())));
top_right.set_y ((int16_t) ceil (top_right.y () * vec.y ())); top_right.set_y(static_cast<int16_t>(std::ceil(top_right.y() * vec.y())));
} }
// rotate doesn't enlarge the box - it just rotates the bottom-left // rotate doesn't enlarge the box - it just rotates the bottom-left
@ -314,8 +318,10 @@ class DLLSYM TBOX { // bounding box
inline TBOX::TBOX( // constructor inline TBOX::TBOX( // constructor
const FCOORD pt // floating centre const FCOORD pt // floating centre
) { ) {
bot_left = ICOORD ((int16_t) floor (pt.x ()), (int16_t) floor (pt.y ())); bot_left = ICOORD(static_cast<int16_t>(std::floor(pt.x())),
top_right = ICOORD ((int16_t) ceil (pt.x ()), (int16_t) ceil (pt.y ())); static_cast<int16_t>(std::floor(pt.y())));
top_right = ICOORD(static_cast<int16_t>(std::ceil(pt.x())),
static_cast<int16_t>(std::ceil(pt.y())));
} }