golang / go

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

proposal: math: fixed decimal types #68272

Open sblackstone opened 4 months ago

sblackstone commented 4 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 4 months ago

Related Issues

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

ianlancetaylor commented 4 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 4 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 4 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 4 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 3 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 2 months 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 2 months ago

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

jufemaiz commented 2 months 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.

Liebenfiels commented 10 hours ago

I read some articles, blogs and nothing has changed: • no float is good for any decimal analysis even if float32 is used • The Cobol had fixed point arithmetic’s at base 10- that works and good precision. • Most in finance use integers or still run on Cobol for above reason • Python import decimal works well as long as a = Decimal(‘0.1’) as string and it is good module with easy syntax, some book teach a=Decimal(0.1) but that will cause problems, library allows to type as no string. • GO does not have decimal module out of box, lots of proposals, there are third party libraries but some are not good. That does not help single standard as multiple choices exist and bring confusion.

Decimal module with easy syntax would help GO adoption- to migrate some finance program from Cobol, and for average users that are familiar with Python and understand floating point problems. I am learning GO so write that as novice observing space.