Skip to content

Commit

Permalink
Remove special case for exp == 1
Browse files Browse the repository at this point in the history
Generalize tests and make it exhaustive.
  • Loading branch information
jschaf authored and jackc committed Feb 6, 2024
1 parent f620c0f commit 797890a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
5 changes: 1 addition & 4 deletions numeric.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,11 +448,8 @@ func (src *Numeric) toFloat64() (float64, error) {
return math.Inf(-1), nil
}

switch {
case src.Exp == 0:
if src.Exp == 1 {
return float64(src.Int.Int64()), nil
case src.Exp == 1:
return float64(src.Int.Int64() * 10), nil
}

buf := make([]byte, 0, 32)
Expand Down
32 changes: 19 additions & 13 deletions numeric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,20 +445,26 @@ func TestNumericSmallNegativeValues(t *testing.T) {
}

// https://github.com/jackc/pgtype/issues/210
func TestNumericFloat64(t *testing.T) {
n := pgtype.Numeric{
Int: big.NewInt(5),
Exp: 1,
Status: pgtype.Present,
}
func TestNumericFloat64FromIntegers(t *testing.T) {
for exp := -10; exp <= 10; exp++ {
for i := -100; i < 100; i++ {
n := pgtype.Numeric{
Int: big.NewInt(int64(i)),
Exp: int32(exp),
Status: pgtype.Present,
}

var f float64
err := n.AssignTo(&f)
if err != nil {
t.Fatal(err)
}
var got float64
err := n.AssignTo(&got)
if err != nil {
t.Fatal(err)
}

if f != 50.0 {
t.Fatalf("expected %s, got %f", "50", f)
want := float64(i) * math.Pow10(exp)
delta := math.Abs(want * 0.0000000001)
if math.Abs(got-want) > delta {
t.Fatalf("expected %f from %de%d, got %f", want, i, exp, got)
}
}
}
}

0 comments on commit 797890a

Please sign in to comment.