Skip to content

Commit

Permalink
expression: change the round rule for approximate value to `round to …
Browse files Browse the repository at this point in the history
…nearest even` (#21324) (#21628)

Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
  • Loading branch information
ti-srebot authored Jan 26, 2021
1 parent 1878b9d commit 89622d1
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 16 deletions.
4 changes: 2 additions & 2 deletions expression/builtin_cast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -488,8 +488,8 @@ func (s *testEvaluatorSuite) TestCastFuncSig(c *C) {
// cast real as int.
{
&Column{RetType: types.NewFieldType(mysql.TypeDouble), Index: 0},
1,
chunk.MutRowFromDatums([]types.Datum{types.NewFloat64Datum(1)}),
2,
chunk.MutRowFromDatums([]types.Datum{types.NewFloat64Datum(2.5)}),
},
// cast Time as int.
{
Expand Down
2 changes: 2 additions & 0 deletions expression/builtin_math_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,8 @@ func (s *testEvaluatorSuite) TestRound(c *C) {
{[]interface{}{1.298, 1}, 1.3},
{[]interface{}{1.298}, 1},
{[]interface{}{1.298, 0}, 1},
{[]interface{}{-1.5, 0}, -2},
{[]interface{}{1.5, 0}, 2},
{[]interface{}{23.298, -1}, 20},
{[]interface{}{newDec("-1.23")}, newDec("-1")},
{[]interface{}{newDec("-1.23"), 1}, newDec("-1.2")},
Expand Down
2 changes: 1 addition & 1 deletion expression/builtin_time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2166,7 +2166,7 @@ func (s *testEvaluatorSuite) TestMakeTime(c *C) {

{[]interface{}{0, 58.4, 0}, "00:58:00"},
{[]interface{}{0, "58.4", 0}, "00:58:00"},
{[]interface{}{0, 58.5, 1}, "00:59:01"},
{[]interface{}{0, 58.5, 1}, "00:58:01"},
{[]interface{}{0, "58.5", 1}, "00:58:01"},
{[]interface{}{0, 59.5, 1}, nil},
{[]interface{}{0, "59.5", 1}, "00:59:01"},
Expand Down
2 changes: 1 addition & 1 deletion expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ func (s *testIntegrationSuite2) TestMathBuiltin(c *C) {

// for round
result = tk.MustQuery("SELECT ROUND(2.5), ROUND(-2.5), ROUND(25E-1);")
result.Check(testkit.Rows("3 -3 3")) // TODO: Should be 3 -3 2
result.Check(testkit.Rows("3 -3 2"))
result = tk.MustQuery("SELECT ROUND(2.5, NULL), ROUND(NULL, 4), ROUND(NULL, NULL), ROUND(NULL);")
result.Check(testkit.Rows("<nil> <nil> <nil> <nil>"))
result = tk.MustQuery("SELECT ROUND('123.4'), ROUND('123e-2');")
Expand Down
2 changes: 1 addition & 1 deletion types/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -970,7 +970,7 @@ func (s *testTypeConvertSuite) TestConvertJSONToInt(c *C) {
{`[]`, 0},
{`3`, 3},
{`-3`, -3},
{`4.5`, 5},
{`4.5`, 4},
{`true`, 1},
{`false`, 0},
{`null`, 0},
Expand Down
8 changes: 4 additions & 4 deletions types/etc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,14 @@ func (s *testTypeEtcSuite) TestRoundFloat(c *C) {
Input float64
Expect float64
}{
{2.5, 3},
{2.5, 2},
{1.5, 2},
{0.5, 1},
{0.5, 0},
{0.49999999999999997, 0},
{0, 0},
{-0.49999999999999997, 0},
{-0.5, -1},
{-2.5, -3},
{-0.5, 0},
{-2.5, -2},
{-1.5, -2},
}

Expand Down
10 changes: 3 additions & 7 deletions types/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,12 @@ import (
"github.com/pingcap/errors"
)

// RoundFloat rounds float val to the nearest integer value with float64 format, like MySQL Round function.
// RoundFloat rounds float val to the nearest even integer value with float64 format, like MySQL Round function.
// RoundFloat uses default rounding mode, see https://dev.mysql.com/doc/refman/5.7/en/precision-math-rounding.html
// so rounding use "round half away from zero".
// so rounding use "round to nearest even".
// e.g, 1.5 -> 2, -1.5 -> -2.
func RoundFloat(f float64) float64 {
if math.Abs(f) < 0.5 {
return 0
}

return math.Trunc(f + math.Copysign(0.5, f))
return math.RoundToEven(f)
}

// Round rounds the argument f to dec decimal places.
Expand Down

0 comments on commit 89622d1

Please sign in to comment.