apd icon indicating copy to clipboard operation
apd copied to clipboard

Loss of precision when round-tripping with float64

Open FGasper opened this issue 5 months ago • 1 comments

Given a high enough integer value, apd seems to lose precision after some round-trips:

package main

import (
	"math/rand/v2"
	"testing"

	"github.com/cockroachdb/apd/v2"
)

func TestRoundTrip(t *testing.T) {
	// This number is exactly representable as a float64:
	f64 := float64(rand.Int64())

	dec := apd.New(0, 0).SetInt64(int64(f64))

	f64RT, err := dec.Float64()
	if err != nil {
		t.Fatalf("round-trip f64->dec->f64: %v", err)
	}

	if f64 != f64RT {
		t.Fatalf("mismatched: expect %f; got %f", f64, f64RT)
	}

	dec2, err := apd.New(0, 0).SetFloat64(f64)
	if err != nil {
		t.Fatalf("round-trip f64->dec->f64->dec: %v", err)
	}

	if dec.Cmp(dec2) != 0 {
		t.Fatalf("mismatched: expect %v; got %v", dec, dec2)
	}
}

FGasper avatar Oct 03 '25 15:10 FGasper

I believe this is because SetFloat64 misuses strconv.AppendFloat. It sets precision to -1, which doesn’t actually stringify the number’s exact value.

FGasper avatar Oct 06 '25 20:10 FGasper