Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix numeric to float64 conversion #212

Merged
merged 3 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion numeric.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,8 +448,11 @@ func (src *Numeric) toFloat64() (float64, error) {
return math.Inf(-1), nil
}

if src.Exp == 1 {
switch {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This optimization isn't present in pgx@v5 so I don't think there's anything to fix in v5.

https://github.com/jackc/pgx/blob/20bf953a17fc7cf6619536a87bd285b9273dbcf9/pgtype/numeric.go#L66

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please double-check my logic. I think src.Int is normalized, so this optimization is valid.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there is a danger of integer overflow in float64(src.Int.Int64() * 10). I think it should be float64(src.Int.Int64()) * 10 instead. But more so, is that branch even necessary / valuable. I assume it would work without it.

Copy link
Contributor Author

@jschaf jschaf Feb 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed the case for exp == 1 since you're right; it's not necessary. I originally kept that case since I thought it was important for the original optimization.

If you wanted to get fancy, you could implement it for all positive exponents, but I'll leave just the case for exp == 0 for now.

	if src.Exp >= 0 {
		return float64(src.Int.Int64()) * math.Pow10(int(src.Exp)), nil
	}

case 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
20 changes: 20 additions & 0 deletions numeric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ func TestNumericEncodeDecodeBinary(t *testing.T) {
123,
0.000012345,
1.00002345,
50.000000,
math.NaN(),
float32(math.NaN()),
math.Inf(1),
Expand Down Expand Up @@ -442,3 +443,22 @@ func TestNumericSmallNegativeValues(t *testing.T) {
t.Fatalf("expected %s, got %s", "-0.000123", s)
}
}

// https://github.com/jackc/pgtype/issues/210
func TestNumericFloat64(t *testing.T) {
n := pgtype.Numeric{
Int: big.NewInt(5),
Exp: 1,
Status: pgtype.Present,
}

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

if f != 50.0 {
t.Fatalf("expected %s, got %f", "50", f)
}
}
Loading