-
Notifications
You must be signed in to change notification settings - Fork 18k
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: fmt: append decimals to whole-number floats when using '%#v' #26363
Comments
Hey @martisch I'd love to have a go at fixing this myself... Would that be ok with you? |
Sure, happy if you send a fix with test case, if this is indeed agreed upon to need a fix (https://golang.org/doc/contribute.html). Looking at it again It is not 100% clear to me if we should fix this and documentation breaking backwards compatibility or remove the mention of v in the code that handles # for floats. Note that the behavior of printing 0 also seems to be in go1.4 and possibly earlier so not introduced by e97f407. #v is special and the documentation says "always print a decimal point for %e, %E, %f, %F, %g and %G;" so my commit message was inconsistent in that regard but from the code it should have applied to #v too. Go language spec says "A floating-point literal is a decimal representation of a floating-point constant. It has an integer part, a decimal point, a fractional part, and an exponent part." (where some parts can be omitted) and fmt documentation says "%#v a Go-syntax representation of the value". Interestingly it only says of the value and not a literal of the type, so it would seem to me 0 is ok according to the documentation and does not force a 0.0 or 0. literal. @robpike for further opinion if #v should always have a dot or leave current formatting to not break existing uses of #v (since at least go1.4). I lean to keeping output as is and fix the code to not handle the unreachable case of verb v in conjunction with f.sharp. |
Yep I agree it could break backwards compatibility. It would definitely break the work-around I made for the bug here. I'll leave you guys to make the call. Happy to fix if needed... |
I think it's a bug and should be fixed. |
Change https://golang.org/cl/123956 mentions this issue: |
So there's a couple of corner cases that I've made assumptions: Should Should Here's a PR: #26383 |
Hey is there anything I need to do to keep this from going stale? |
This fixes the unwanted behaviour where printing a zero float with the #v fmt verb outputs "0" - e.g. missing the trailing decimal. This means that the output would be interpreted as an int rather than a float when parsed as Go source. After this change the the output is "0.0". Fixes golang#26363
This fixes the unwanted behaviour where printing a zero float with the #v fmt verb outputs "0" - e.g. missing the trailing decimal. This means that the output would be interpreted as an int rather than a float when parsed as Go source. After this change the the output is "0.0". Fixes golang#26363
This fixes the unwanted behaviour where printing a zero float with the that the output would be interpreted as an int rather than a float when parsed as Go source. After this change the the output is "0.0". Fixes golang#26363
This fixes the unwanted behaviour where printing a zero float with the that the output would be interpreted as an int rather than a float when parsed as Go source. After this change the the output is "0.0". Fixes golang#26363
This fixes the unwanted behaviour where printing a zero float with the that the output would be interpreted as an int rather than a float when parsed as Go source. After this change the the output is "0.0". Fixes golang#26363
As noted in #27634, a breaking change to |
See also #27634 (comment) for an argument that the existing behavior was correct (but admittedly not the clearest possible choice). |
Change https://golang.org/cl/142597 mentions this issue: |
Numbers without decimals are valid Go representations of whole-number floats. That is, "var x float64 = 5" is valid Go. Avoid breakage in tests that expect a certain output from %#v by reverting to it. To guarantee the right type is generated by a print use %T(%#v) instead. Added a test to lock in this behavior. This reverts commit 7c7cecc. Fixes #27634 Updates #26363 Change-Id: I544c400a0903777dd216452a7e86dfe60b0b0283 Reviewed-on: https://go-review.googlesource.com/c/142597 Run-TryBot: Filippo Valsorda <filippo@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Rob Pike <r@golang.org> Reviewed-by: Bryan C. Mills <bcmills@google.com>
Because this broke things, we rolled it back. We're going to leave things as is. Declining the proposal. |
Please answer these questions before submitting your issue. Thanks!
What version of Go are you using (
go version
)?1.10.3
Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?darwin/amd64
What did you do?
fmt.Printf("%#v", float64(0.0))
Expected: 0.0
Actual: 0
https://play.golang.org/p/KkcUz1voJG4
As per the description in e97f407 I would expect to see the decimal point added for all floats. The
%#v
format string is intended to return valid Go syntax. In this case, the returned Go syntax represents an integer instead of a float.It looks like the commit linked above forgot that the
sharp
flag is replaced withsharpV
in the case of thev
verb here...There's more discussion of this here: dave/jennifer#39
Attn: @martisch
The text was updated successfully, but these errors were encountered: