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

Numeric type is broken for small negative values #210

Closed
kirk-anchor opened this issue Jan 4, 2024 · 4 comments
Closed

Numeric type is broken for small negative values #210

kirk-anchor opened this issue Jan 4, 2024 · 4 comments

Comments

@kirk-anchor
Copy link

pgtype.Numeric does not work correctly for small negative values, e.g. -0.000123

	n := pgtype.Numeric{}
	n.Set("-0.000123")
	s := ""
	n.AssignTo(&s)
	fmt.Println(s)

Results in: 0.00-123 instead of -0.000123

@kirk-anchor
Copy link
Author

Thanks for the fix! pggen still uses v4 and string conversion was needed for using numerics in composite types due to a pggen bug jschaf/pggen#102

@jschaf
Copy link
Contributor

jschaf commented Feb 1, 2024

In an ironic turn of fate (as the pggen author), I had to skip pgtype v1.14.1 which includes fdca434.

Upgrading pgtype to v1.14.1 caused one of our tests to fail. A Postgres numeric value of 50 was decoded into a float64 as 5. Interestingly, it only broke a few tests so it's probably subtle.

I'll try to reproduce to see if it's something we're doing or something in pgtype.

jschaf added a commit to jschaf/pgtype that referenced this issue Feb 1, 2024
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
@jschaf
Copy link
Contributor

jschaf commented Feb 1, 2024

@jackc This looks like a pretty bad bug. I think pgtype.Numeric to float64 conversion is wrong for all values with an exponent of 1 divisible by 10, meaning [10, 20, 30, ..., 90]

jackc pushed a commit that referenced this issue Feb 6, 2024
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.

#210
@jackc
Copy link
Owner

jackc commented Feb 6, 2024

Thanks @jschaf. Fix is merged and new release v1.14.2 is tagged.

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

No branches or pull requests

3 participants