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: maps with NaN keys are not printed correctly #14427

soniakeys opened this issue Feb 20, 2016 · 3 comments

fmt: maps with NaN keys are not printed correctly #14427

soniakeys opened this issue Feb 20, 2016 · 3 comments


Copy link

@soniakeys soniakeys commented Feb 20, 2016

go version go1.6 linux/amd64

m := map[float64]int{math.NaN(): 3, math.NaN(): 4, .1: 5}

prints map[0.1:5 NaN:<nil> NaN:<nil>].

I would have hoped for map[0.1:5 NaN:3 NaN:4] since for example iterating over the map with a for loop can retrieve the values 3 and 4. Perhaps acceptable would be map[0.1:5 NaN:0 NaN:0], consistent with v, ok := m[math.NaN()] returning 0, false. But printing when nil is not a valid value for the int type is unexpected.

Copy link

@martisch martisch commented Feb 20, 2016

This behaviour comes from the reflect package used in fmt to print the map.
For a NaN float as reflect value, as key to MapIndex the returned value is:
invalid reflect.Value

reflect MapIndex has the comment:
// It returns the zero Value if key is not found in the map or if v represents a nil map.
So its seems returning Value{} here would be correct for reflect and fmt needs to use a different way for iterating through the key , value pairs of the map (if those should reflect the stored values in the map) since the key math.Nan() will never compare equal to itself and therefore the value associated with that key will never be found in the map either by reflect or m[].

Btw. i dont think 0, false for m[math.NaN()] is consistent with map[0.1:5 NaN:0 NaN:0] as the earlier means the key was not found and therefore the 0 return value has no relation with the contents of the map.

Copy link

@randall77 randall77 commented Feb 20, 2016

Yes, this is an unfortunate wart that comes from the combination of NaN and the fact that reflect only provides MapKeys, not MapEntries. MapEntries wouldn't be hard to do, I'd be in favor of doing it. The question is whether it is worth the extra API just to accommodate NaN keys, something that is probably not advisable anyway.

Also see #11104

@ianlancetaylor ianlancetaylor changed the title fmt: Default formatting of map values for NaN key is <nil> even when nil is not valid for the type. fmt: maps with NaN keys are not printed correctly Feb 21, 2016
@ianlancetaylor ianlancetaylor added this to the Unplanned milestone Feb 21, 2016
Copy link

@gopherbot gopherbot commented Aug 19, 2018

Change mentions this issue: fmt: print values for map keys with non-reflexive equality

@gopherbot gopherbot closed this in 773e894 Aug 22, 2018
@golang golang locked and limited conversation to collaborators Aug 22, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
6 participants
You can’t perform that action at this time.