Skip to content

Commit

Permalink
Merge pull request #124 from nvanbenschoten/nvanbenschoten/remErrors
Browse files Browse the repository at this point in the history
apd: drop github.com/pkg/errors
  • Loading branch information
nvanbenschoten authored Sep 9, 2022
2 parents 06ef971 + 46c8453 commit e2030eb
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 35 deletions.
6 changes: 3 additions & 3 deletions condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
package apd

import (
"errors"
"fmt"
"strings"

"github.com/pkg/errors"
)

// Condition holds condition flags.
Expand Down Expand Up @@ -143,7 +143,7 @@ func (r Condition) String() string {
case Clamped:
s = "clamped"
default:
panic(errors.Errorf("unknown condition %d", i))
panic(fmt.Errorf("unknown condition %d", i))
}
names = append(names, s)
}
Expand Down
22 changes: 11 additions & 11 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
package apd

import (
"errors"
"fmt"
"math"

"github.com/pkg/errors"
)

// Context maintains options for Decimal operations. It can safely be used
Expand Down Expand Up @@ -116,7 +116,7 @@ func (c *Context) setAsNaN(d *Decimal, x, y *Decimal) (Condition, error) {
} else if y != nil && y.Form == NaN {
nan = y
} else {
return 0, errors.Errorf("no NaN value found; was shouldSetAsNaN called?")
return 0, errors.New("no NaN value found; was shouldSetAsNaN called?")
}
d.Set(nan)
var res Condition
Expand Down Expand Up @@ -149,7 +149,7 @@ func (c *Context) add(d, x, y *Decimal, subtract bool) (Condition, error) {
var tmp BigInt
a, b, s, err := upscale(x, y, &tmp)
if err != nil {
return 0, errors.Wrap(err, "add")
return 0, fmt.Errorf("add: %w", err)
}
d.Negative = xn
if xn == yn {
Expand Down Expand Up @@ -379,7 +379,7 @@ func (c *Context) QuoInteger(d, x, y *Decimal) (Condition, error) {
var tmp BigInt
a, b, _, err := upscale(x, y, &tmp)
if err != nil {
return 0, errors.Wrap(err, "QuoInteger")
return 0, fmt.Errorf("QuoInteger: %w", err)
}
d.Coeff.Quo(a, b)
d.Form = Finite
Expand Down Expand Up @@ -421,7 +421,7 @@ func (c *Context) Rem(d, x, y *Decimal) (Condition, error) {
var tmp1 BigInt
a, b, s, err := upscale(x, y, &tmp1)
if err != nil {
return 0, errors.Wrap(err, "Rem")
return 0, fmt.Errorf("Rem: %w", err)
}
var tmp2 BigInt
tmp2.QuoRem(a, b, &d.Coeff)
Expand Down Expand Up @@ -858,7 +858,7 @@ func (c *Context) Log10(d, x *Decimal) (Condition, error) {
var z Decimal
_, err := nc.Ln(&z, x)
if err != nil {
return 0, errors.Wrap(err, "ln")
return 0, fmt.Errorf("ln: %w", err)
}
nc.Precision = c.Precision

Expand Down Expand Up @@ -942,7 +942,7 @@ func (c *Context) Exp(d, x *Decimal) (Condition, error) {
nc := c.WithPrecision(cp)
nc.Rounding = RoundHalfEven
if _, err := nc.Quo(&r, x, &k); err != nil {
return 0, errors.Wrap(err, "Quo")
return 0, fmt.Errorf("Quo: %w", err)
}
var ra Decimal
ra.Abs(&r)
Expand All @@ -951,7 +951,7 @@ func (c *Context) Exp(d, x *Decimal) (Condition, error) {
// Stage 3
rf, err := ra.Float64()
if err != nil {
return 0, errors.Wrap(err, "r.Float64")
return 0, fmt.Errorf("r.Float64: %w", err)
}
pf := float64(p)
nf := math.Ceil((1.435*pf - 1.182) / math.Log10(pf/rf))
Expand Down Expand Up @@ -983,11 +983,11 @@ func (c *Context) Exp(d, x *Decimal) (Condition, error) {
var tmpE BigInt
ki, err := exp10(int64(t), &tmpE)
if err != nil {
return 0, errors.Wrap(err, "ki")
return 0, fmt.Errorf("ki: %w", err)
}
ires, err := nc.integerPower(d, &sum, ki)
if err != nil {
return 0, errors.Wrap(err, "integer power")
return 0, fmt.Errorf("integer power: %w", err)
}
res |= ires
nc.Precision = c.Precision
Expand Down
21 changes: 11 additions & 10 deletions decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
package apd

import (
"errors"
"fmt"
"strconv"
"strings"
"unsafe"

"database/sql/driver"
"github.com/pkg/errors"
)

// Decimal is an arbitrary-precision decimal. Its value is:
Expand Down Expand Up @@ -113,7 +114,7 @@ func (d *Decimal) setString(c *Context, s string) (Condition, error) {
// Until there are no parse errors, leave as NaN.
d.Form = NaN
if strings.HasPrefix(s, "-") || strings.HasPrefix(s, "+") {
return 0, errors.Errorf("could not parse: %s", orig)
return 0, fmt.Errorf("could not parse: %s", orig)
}
switch s {
case "infinity", "inf":
Expand All @@ -135,7 +136,7 @@ func (d *Decimal) setString(c *Context, s string) (Condition, error) {
// We ignore these digits, but must verify them.
_, err := strconv.ParseUint(s, 10, 64)
if err != nil {
return 0, errors.Wrapf(err, "parse payload: %s", s)
return 0, fmt.Errorf("parse payload: %s: %w", s, err)
}
}
return 0, nil
Expand All @@ -145,7 +146,7 @@ func (d *Decimal) setString(c *Context, s string) (Condition, error) {
if i := strings.IndexByte(s, 'e'); i >= 0 {
exp, err := strconv.ParseInt(s[i+1:], 10, 32)
if err != nil {
return 0, errors.Wrapf(err, "parse exponent: %s", s[i+1:])
return 0, fmt.Errorf("parse exponent: %s: %w", s[i+1:], err)
}
exps = append(exps, exp)
s = s[:i]
Expand All @@ -156,7 +157,7 @@ func (d *Decimal) setString(c *Context, s string) (Condition, error) {
s = s[:i] + s[i+1:]
}
if _, ok := d.Coeff.SetString(s, 10); !ok {
return 0, errors.Errorf("parse mantissa: %s", s)
return 0, fmt.Errorf("parse mantissa: %s", s)
}
// No parse errors, can now flag as finite.
d.Form = Finite
Expand Down Expand Up @@ -248,19 +249,19 @@ func (d *Decimal) SetFloat64(f float64) (*Decimal, error) {
// int64, an error is returned.
func (d *Decimal) Int64() (int64, error) {
if d.Form != Finite {
return 0, errors.Errorf("%s is not finite", d.String())
return 0, fmt.Errorf("%s is not finite", d.String())
}
var integ, frac Decimal
d.Modf(&integ, &frac)
if !frac.IsZero() {
return 0, errors.Errorf("%s: has fractional part", d.String())
return 0, fmt.Errorf("%s: has fractional part", d.String())
}
var ed ErrDecimal
if integ.Cmp(decimalMaxInt64) > 0 {
return 0, errors.Errorf("%s: greater than max int64", d.String())
return 0, fmt.Errorf("%s: greater than max int64", d.String())
}
if integ.Cmp(decimalMinInt64) < 0 {
return 0, errors.Errorf("%s: less than min int64", d.String())
return 0, fmt.Errorf("%s: less than min int64", d.String())
}
if err := ed.Err(); err != nil {
return 0, err
Expand Down Expand Up @@ -781,7 +782,7 @@ func (d *Decimal) Scan(src interface{}) error {
_, err := d.SetFloat64(src)
return err
default:
return errors.Errorf("could not convert %T to Decimal", src)
return fmt.Errorf("could not convert %T to Decimal", src)
}
}

Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module github.com/cockroachdb/apd/v3

go 1.13
go 1.17

require github.com/pkg/errors v0.8.0
require github.com/lib/pq v1.10.7
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/lib/pq v1.10.7 h1:p7ZhMD+KsSRozJr34udlUrhboJwWAgCg34+/ZZNvZZw=
github.com/lib/pq v1.10.7/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
5 changes: 2 additions & 3 deletions loop.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
package apd

import (
"fmt"
"math"

"github.com/pkg/errors"
)

type loop struct {
Expand Down Expand Up @@ -79,7 +78,7 @@ func (l *loop) done(z *Decimal) (bool, error) {
}
l.i++
if l.i == l.maxIterations {
return false, errors.Errorf(
return false, fmt.Errorf(
"%s %s: did not converge after %d iterations; prev,last result %s,%s delta %s precision: %d",
l.name, l.arg.String(), l.maxIterations, z.String(), l.prevZ.String(), l.delta.String(), l.precision,
)
Expand Down
9 changes: 5 additions & 4 deletions sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// implied. See the License for the specific language governing
// permissions and limitations under the License.

//go:build sql
// +build sql

package apd
Expand All @@ -34,8 +35,8 @@ func TestSQL(t *testing.T) {
if err != nil {
t.Fatal(err)
}
a, _, err := NewFromString("1234.567e5")
if err != nil {
var a Decimal
if _, _, err = a.SetString("1234.567e5"); err != nil {
t.Fatal(err)
}
if _, err := db.Exec("drop table if exists d"); err != nil {
Expand All @@ -52,14 +53,14 @@ func TestSQL(t *testing.T) {
}
var b, c, d Decimal
var nd NullDecimal
if err := db.QueryRow("select v, v::text, v::int, v::float, v from d").Scan(a, &b, &c, &d, &nd); err != nil {
if err := db.QueryRow("select v, v::text, v::int, v::float, v from d").Scan(&a, &b, &c, &d, &nd); err != nil {
t.Fatal(err)
}
want, _, err := NewFromString("123556700")
if err != nil {
t.Fatal(err)
}
for i, v := range []*Decimal{a, &b, &c, &d, &nd.Decimal} {
for i, v := range []*Decimal{&a, &b, &c, &d, &nd.Decimal} {
if v.Cmp(want) != 0 {
t.Fatalf("%d: unexpected: %s, want: %s", i, v.String(), want.String())
}
Expand Down

0 comments on commit e2030eb

Please sign in to comment.