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
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
41 changes: 27 additions & 14 deletions go/arrow/decimal128/decimal128.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,23 +266,36 @@ func FromString(v string, prec, scale int32) (n Num, err error) {
return
}

// 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.
out.Mul(out, big.NewFloat(math.Pow10(int(scale)))).SetPrec(precInBits)
if out.Signbit() {
out.Sub(out, pt5)
if scale < 0 {
var tmp big.Int
val, _ := out.Int(&tmp)
if val.BitLen() > 127 {
return Num{}, errors.New("bitlen too large for decimal128")
}
n = FromBigInt(val)
n, _ = n.Div(scaleMultipliers[-scale])
} else {
out.Add(out, pt5)
}

var tmp big.Int
val, _ := out.Int(&tmp)
if val.BitLen() > 127 {
return Num{}, errors.New("bitlen too large for decimal128")
// 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])
zeroshade marked this conversation as resolved.
Show resolved Hide resolved
out.Mul(out, p).SetPrec(precInBits)
if out.Signbit() {
out.Sub(out, pt5)
} else {
out.Add(out, pt5)
}
Comment on lines +284 to +288
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!


var tmp big.Int
val, _ := out.Int(&tmp)
if val.BitLen() > 127 {
return Num{}, errors.New("bitlen too large for decimal128")
}
n = FromBigInt(val)
}
n = FromBigInt(val)

if !n.FitsInPrecision(prec) {
err = fmt.Errorf("val %v doesn't fit in precision %d", n, prec)
}
Expand Down
1 change: 1 addition & 0 deletions go/arrow/decimal128/decimal128_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,7 @@ func TestFromString(t *testing.T) {
{"1e-37", 1, 37},
{"2112.33", 211233, 2},
{"-2112.33", -211233, 2},
{"12E2", 12, -2},
}

for _, tt := range tests {
Expand Down
39 changes: 25 additions & 14 deletions go/arrow/decimal256/decimal256.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,23 +154,34 @@ func FromString(v string, prec, scale int32) (n Num, err error) {
return
}

out.Mul(out, (&big.Float{}).SetInt(scaleMultipliers[scale].BigInt())).SetPrec(precInBits)
// 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.
if out.Signbit() {
out.Sub(out, pt5)
if scale < 0 {
var tmp big.Int
val, _ := out.Int(&tmp)
if val.BitLen() > 255 {
return Num{}, errors.New("bitlen too large for decimal256")
}
n = FromBigInt(val)

n, _ = n.Div(scaleMultipliers[-scale])
} else {
out.Add(out, pt5)
}
out.Mul(out, (&big.Float{}).SetFloat64(float64PowersOfTen[scale+76])).SetPrec(precInBits)
// 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.
if out.Signbit() {
out.Sub(out, pt5)
} else {
out.Add(out, pt5)
}

var tmp big.Int
val, _ := out.Int(&tmp)
if val.BitLen() > 255 {
return Num{}, errors.New("bitlen too large for decimal256")
var tmp big.Int
val, _ := out.Int(&tmp)
if val.BitLen() > 255 {
return Num{}, errors.New("bitlen too large for decimal256")
}
n = FromBigInt(val)
}
n = FromBigInt(val)
if !n.FitsInPrecision(prec) {
err = fmt.Errorf("value %v doesn't fit in precision %d", n, prec)
}
Expand Down
1 change: 1 addition & 0 deletions go/arrow/decimal256/decimal256_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,7 @@ func TestFromString(t *testing.T) {
{"1e-37", 1, 37},
{"2112.33", 211233, 2},
{"-2112.33", -211233, 2},
{"12E2", 12, -2},
}

for _, tt := range tests {
Expand Down
Loading