Skip to content

Commit

Permalink
*: fix issue#1089 and add tests (pingcap#1106)
Browse files Browse the repository at this point in the history
  • Loading branch information
zimulala committed Apr 18, 2016
1 parent 8d877cc commit 895e9a0
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
18 changes: 18 additions & 0 deletions session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,24 @@ func (s *testSessionSuite) TestIssue986(c *C) {
c.Assert(err, IsNil)
}

func (s *testSessionSuite) TestIssue1089(c *C) {
defer testleak.AfterTest(c)()
store := newStore(c, s.dbName)
se := newSession(c, store, s.dbName)

r := mustExecSQL(c, se, "select cast(0.5 as unsigned)")
row, err := r.Next()
c.Assert(err, IsNil)
match(c, row.Data, 1)
r = mustExecSQL(c, se, "select cast(-0.5 as signed)")
row, err = r.Next()
c.Assert(err, IsNil)
match(c, row.Data, -1)

err = store.Close()
c.Assert(err, IsNil)
}

func (s *testSessionSuite) TestSelectForUpdate(c *C) {
defer testleak.AfterTest(c)()
store := newStore(c, s.dbName)
Expand Down
10 changes: 6 additions & 4 deletions util/types/etc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,12 +258,14 @@ func (s *testTypeEtcSuite) TestRoundFloat(c *C) {
Input float64
Expect float64
}{
{2.5, 2},
{2.5, 3},
{1.5, 2},
{0.5, 0},
{0.5, 1},
{0.49999999999999997, 0},
{0, 0},
{-0.5, 0},
{-2.5, -2},
{-0.49999999999999997, 0},
{-0.5, -1},
{-2.5, -3},
{-1.5, -2},
}

Expand Down
15 changes: 8 additions & 7 deletions util/types/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,16 @@ import (
"github.com/pingcap/tidb/mysql"
)

// RoundFloat rounds float val to the nearest integer value with float64 format, like GNU rint function.
// RoundFloat uses default rounding mode, see http://www.gnu.org/software/libc/manual/html_node/Rounding.html
// so we will choose the even number if the result is midway between two representable value.
// e.g, 1.5 -> 2, 2.5 -> 2.
// RoundFloat rounds float val to the nearest 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".
// e.g, 1.5 -> 2, -1.5 -> -2.
func RoundFloat(f float64) float64 {
if math.Remainder(f, 1.0) < 0 {
return math.Ceil(f)
if math.Abs(f) < 0.5 {
return 0
}
return math.Floor(f)

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

func getMaxFloat(flen int, decimal int) float64 {
Expand Down

0 comments on commit 895e9a0

Please sign in to comment.