-
Notifications
You must be signed in to change notification settings - Fork 18.7k
Description
The Int type in the math/big package represents an arbitrary-precision integer. Today, it provides methods called Int64 and Uint64, which return the integer in the int64 and uint64 (machine) representations. However, it doesn't indicate whether the conversion is exact, so the caller must ascertain this, perhaps by a prior call to IsInt64 or IsUint64; otherwise, the result is undefined.
I propose to add three new methods to the package: Int.{ToInt64,ToUint64,Float64}. All three follow the same pattern of returning the closest representable value, and a big.Accuracy enum indicating whether the conversion was exact, a rounding up, or a rounding down. This matches the API of big.Float, which provides similar conversions to several types, including Int.
The To prefix is needed for ToInt64 and ToUint64 because the unprefixed names are taken.
Implementation sketch in https://go.dev/cl/453115. Tests to be added later, along with optimizations, such as a fast path of Float64 for 64-bit values.