diff --git a/src/common/types/int128_t.cpp b/src/common/types/int128_t.cpp index 79aeb53358..a3191b1c7e 100644 --- a/src/common/types/int128_t.cpp +++ b/src/common/types/int128_t.cpp @@ -357,9 +357,9 @@ bool TryCastInt128Template(int128_t input, DST& result) { throw common::OverflowException( "Cast failed. Cannot cast " + Int128_t::ToString(input) + " to unsigned type."); } - if (input.low >= uint64_t(function::NumericLimits::maximum()) - + if (input.low >= function::NumericLimits::maximum() - uint64_t(function::NumericLimits::maximum())) { - result = static_cast(function::NumericLimits::maximum() - input.low) - 1; + result = -DST(function::NumericLimits::maximum() - input.low) - 1; return true; } break; @@ -520,6 +520,7 @@ bool castFloatingToInt128(REAL_T value, int128_t& result) { if (negative) { value = -value; } + value = std::nearbyint(value); result.low = (uint64_t)fmod(value, REAL_T(function::NumericLimits::maximum())); result.high = (uint64_t)(value / REAL_T(function::NumericLimits::maximum())); if (negative) { diff --git a/test/test_files/tinysnb/function/cast.test b/test/test_files/tinysnb/function/cast.test index 7c5f084480..f1d3ee58da 100644 --- a/test/test_files/tinysnb/function/cast.test +++ b/test/test_files/tinysnb/function/cast.test @@ -64,10 +64,10 @@ -LOG CastFromFLOAT -STATEMENT MATCH (p:person) WHERE p.ID=0 RETURN to_int16(p.height), to_int32(p.height), to_int64(p.height), to_int128(p.height), to_double(p.height) ---- 1 -2|2|2|1|1.731000 +2|2|2|2|1.731000 -STATEMENT MATCH (p:person) WHERE p.ID=0 RETURN cast(p.height, "uint8"), cast(p.height, "int16"), cast(p.height, "int32"), cast(p.height, "int64"), cast(p.height, "int128"), cast(p.height, "double"), cast(p.height, "float") ---- 1 -2|2|2|2|1|1.731000|1.731000 +2|2|2|2|2|1.731000|1.731000 -LOG CastFromDOUBLE -STATEMENT MATCH (p:person) WHERE p.ID=0 RETURN to_int16(p.eyeSight), to_int32(p.eyeSight), to_int64(p.eyeSight), to_int128(p.eyeSight), to_float(p.eyeSight) @@ -1088,3 +1088,10 @@ False -STATEMENT RETURN cast("[423, 321, 423]", "INT64[3]"), cast(null, "INT64[5]"), cast("[432.43214]", "FLOAT[1]"), cast("[4, -5]", "double[2]"), cast("[4234, 42312, 432, 1321]", "INT32[4]"), cast("[-32768]", "INT16[1]") ---- 1 [423,321,423]||[432.432129]|[4.000000,-5.000000]|[4234,42312,432,1321]|[-32768] +-STATEMENT Return cast(cast(-4324324, "int128"), "int64") +---- 1 +-4324324 + +-STATEMENT Return to_int64(to_int128(-4324324)) +---- 1 +-4324324