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

fmt: %#v does not print “a Go-syntax representation” for NaN values #51486

Closed
bcmills opened this issue Mar 4, 2022 · 2 comments
Closed
Assignees
Labels
FixPending Issues that have a fix which has not yet been reviewed or submitted. NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@bcmills
Copy link
Contributor

bcmills commented Mar 4, 2022

What version of Go are you using (go version)?

devel go1.19-c9b60632eb Fri Mar 4 14:10:38 2022 +0000

Does this issue reproduce with the latest release?

Yes

What did you do?

Format a floating-point NaN value with %#v (https://go.dev/play/p/zlxs4NzQ0bl?v=gotip):

	x := float32(math.NaN())
	fmt.Printf("x := %T(%#v)", x, x)

What did you expect to see?

Per https://pkg.go.dev/fmt#hdr-Printing, “a Go-syntax representation of the value” — that is, valid Go syntax for the value.

Perhaps a line like:

	x := float32(math.NaN())

or

	x := float32(math.Float32frombits(0x7fc00000))

What did you see instead?

The string NaN:

x := float32(NaN)

which is not “a Go-syntax representation”. Feeding it back into the program gives (https://go.dev/play/p/908uCl2KbVN?v=gotip):

./prog.go:13:15: undefined: NaN

Go build failed.

This problem is especially acute if the NaN value is not the same bits as math.NaN() (https://go.dev/play/p/Xl13miTI7pe?v=gotip), since there is no indication that there is anything unusual about it.

@bcmills bcmills added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Mar 4, 2022
@bcmills bcmills added this to the Backlog milestone Mar 4, 2022
@robpike robpike self-assigned this Mar 4, 2022
@robpike
Copy link
Contributor

robpike commented May 10, 2022

What if the documentation just said that it printed "NaN" for NaNs? For the purpose of debugging, that seems a lot more pleasant.

@gopherbot
Copy link

Change https://go.dev/cl/557235 mentions this issue: fmt: document how %#g handles infinities and NaNs

@dmitshur dmitshur added NeedsFix The path to resolution is known, but the work has not been done. FixPending Issues that have a fix which has not yet been reviewed or submitted. and removed NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels Jan 29, 2024
@dmitshur dmitshur modified the milestones: Backlog, Go1.23 Jan 29, 2024
ezz-no pushed a commit to ezz-no/go-ezzno that referenced this issue Feb 18, 2024
The %#g format prints a "Go-syntax representation", but there is
no such thing for IEEE754 infinities and NaNs, so just document
what happens, which is that it prints +Inf, -Inf, or NaN.  We could
show something like math.Inf(1) and math.Nan(), but that doesn't
sit right, and anyway for NaNs you can't even recover the original
value. Simpler and more honest to give up.

Fixes golang#51486

Change-Id: I8d4e8186f5d7acc3e0e7b51d0b322142908ea0a2
Reviewed-on: https://go-review.googlesource.com/c/go/+/557235
Run-TryBot: Rob Pike <r@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
FixPending Issues that have a fix which has not yet been reviewed or submitted. NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

4 participants