Skip to content

Commit f264464

Browse files
committed
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>
1 parent 1e4768c commit f264464

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

src/ccstruct/rect.h

+21-15
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#define RECT_H
2222

2323
#include <algorithm> // for std::max, std::min
24-
#include <cmath> // for ceil, floor
24+
#include <cmath> // for std::ceil, std::floor
2525
#include <cstdint> // for INT16_MAX
2626
#include <cstdio> // for FILE
2727
#include "platform.h" // for DLLSYM
@@ -162,29 +162,33 @@ class DLLSYM TBOX { // bounding box
162162

163163
void move( // move box
164164
const FCOORD vec) { // by float vector
165-
bot_left.set_x ((int16_t) floor (bot_left.x () + vec.x ()));
165+
bot_left.set_x(static_cast<int16_t>(std::floor(bot_left.x() + vec.x())));
166166
// round left
167-
bot_left.set_y ((int16_t) floor (bot_left.y () + vec.y ()));
167+
bot_left.set_y(static_cast<int16_t>(std::floor(bot_left.y() + vec.y())));
168168
// round down
169-
top_right.set_x ((int16_t) ceil (top_right.x () + vec.x ()));
169+
top_right.set_x(static_cast<int16_t>(std::ceil(top_right.x() + vec.x())));
170170
// round right
171-
top_right.set_y ((int16_t) ceil (top_right.y () + vec.y ()));
171+
top_right.set_y(static_cast<int16_t>(std::ceil(top_right.y() + vec.y())));
172172
// round up
173173
}
174174

175175
void scale( // scale box
176176
const float f) { // by multiplier
177-
bot_left.set_x ((int16_t) floor (bot_left.x () * f)); // round left
178-
bot_left.set_y ((int16_t) floor (bot_left.y () * f)); // round down
179-
top_right.set_x ((int16_t) ceil (top_right.x () * f)); // round right
180-
top_right.set_y ((int16_t) ceil (top_right.y () * f)); // round up
177+
// round left
178+
bot_left.set_x(static_cast<int16_t>(std::floor(bot_left.x() * f)));
179+
// round down
180+
bot_left.set_y(static_cast<int16_t>(std::floor(bot_left.y() * f)));
181+
// round right
182+
top_right.set_x(static_cast<int16_t>(std::ceil(top_right.x() * f)));
183+
// round up
184+
top_right.set_y(static_cast<int16_t>(std::ceil(top_right.y() * f)));
181185
}
182186
void scale( // scale box
183187
const FCOORD vec) { // by float vector
184-
bot_left.set_x ((int16_t) floor (bot_left.x () * vec.x ()));
185-
bot_left.set_y ((int16_t) floor (bot_left.y () * vec.y ()));
186-
top_right.set_x ((int16_t) ceil (top_right.x () * vec.x ()));
187-
top_right.set_y ((int16_t) ceil (top_right.y () * vec.y ()));
188+
bot_left.set_x(static_cast<int16_t>(std::floor(bot_left.x() * vec.x())));
189+
bot_left.set_y(static_cast<int16_t>(std::floor(bot_left.y() * vec.y())));
190+
top_right.set_x(static_cast<int16_t>(std::ceil(top_right.x() * vec.x())));
191+
top_right.set_y(static_cast<int16_t>(std::ceil(top_right.y() * vec.y())));
188192
}
189193

190194
// rotate doesn't enlarge the box - it just rotates the bottom-left
@@ -314,8 +318,10 @@ class DLLSYM TBOX { // bounding box
314318
inline TBOX::TBOX( // constructor
315319
const FCOORD pt // floating centre
316320
) {
317-
bot_left = ICOORD ((int16_t) floor (pt.x ()), (int16_t) floor (pt.y ()));
318-
top_right = ICOORD ((int16_t) ceil (pt.x ()), (int16_t) ceil (pt.y ()));
321+
bot_left = ICOORD(static_cast<int16_t>(std::floor(pt.x())),
322+
static_cast<int16_t>(std::floor(pt.y())));
323+
top_right = ICOORD(static_cast<int16_t>(std::ceil(pt.x())),
324+
static_cast<int16_t>(std::ceil(pt.y())));
319325
}
320326

321327

0 commit comments

Comments
 (0)