diff --git a/src/util/inc/tcompare.h b/src/util/inc/tcompare.h index 5e951923c1513a4e90b8d9cccea15cbeb5664ee5..612ce7ede0976764843a16002c7770cfa2a6bbb7 100644 --- a/src/util/inc/tcompare.h +++ b/src/util/inc/tcompare.h @@ -27,6 +27,13 @@ extern "C" { #define TSDB_PATTERN_NOWILDCARDMATCH 2 #define TSDB_PATTERN_STRING_MAX_LEN 20 +#define FLT_COMPAR_TOL_FACTOR 4 +#define FLT_EQUAL(_x, _y) (fabs((_x) - (_y)) <= (FLT_COMPAR_TOL_FACTOR * FLT_EPSILON)) +#define FLT_GREATER(_x, _y) (!FLT_EQUAL((_x), (_y)) && ((_x) > (_y))) +#define FLT_LESS(_x, _y) (!FLT_EQUAL((_x), (_y)) && ((_x) < (_y))) +#define FLT_GREATEREQUAL(_x, _y) (FLT_EQUAL((_x), (_y)) || ((_x) > (_y))) +#define FLT_LESSEQUAL(_x, _y) (FLT_EQUAL((_x), (_y)) || ((_x) < (_y))) + #define PATTERN_COMPARE_INFO_INITIALIZER { '%', '_' } typedef struct SPatternCompareInfo { diff --git a/src/util/src/tcompare.c b/src/util/src/tcompare.c index f91d13e6d0a4310181594b840631249bd1bcade8..e953f4c4649b6f906b8517a7165600d246da2176 100644 --- a/src/util/src/tcompare.c +++ b/src/util/src/tcompare.c @@ -90,13 +90,10 @@ int32_t compareFloatVal(const void *pLeft, const void *pRight) { if (isnan(p2)) { return 1; } - - float ret = p1 - p2; - if (fabs(ret) < FLT_EPSILON) { + if (FLT_EQUAL(p1, p2)) { return 0; - } else { - return ret > 0? 1 : -1; - } + } + return FLT_GREATER(p1, p2) ? 1: -1; } int32_t compareDoubleVal(const void *pLeft, const void *pRight) { @@ -114,13 +111,10 @@ int32_t compareDoubleVal(const void *pLeft, const void *pRight) { if (isnan(p2)) { return 1; } - - double ret = p1 - p2; - if (fabs(ret) < FLT_EPSILON) { + if (FLT_EQUAL(p1, p2)) { return 0; - } else { - return ret > 0? 1 : -1; - } + } + return FLT_GREATER(p1, p2) ? 1: -1; } int32_t compareLenPrefixedStr(const void *pLeft, const void *pRight) { diff --git a/src/util/src/thashutil.c b/src/util/src/thashutil.c index a0f3e58577dd402c371aea1533664e867124bdf1..e4b900f73857faac7c3a820218d99f47f14c4fed 100644 --- a/src/util/src/thashutil.c +++ b/src/util/src/thashutil.c @@ -26,8 +26,7 @@ (h) *= 0x85ebca6b; \ (h) ^= (h) >> 13; \ (h) *= 0xc2b2ae35; \ - (h) ^= (h) >> 16; \ - } while (0) + (h) ^= (h) >> 16; } while (0) uint32_t MurmurHash3_32(const char *key, uint32_t len) { const uint8_t *data = (const uint8_t *)key; @@ -84,22 +83,24 @@ uint32_t taosFloatHash(const char *key, uint32_t UNUSED_PARAM(len)) { if (isnan(f)) { return 0x7fc00000; } - if (fabs(f - 0.0) < FLT_EPSILON) { + + if (FLT_EQUAL(f, 0.0)) { return 0; - } - - return *(uint32_t *)(key); + } + int t = (int)round(f); + return (uint32_t)t; } uint32_t taosDoubleHash(const char *key, uint32_t UNUSED_PARAM(len)) { double f = GET_DOUBLE_VAL(key); if (isnan(f)) { return 0x7fc00000; } - if (fabs(f - 0.0) < FLT_EPSILON) { + + if (FLT_EQUAL(f, 0.0)) { return 0; - } - return *(uint32_t *)(key); - + } + int t = (int)(round(f)); + return (uint32_t)t; } uint32_t taosIntHash_64(const char *key, uint32_t UNUSED_PARAM(len)) { uint64_t val = *(uint64_t *)key;