golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
123.22k stars 17.57k forks source link

proposal: math: fixed decimal types #68272

Open sblackstone opened 3 months ago

sblackstone commented 3 months ago

Proposal Details

I propose reconsidering the inclusion of fixed decimal types as a standard language feature.

This has been extensively discussed in these two issues:

Motivation

Currently, fixed decimal types are supported by several third-party packages, including the popular shopspring/decimal. However, a recent PR suggests that the long-term maintenance of this library is uncertain. This situation forces the community to constantly adapt to the latest maintained fork, which is a long-term burden on the ecosystem.

Decimal libraries are also crucial in various core areas, such as database drivers, validation, and testing libraries. Standardization of a fixed decimal type would ensure that:

For these reasons I think it is a worth another look at one of the above proposals or a suitable alternative that supports standards and furthers interoperability.

gabyhelp commented 3 months ago

Related Issues

(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)

ianlancetaylor commented 3 months ago

We want to avoid the trap of continually revisiting old decisions. That can easily consume all of our time and make it difficult to move forward. Is there significant new information on this topic that would lead us to make a different decision this time?

sblackstone commented 3 months ago

The impetus for opening this was noticing that a package my employer depends on heavily at the moment (shopspring/decimal) seems to be semi-abandoned as it's now actively pointing at forks. The previous proposals rely on the existence of these tools in the community as part of the justification for exclusion from the language.

Primitive decimal math is something that deserves a higher level of support for interoperability, security and long term stability.

timothy-king commented 3 months ago

rsc's comment (from 2016) when #12127 was closed: https://github.com/golang/go/issues/12127#issuecomment-262075744 . FWIW https://pkg.go.dev/github.com/shopspring/decimal?tab=importedby lists 34,460 known importers.

bojanz commented 2 months ago

In my opinion, shopspring/decimal hasn't been the most optimal* decimal implementation in the ecosystem for many years now. Projects should be looking to migrate to https://github.com/cockroachdb/apd

* - in terms of performance and correctness

sblackstone commented 2 months ago

In my opinion, shopspring/decimal hasn't been the most optimal* decimal implementation in the ecosystem for many years now. Projects should be looking to migrate to https://github.com/cockroachdb/apd

    • in terms of performance and correctness

I think this highlights the problem of something so fundamental being relegated to library support - we are forever stuck with a fragmented ecosystem for solutions that traditionally rely on fixed decimal (e.g. finance).

jufemaiz commented 1 month ago

I'm here for this proposal. That Go doesn't have a native type for decimals is IMHO a deficiency of the language. Claiming that "yeah, but there's non-native packages" is an acceptance of a shortcoming of a type critical for any measurement or financial use case.

IMHO this https://github.com/golang/go/issues/19787 would be the optimal spot for the discussion (and has it earmarked for Go v2, whenever that may be delivered)

ianlancetaylor commented 1 month ago

19787 (which is no longer marked v2) is about decimal float types, this issue is about decimal fixed types.

jufemaiz commented 1 month ago

19787 (which is no longer marked v2) is about decimal float types, this issue is about decimal fixed types.

Good catch (v2)! And 🤦🏼 on my behalf.