Skip to content

Commit

Permalink
Remove special case for exp == 1
Browse files Browse the repository at this point in the history
Generalize tests
  • Loading branch information
jschaf committed Feb 5, 2024
1 parent 77b8b32 commit 8efe8c3
Show file tree
Hide file tree
Showing 2 changed files with 17 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 == 0 {
return float64(src.Int.Int64()), nil
case src.Exp == 1:
return float64(src.Int.Int64() * 10), nil
}

buf := make([]byte, 0, 32)
Expand Down
29 changes: 16 additions & 13 deletions numeric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,20 +445,23 @@ 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 i := 0; i < 1024; i++ {
n := pgtype.Numeric{
Int: big.NewInt(int64(i)),
Exp: 0,
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)
if got != want {
t.Fatalf("expected %d, got %f", i, got)
}
}
}

0 comments on commit 8efe8c3

Please sign in to comment.