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

math/big: alternative format for Go syntax representation %#v of Int and Float #67216

Open
larhun opened this issue May 6, 2024 · 4 comments
Assignees
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@larhun
Copy link

larhun commented May 6, 2024

Go version

go1.21.6 windows/amd64

Output of go env in your module/workspace:

insignificant

What did you do?

Run the snippet https://go.dev/play/p/bxXUjdjkcxQ.

What did you see happen?

Printing a big.Int and a big.Float value with the %v and %#v formats yields the same "default value".

Printing a big.Rat value %v and %#v formats yields a "default value" and a "Go-syntax representation of the value" as mandated by the fmt package.

What did you expect to see?

Printing a big.Int and a big.Float value with the %#v format should yield a "Go-syntax representation of the value" as mandated by the fmt package.

@mauri870 mauri870 added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label May 6, 2024
@mauri870
Copy link
Member

mauri870 commented May 6, 2024

I'm not sure if changing this is possible, it can break existing Go programs.

cc @griesemer

@larhun
Copy link
Author

larhun commented May 7, 2024

The %#v is a very simple and effective printing format to log the inner structure of a value at testing. This is not possible without a fix for the current implementation of the printing formats for big.Int and big.Float. If not fixed, the documentation should be updated to clarify the bug. I prefer a fix.

@griesemer griesemer self-assigned this May 7, 2024
@griesemer griesemer added this to the Backlog milestone May 7, 2024
@JonasUnderscore
Copy link

const x = 170141183460469231731687303715884105727
fmt.Printf("const x = %#v\n", new(big.Int).Not(new(big.Int).Lsh(big.NewInt(-1), x&127)))
// prints: const x = 170141183460469231731687303715884105727

I think this is a better go representation of an integer than arbitrary fields and values.
Rat doesn't have any good representations other than String() -> "num/denom", so it doesn't need a Format function that overrides the fmt struct formatting.

@griesemer
Copy link
Contributor

Arguably, for ints and floats, 42 and 42.0 are valid "Go-syntax representations of the values" and thus legitimate representations when using the %#v formats. We might even use the fractional representation, say 3/43 for rationals with the %#v format because such fractions are also "valid Go" (at least for small numerators/denominators).

@seankhliao seankhliao changed the title math/big: fix "%#v" format math/big: alternative format for Go syntax representation %#v of Int and Float Jun 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

4 participants