-
Notifications
You must be signed in to change notification settings - Fork 112
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
Conversation
numeric.go
Outdated
@@ -448,8 +448,11 @@ func (src *Numeric) toFloat64() (float64, error) { | |||
return math.Inf(-1), nil | |||
} | |||
|
|||
if src.Exp == 1 { | |||
switch { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
8efe8c3
to
91d2ae3
Compare
A previous optimization in 50933c0 had a logic bug when converting pgtype.Numeric to a float64:
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