Browse Source

YQL-17080 fix win build + adjust bounds

vvvv 1 year ago
parent
commit
5ffa940699

+ 3 - 1
ydb/library/yql/udfs/common/math/lib/round.h

@@ -50,7 +50,9 @@ inline std::optional<i64> NearbyIntImpl(double value, decltype(FE_DOWNWARD) mode
     ::fesetround(mode);
     ::fesetround(mode);
     auto res = ::nearbyint(value);
     auto res = ::nearbyint(value);
     ::fesetround(prevMode);
     ::fesetround(prevMode);
-    if (res < std::numeric_limits<i64>::min() || res > std::numeric_limits<i64>::max()) {
+    // cast to i64 gives wrong sign above 9223372036854774784
+    // lower bound is adjusted to -9223372036854774784 as well
+    if (res < double(std::numeric_limits<i64>::min() + 513) || res > double(std::numeric_limits<i64>::max() - 512)) {
         return {};
         return {};
     }
     }
    
    

+ 9 - 0
ydb/library/yql/udfs/common/math/lib/round_ut.cpp

@@ -58,4 +58,13 @@ Y_UNIT_TEST_SUITE(TRound) {
         UNIT_ASSERT_VALUES_EQUAL(*Rem(-14, -7), 0);
         UNIT_ASSERT_VALUES_EQUAL(*Rem(-14, -7), 0);
         UNIT_ASSERT(!Rem(-14, 0));
         UNIT_ASSERT(!Rem(-14, 0));
     }
     }
+
+    Y_UNIT_TEST(NearbyInt) {
+        const i64 maxV = 9223372036854774784ll;
+        const i64 minV = -9223372036854774784ll;
+        UNIT_ASSERT_VALUES_EQUAL((i64)(double)(maxV), maxV);
+        UNIT_ASSERT_VALUES_EQUAL((i64)(double)(minV), minV);
+
+        UNIT_ASSERT_VALUES_UNEQUAL((i64)(double)(maxV + 1), maxV + 1);
+    }
 }
 }