I'm using the golang's bigint as counters for a long run service where the statistics counters might overflow a regular uint64 over long run; but occasionally I need to calculate something like what's the average rate since program beginning? need a division like float(bigint) / time.Since(beginning).Seconds()
; the precision losing of conversion is acceptable in the rate calculation
https://golang.org/pkg/math/big/#Int.Uint64
but this kind of float64(bigint)
doesn't work actually, I see the library has 'bigint.Uint64()' conversion but it's undefined if overflowed a regular uint64; I wonder why the standard library doesn't provide a to .Float64()
method? and is there any workaround how to get a float
You can convert a big.Int
to a big.Float
using the Float.SetInt
method.
i := big.NewInt(12345)
f := new(big.Float).SetInt(i)
Then you can either convert f
as a float64 with the accepted accuracy, or use the big.Float
methods to calculate the output with greater accuracy.