sstemmle commented on PR #38478:
URL: https://github.com/apache/arrow/pull/38478#issuecomment-1782505446

   Thanks for working on this @zeroshade. The current version is still failing 
for e.g. `9323406071781562130.6457232358109488923`. The following piece of code 
can be used to check a range of different input values.
   ```
   func TestFromStringDecimal128(t *testing.T) {
        rq := require.New(t)
   
        const maxprec = 38
   
        for {
                scale := int(rand.Int31()) % (maxprec + 1)
   
                s := ""
                if maxprec-scale > 0 {
                        for i := 0; i < maxprec-scale; i++ {
                                s += randDigit()
                        }
                } else {
                        s += "0"
                }
   
                if scale > 0 {
                        s += "."
   
                        for i := 0; i < scale; i++ {
                                s += randDigit()
                        }
                }
   
                num, err := decimal128.FromString(s, maxprec, int32(scale))
                rq.NoErrorf(err, "s=%s, scale=%d", s, scale)
   
                actualCoeff := num.BigInt()
   
                expectedCoeff, _ := (&big.Int{}).SetString(strings.Replace(s, 
".", "", -1), 10)
                rq.Equal(expectedCoeff.Bytes(), actualCoeff.Bytes(), s)
        }
   }
   
   func randDigit() string {
        return strconv.Itoa(int(rand.Int31() % 10))
   }
   ```
   
   I think one needs to adjust the precision provided to `big.ParseFloat()` to 
128. See 
https://stackoverflow.com/questions/30688422/is-the-most-significant-decimal-digits-precision-that-can-be-converted-to-binary
 .
   
   The significant (in the above sense) decimal digits giving a floating point 
precision are given by ⌊(N-1) log10(2)⌋.
   To get 38 digits, 127 is not enough, but 128 is needed. For 76 digits 255 is 
enough.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to