Skip to content

Commit

Permalink
intproto: Use more efficient float calculations for floor
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 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>
  • Loading branch information
stweil committed Oct 6, 2018
1 parent 06a8de0 commit b26866b
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/classify/intproto.cpp
Expand Up @@ -20,7 +20,7 @@
-----------------------------------------------------------------------------*/

#include <algorithm>
#include <cmath>
#include <cmath> // for std::floor
#include <cstdio>
#include <cassert>

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

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

/*---------------------------------------------------------------------------
Private Function Prototypes
Expand Down Expand Up @@ -1205,11 +1205,11 @@ void FillPPCircularBits(uint32_t ParamTable[NUM_PP_BUCKETS][WERDS_PER_PP_VECTOR]
if (Spread > 0.5)
Spread = 0.5;

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

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

FirstBucket = (int) floor ((Center - Spread) * NUM_PP_BUCKETS);
FirstBucket = static_cast<int>(std::floor((Center - Spread) * NUM_PP_BUCKETS));
if (FirstBucket < 0)
FirstBucket = 0;

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

Expand Down Expand Up @@ -1736,7 +1736,7 @@ int TruncateParam(float Param, int Min, int Max, char *Id) {
Id, Param, Max);
Param = Max;
}
return static_cast<int>(floor(Param));
return static_cast<int>(std::floor(Param));
} /* TruncateParam */


Expand Down

0 comments on commit b26866b

Please sign in to comment.