Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal: math/big: Comparison to arbitrary number of decimal points #71288

Closed
chaslain opened this issue Jan 15, 2025 · 1 comment
Closed

proposal: math/big: Comparison to arbitrary number of decimal points #71288

chaslain opened this issue Jan 15, 2025 · 1 comment
Labels
LibraryProposal Issues describing a requested change to the Go standard library or x/ libraries, but not to a tool Proposal
Milestone

Comments

@chaslain
Copy link

Proposal Details

When representing arbitrary precision to compare two decimal money values, I have come to realize that sometimes the following code:

fmt.Println(float1.String() + " " + float2.String())
fmt.Println(float1.Cmp(float2))

Can have the output

804.26 804.26
-1

I would classify this as an unexpected result.
[For context, float1 is the sum of big floats parsed from strings, each string is formatted matching the regex "\d+.\d\d"]
At some point, during adding, the internal mantissas does not match, triggering comparison to view the values as not equal.

In this specific case, this code gets around the problem:

float1.SetString(float1.String())
float2.SetString(float2.String())
float1.Cmp(float2)

I propose an arbitrary precision comparison similar to the PHP arbitrary precision library, which allows comparison down to a specific number of digits.
I realize that base 10 are a human concept and not a machine one, and likely string manipulation will be required to compare the values.

Proposal:

// CmpArbitrary Compares to a specific digit in base 10. If places is negative, digits to the left of the decimal are truncated. If places is positive, all 
// digits to the right of the decimal except to that place are truncated.
// if places is 0, the floats are compared as integers.
func (x* big.Float) CmpArbitrary(y *big.Float, z *big.Float, places int) int {
// some implementation
}
@gopherbot gopherbot added this to the Proposal milestone Jan 15, 2025
@gabyhelp gabyhelp added the LibraryProposal Issues describing a requested change to the Go standard library or x/ libraries, but not to a tool label Jan 15, 2025
@seankhliao
Copy link
Member

I believe https://pkg.go.dev/math/big#Float.SetPrec is the appropriate thing to use here.
As you've noted, the usage of base 10 doesn't fit in with the rest of the package.

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Jan 15, 2025
@seankhliao seankhliao changed the title proposal: go/big: Comparison to arbitrary number of decimal points proposal: math/big: Comparison to arbitrary number of decimal points Jan 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
LibraryProposal Issues describing a requested change to the Go standard library or x/ libraries, but not to a tool Proposal
Projects
None yet
Development

No branches or pull requests

4 participants