Skip to content

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

@bcmills

Description

@bcmills

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.

Metadata

Metadata

Assignees

Labels

FixPendingIssues that have a fix which has not yet been reviewed or submitted.FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions