Skip to content

Commit b26866b

Browse files
committed
intproto: Use more efficient float calculations for 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 06a8de0 commit b26866b

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

src/classify/intproto.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
-----------------------------------------------------------------------------*/
2121

2222
#include <algorithm>
23-
#include <cmath>
23+
#include <cmath> // for std::floor
2424
#include <cstdio>
2525
#include <cassert>
2626

@@ -117,7 +117,7 @@ FILL_SPEC;
117117
#define CircularIncrement(i,r) (((i) < (r) - 1)?((i)++):((i) = 0))
118118

119119
/** macro for mapping floats to ints without bounds checking */
120-
#define MapParam(P,O,N) (floor (((P) + (O)) * (N)))
120+
#define MapParam(P,O,N) (std::floor(((P) + (O)) * (N)))
121121

122122
/*---------------------------------------------------------------------------
123123
Private Function Prototypes
@@ -1205,11 +1205,11 @@ void FillPPCircularBits(uint32_t ParamTable[NUM_PP_BUCKETS][WERDS_PER_PP_VECTOR]
12051205
if (Spread > 0.5)
12061206
Spread = 0.5;
12071207

1208-
FirstBucket = (int) floor ((Center - Spread) * NUM_PP_BUCKETS);
1208+
FirstBucket = static_cast<int>(std::floor((Center - Spread) * NUM_PP_BUCKETS));
12091209
if (FirstBucket < 0)
12101210
FirstBucket += NUM_PP_BUCKETS;
12111211

1212-
LastBucket = (int) floor ((Center + Spread) * NUM_PP_BUCKETS);
1212+
LastBucket = static_cast<int>(std::floor((Center + Spread) * NUM_PP_BUCKETS));
12131213
if (LastBucket >= NUM_PP_BUCKETS)
12141214
LastBucket -= NUM_PP_BUCKETS;
12151215
if (debug) tprintf("Circular fill from %d to %d", FirstBucket, LastBucket);
@@ -1243,11 +1243,11 @@ void FillPPLinearBits(uint32_t ParamTable[NUM_PP_BUCKETS][WERDS_PER_PP_VECTOR],
12431243
int Bit, float Center, float Spread, bool debug) {
12441244
int i, FirstBucket, LastBucket;
12451245

1246-
FirstBucket = (int) floor ((Center - Spread) * NUM_PP_BUCKETS);
1246+
FirstBucket = static_cast<int>(std::floor((Center - Spread) * NUM_PP_BUCKETS));
12471247
if (FirstBucket < 0)
12481248
FirstBucket = 0;
12491249

1250-
LastBucket = (int) floor ((Center + Spread) * NUM_PP_BUCKETS);
1250+
LastBucket = static_cast<int>(std::floor((Center + Spread) * NUM_PP_BUCKETS));
12511251
if (LastBucket >= NUM_PP_BUCKETS)
12521252
LastBucket = NUM_PP_BUCKETS - 1;
12531253

@@ -1736,7 +1736,7 @@ int TruncateParam(float Param, int Min, int Max, char *Id) {
17361736
Id, Param, Max);
17371737
Param = Max;
17381738
}
1739-
return static_cast<int>(floor(Param));
1739+
return static_cast<int>(std::floor(Param));
17401740
} /* TruncateParam */
17411741

17421742

0 commit comments

Comments
 (0)