decimal icon indicating copy to clipboard operation
decimal copied to clipboard

NewFromFloat loses precision on integers

Open FGasper opened this issue 2 months ago • 1 comments

A reopening/restatement of #410, now that I look yet again at the problem:

See this example:

package main

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

	"github.com/shopspring/decimal"
)

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

	t.Logf("Number: %f", f64)

	fromInt := decimal.NewFromInt(int64(f64))
	fromFloat := decimal.NewFromFloat(f64)

	if fromInt.Cmp(fromFloat) != 0 {
		t.Errorf("from-float (%v) != from-int (%v)", fromFloat, fromInt)
	}
}

The above outputs something like:

=== RUN   Test
    prog_test.go:14: Number: 5202671607238904832.000000
    prog_test.go:21: dec2 (5202671607238905000) != original (5202671607238904832)
--- FAIL: Test (0.00s)
FAIL

The original number is 5202671607238904832, but the NewFromFloat-created Decimal is 5202671607238905000.

FGasper avatar Oct 05 '25 00:10 FGasper

w(゚Д゚)w,This is a serious issue

CyJaySong avatar Dec 05 '25 03:12 CyJaySong