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

Conversation

jschaf
Copy link
Contributor

@jschaf jschaf commented Feb 1, 2024

A previous optimization in 50933c0 had a logic bug when converting pgtype.Numeric to a float64:

        // BUG: An exponent of one means multiply by 10.
	if src.Exp == 1 {
		return float64(src.Int.Int64()), nil
	}

I think the current pgtype.Numeric float64 conversion is incorrect for integers with an exponent of 1, that are divisible by 10, meaning [10, 20, 30, ..., 90].

#210

A previous optimization in 50933c0 had a logic bug when converting
pgtype.Numeric to a float64:

```
	if src.Exp == 1 {
		return float64(src.Int.Int64()), nil
	}
```

An exponent of one means multiply by 10.

jackc#210
numeric.go Outdated
@@ -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
	}

Generalize tests and make it exhaustive.
@jschaf jschaf requested a review from jackc February 5, 2024 20:12
@jackc jackc merged commit d0df6f7 into jackc:master Feb 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants