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

GH-38395: [Go] fix rounding errors in decimal256 string functions #38426

Merged
merged 5 commits into from
Oct 24, 2023

Conversation

zeroshade
Copy link
Member

@zeroshade zeroshade commented Oct 23, 2023

Rationale for this change

Slight modifications to the ToString and FromString methods of decimal 256 to ensure proper accurate and proper conversion given precision and scale.

Are these changes tested?

Yes

@github-actions
Copy link

⚠️ GitHub issue #38395 has been automatically assigned in GitHub to PR creator.

@github-actions github-actions bot added awaiting changes Awaiting changes and removed awaiting committer review Awaiting committer review labels Oct 24, 2023
Comment on lines +285 to +289
if out.Signbit() {
out.Sub(out, pt5)
} else {
out.Add(out, pt5)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This is done because the round is a "round up", right?

Copy link
Member Author

Choose a reason for hiding this comment

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

more accurately it's a round away from zero

Copy link
Contributor

Choose a reason for hiding this comment

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

So -7.0 will become -7.0-0.5=-7.5 and round to -8.0?

Copy link
Member Author

Choose a reason for hiding this comment

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

Not quite, check the comment above the spot: we truncate the value to get an integer. So -7.0 with a scale of 0 becomes -7.5 and then we truncate it to -7. -7.5 becomes -8.0 and then we truncate it to -8.

A better example would be if we're given the value -0.75 with a scale of 1: The scale of 1 means that our final Decimal value is going to be the integer -8 because of rounding.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thank you for the explanation!

// Since we're going to truncate this to get an integer, we need to round
// the value instead because of edge cases so that we match how other implementations
// (e.g. C++) handles Decimal values. So if we're negative we'll subtract 0.5 and if
// we're positive we'll add 0.5.
p := (&big.Float{}).SetFloat64(float64PowersOfTen[scale+38])
p := (&big.Float{}).SetInt(scaleMultipliers[scale].BigInt())
Copy link
Contributor

Choose a reason for hiding this comment

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

A-ha! I was reviewing commit by commit :)

Copy link
Member

@bkietz bkietz left a comment

Choose a reason for hiding this comment

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

Mostly this looks good to me, just a couple comments on the tests

go/arrow/decimal256/decimal256_test.go Outdated Show resolved Hide resolved
go/arrow/decimal256/decimal256_test.go Show resolved Hide resolved
@zeroshade zeroshade requested a review from bkietz October 24, 2023 20:17
@github-actions github-actions bot added awaiting change review Awaiting change review awaiting changes Awaiting changes and removed awaiting changes Awaiting changes awaiting change review Awaiting change review labels Oct 24, 2023
@github-actions github-actions bot added awaiting merge Awaiting merge and removed awaiting changes Awaiting changes labels Oct 24, 2023
@zeroshade zeroshade merged commit 9e2d4aa into apache:main Oct 24, 2023
25 checks passed
@zeroshade zeroshade removed the awaiting merge Awaiting merge label Oct 24, 2023
@zeroshade zeroshade deleted the decimal256-rounding-issues branch October 24, 2023 21:12
JerAguilon pushed a commit to JerAguilon/arrow that referenced this pull request Oct 25, 2023
…ns (apache#38426)

### Rationale for this change
Slight modifications to the `ToString` and `FromString` methods of decimal 256 to ensure proper accurate and proper conversion given precision and scale.

### Are these changes tested?
Yes

* Closes: apache#38395

Authored-by: Matt Topol <[email protected]>
Signed-off-by: Matt Topol <[email protected]>
@conbench-apache-arrow
Copy link

After merging your PR, Conbench analyzed the 5 benchmarking runs that have been run so far on merge-commit 9e2d4aa.

There were no benchmark performance regressions. 🎉

The full Conbench report has more details. It also includes information about 4 possible false positives for unstable benchmarks that are known to sometimes produce them.

loicalleyne pushed a commit to loicalleyne/arrow that referenced this pull request Nov 13, 2023
…ns (apache#38426)

### Rationale for this change
Slight modifications to the `ToString` and `FromString` methods of decimal 256 to ensure proper accurate and proper conversion given precision and scale.

### Are these changes tested?
Yes

* Closes: apache#38395

Authored-by: Matt Topol <[email protected]>
Signed-off-by: Matt Topol <[email protected]>
dgreiss pushed a commit to dgreiss/arrow that referenced this pull request Feb 19, 2024
…ns (apache#38426)

### Rationale for this change
Slight modifications to the `ToString` and `FromString` methods of decimal 256 to ensure proper accurate and proper conversion given precision and scale.

### Are these changes tested?
Yes

* Closes: apache#38395

Authored-by: Matt Topol <[email protected]>
Signed-off-by: Matt Topol <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Go] rounding errors in decimal256 string functions
3 participants