Skip to content

Commit b1ef543

Browse files
committed
ARROW-10234: [C++][Gandiva] Fix logic of round() for floats/decimals in Gandiva
This patch attempts to resolve the bug introduced by the addition of round() in ARROW-9641 round() for floats is returning incorrect results for some edge cases, like round(cast(1.55 as float), 1) gives 1.6, but it should be 1.5, since the result after casting 1.55 to float comes to 1.5499999523162842, due to inaccurate representation of floating point numbers in memory. Removing an intermediate explicit cast to float statement for a double value, which is used in subsequent computations, minimises the error introduced due to the incorrect representation.
1 parent d4cbc4b commit b1ef543

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

cpp/src/gandiva/precompiled/extended_math_ops.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,7 @@ POWER(float64, float64, float64)
116116
#define ROUND_DECIMAL(TYPE) \
117117
FORCE_INLINE \
118118
gdv_##TYPE round_##TYPE##_int32(gdv_##TYPE number, gdv_int32 out_scale) { \
119-
gdv_##TYPE scale_multiplier = \
120-
static_cast<gdv_##TYPE>(get_scale_multiplier(out_scale)); \
119+
gdv_float64 scale_multiplier = get_scale_multiplier(out_scale); \
121120
return static_cast<gdv_##TYPE>( \
122121
trunc(number * scale_multiplier + ((number > 0) ? 0.5 : -0.5)) / \
123122
scale_multiplier); \

cpp/src/gandiva/precompiled/extended_math_ops_test.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ TEST(TestExtendedMathOps, TestRoundDecimal) {
9393
EXPECT_FLOAT_EQ(round_float32_int32(-1234.4567f, 3), -1234.457f);
9494
EXPECT_FLOAT_EQ(round_float32_int32(-1234.4567f, -3), -1000);
9595
EXPECT_FLOAT_EQ(round_float32_int32(1234.4567f, 0), 1234);
96+
EXPECT_FLOAT_EQ(round_float32_int32(1.5499999523162842f, 1), 1.5f);
97+
EXPECT_FLOAT_EQ(round_float32_int32(static_cast<float>(1.55), 1), 1.5f);
98+
EXPECT_FLOAT_EQ(round_float32_int32(static_cast<float>(9.134123), 2), 9.13f);
99+
EXPECT_FLOAT_EQ(round_float32_int32(static_cast<float>(-1.923), 1), -1.9f);
96100

97101
VerifyFuzzyEquals(round_float64_int32(1234.789, 2), 1234.79);
98102
VerifyFuzzyEquals(round_float64_int32(1234.12345, -3), 1000);

0 commit comments

Comments
 (0)