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

out.Mul(out, big.NewFloat(math.Pow10(int(scale)))).SetPrec(precInBits)
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
Expand Down Expand Up @@ -506,7 +506,7 @@ func (n Num) FitsInPrecision(prec int32) bool {

func (n Num) ToString(scale int32) string {
f := (&big.Float{}).SetInt(n.BigInt())
f.Quo(f, (&big.Float{}).SetInt(scaleMultipliers[scale].BigInt()))
f.SetPrec(256).Quo(f, (&big.Float{}).SetInt(scaleMultipliers[scale].BigInt()))
zeroshade marked this conversation as resolved.
Show resolved Hide resolved
return f.Text('f', int(scale))
}

Expand Down
32 changes: 32 additions & 0 deletions go/arrow/decimal256/decimal256_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"math"
"math/big"
"strings"
"testing"

"github.com/apache/arrow/go/v14/arrow/decimal256"
Expand Down Expand Up @@ -575,3 +576,34 @@ func TestFromString(t *testing.T) {
})
}
}

bkietz marked this conversation as resolved.
Show resolved Hide resolved
// Test issues from GH-38395
func TestToString(t *testing.T) {
const decStr = "3379334159166193114608287418738414931564221155305735605033949613740461239999"

integer, _ := (&big.Int{}).SetString(decStr, 10)
dec := decimal256.FromBigInt(integer)

expected := "0." + decStr
actual := dec.ToString(76)
zeroshade marked this conversation as resolved.
Show resolved Hide resolved

if expected != actual {
t.Errorf("expected: %s, actual: %s\n", expected, actual)
}
}

// Test issues from GH-38395
func TestHexFromString(t *testing.T) {
const decStr = "11111111111111111111111111111111111111.00000000000000000000000000000000000000"

num, err := decimal256.FromString(decStr, 76, 38)
if err != nil {
t.Error(err)
} else if decStr != num.ToString(38) {
t.Errorf("expected: %s, actual: %s\n", decStr, num.ToString(38))

actualCoeff := num.BigInt()
expectedCoeff, _ := (&big.Int{}).SetString(strings.Replace(decStr, ".", "", -1), 10)
t.Errorf("expected(hex): %X, actual(hex): %X\n", expectedCoeff.Bytes(), actualCoeff.Bytes())
}
}
Loading