Skip to content

Commit

Permalink
Fix mul and minus
Browse files Browse the repository at this point in the history
  • Loading branch information
wjhuang2016 committed Jul 16, 2019
1 parent 39624bf commit ed7413d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
6 changes: 4 additions & 2 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2839,7 +2839,10 @@ func (s *testIntegrationSuite) TestArithmeticBuiltin(c *C) {
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "[types:1690]BIGINT UNSIGNED value is out of range in '(18446744073709551615 - -1)'")
c.Assert(rs.Close(), IsNil)
tk.MustQuery(`select cast(-3 as unsigned) - cast(-1 as signed);`).Check(testkit.Rows("18446744073709551614"))
tk.MustQuery("select 1.11 - 1.11;").Check(testkit.Rows("0.00"))

// for multiply
tk.MustQuery("select 1234567890 * 1234567890").Check(testkit.Rows("1524157875019052100"))
rs, err = tk.Exec("select 1234567890 * 12345671890")
c.Assert(err, IsNil)
Expand All @@ -2866,8 +2869,7 @@ func (s *testIntegrationSuite) TestArithmeticBuiltin(c *C) {
_, err = session.GetRows4Test(ctx, tk.Se, rs)
c.Assert(terror.ErrorEqual(err, types.ErrOverflow), IsTrue)
c.Assert(rs.Close(), IsNil)
result = tk.MustQuery(`select cast(-3 as unsigned) - cast(-1 as signed);`)
result.Check(testkit.Rows("18446744073709551614"))
tk.MustQuery("select 0.0 * -1;").Check(testkit.Rows("0.0"))

tk.MustExec("DROP TABLE IF EXISTS t;")
tk.MustExec("CREATE TABLE t(a DECIMAL(4, 2), b DECIMAL(5, 3));")
Expand Down
15 changes: 10 additions & 5 deletions types/mydecimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@ var (
zeroMyDecimal = MyDecimal{}
)

// get the zero of MyDecimal with the specified result fraction digits
func zeroMyDecimalWithFrac(frac int8) MyDecimal {
zero := MyDecimal{}
zero.resultFrac = frac
return zero
}

// add adds a and b and carry, returns the sum and new carry.
func add(a, b, carry int32) (int32, int32) {
sum := a + b + carry
Expand Down Expand Up @@ -1556,7 +1563,7 @@ func doSub(from1, from2, to *MyDecimal) (cmp int, err error) {
if to == nil {
return 0, nil
}
*to = zeroMyDecimal
*to = zeroMyDecimalWithFrac(to.resultFrac)
return 0, nil
}
}
Expand Down Expand Up @@ -1911,7 +1918,7 @@ func DecimalMul(from1, from2, to *MyDecimal) error {
idx++
/* We got decimal zero */
if idx == end {
*to = zeroMyDecimal
*to = zeroMyDecimalWithFrac(to.resultFrac)
break
}
}
Expand Down Expand Up @@ -2010,9 +2017,7 @@ func doDivMod(from1, from2, to, mod *MyDecimal, fracIncr int) error {
}
if prec1 <= 0 {
/* short-circuit everything: from1 == 0 */
resultFrac := to.resultFrac
*to = zeroMyDecimal
to.resultFrac = resultFrac
*to = zeroMyDecimalWithFrac(to.resultFrac)
return nil
}
prec1 -= countLeadingZeroes((prec1-1)%digitsPerWord, from1.wordBuf[idx1])
Expand Down

0 comments on commit ed7413d

Please sign in to comment.