mirror of https://github.com/golang/go.git
Revert "fmt: fix incorrect format of whole-number floats when using %#v"
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 7c7cecc184.
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>
This commit is contained in:
parent
965fa3b191
commit
a52289ef2b
|
|
@ -686,11 +686,10 @@ var fmtTests = []struct {
|
||||||
{"%#v", 1.2345678, "1.2345678"},
|
{"%#v", 1.2345678, "1.2345678"},
|
||||||
{"%#v", float32(1.2345678), "1.2345678"},
|
{"%#v", float32(1.2345678), "1.2345678"},
|
||||||
|
|
||||||
// Whole number floats should have a single trailing zero added, but not
|
// Whole number floats are printed without decimals. See Issue 27634.
|
||||||
// for exponent notation.
|
{"%#v", 1.0, "1"},
|
||||||
{"%#v", 1.0, "1.0"},
|
|
||||||
{"%#v", 1000000.0, "1e+06"},
|
{"%#v", 1000000.0, "1e+06"},
|
||||||
{"%#v", float32(1.0), "1.0"},
|
{"%#v", float32(1.0), "1"},
|
||||||
{"%#v", float32(1000000.0), "1e+06"},
|
{"%#v", float32(1000000.0), "1e+06"},
|
||||||
|
|
||||||
// Only print []byte and []uint8 as type []byte if they appear at the top level.
|
// Only print []byte and []uint8 as type []byte if they appear at the top level.
|
||||||
|
|
|
||||||
|
|
@ -481,46 +481,36 @@ func (f *fmt) fmtFloat(v float64, size int, verb rune, prec int) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// The sharp flag forces printing a decimal point for non-binary formats
|
// The sharp flag forces printing a decimal point for non-binary formats
|
||||||
// and retains trailing zeros, which we may need to restore. For the sharpV
|
// and retains trailing zeros, which we may need to restore.
|
||||||
// flag, we ensure a single trailing zero is present if the output is not
|
if f.sharp && verb != 'b' {
|
||||||
// in exponent notation.
|
|
||||||
if f.sharpV || (f.sharp && verb != 'b') {
|
|
||||||
digits := 0
|
digits := 0
|
||||||
if !f.sharpV {
|
|
||||||
switch verb {
|
switch verb {
|
||||||
case 'g', 'G':
|
case 'v', 'g', 'G':
|
||||||
digits = prec
|
digits = prec
|
||||||
// If no precision is set explicitly use a precision of 6.
|
// If no precision is set explicitly use a precision of 6.
|
||||||
if digits == -1 {
|
if digits == -1 {
|
||||||
digits = 6
|
digits = 6
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Buffer pre-allocated with enough room for
|
// Buffer pre-allocated with enough room for
|
||||||
// exponent notations of the form "e+123".
|
// exponent notations of the form "e+123".
|
||||||
var tailBuf [5]byte
|
var tailBuf [5]byte
|
||||||
tail := tailBuf[:0]
|
tail := tailBuf[:0]
|
||||||
|
|
||||||
var hasDecimalPoint, hasExponent bool
|
hasDecimalPoint := false
|
||||||
// Starting from i = 1 to skip sign at num[0].
|
// Starting from i = 1 to skip sign at num[0].
|
||||||
for i := 1; i < len(num); i++ {
|
for i := 1; i < len(num); i++ {
|
||||||
switch num[i] {
|
switch num[i] {
|
||||||
case '.':
|
case '.':
|
||||||
hasDecimalPoint = true
|
hasDecimalPoint = true
|
||||||
case 'e', 'E':
|
case 'e', 'E':
|
||||||
hasExponent = true
|
|
||||||
tail = append(tail, num[i:]...)
|
tail = append(tail, num[i:]...)
|
||||||
num = num[:i]
|
num = num[:i]
|
||||||
default:
|
default:
|
||||||
digits--
|
digits--
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if f.sharpV {
|
|
||||||
if !hasDecimalPoint && !hasExponent {
|
|
||||||
num = append(num, '.', '0')
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if !hasDecimalPoint {
|
if !hasDecimalPoint {
|
||||||
num = append(num, '.')
|
num = append(num, '.')
|
||||||
}
|
}
|
||||||
|
|
@ -528,7 +518,6 @@ func (f *fmt) fmtFloat(v float64, size int, verb rune, prec int) {
|
||||||
num = append(num, '0')
|
num = append(num, '0')
|
||||||
digits--
|
digits--
|
||||||
}
|
}
|
||||||
}
|
|
||||||
num = append(num, tail...)
|
num = append(num, tail...)
|
||||||
}
|
}
|
||||||
// We want a sign if asked for and if the sign is not positive.
|
// We want a sign if asked for and if the sign is not positive.
|
||||||
|
|
|
||||||
|
|
@ -24,8 +24,8 @@ func f0(x int) {
|
||||||
func f1(x float32) {
|
func f1(x float32) {
|
||||||
switch x {
|
switch x {
|
||||||
case 5:
|
case 5:
|
||||||
case 5: // ERROR "duplicate case 5 .value 5\.0. in switch"
|
case 5: // ERROR "duplicate case 5 in switch"
|
||||||
case 5.0: // ERROR "duplicate case 5 .value 5\.0. in switch"
|
case 5.0: // ERROR "duplicate case 5 in switch"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -44,9 +44,9 @@ func f3(e interface{}) {
|
||||||
case 0: // ERROR "duplicate case 0 in switch"
|
case 0: // ERROR "duplicate case 0 in switch"
|
||||||
case int64(0):
|
case int64(0):
|
||||||
case float32(10):
|
case float32(10):
|
||||||
case float32(10): // ERROR "duplicate case float32\(10\) .value 10\.0. in switch"
|
case float32(10): // ERROR "duplicate case float32\(10\) .value 10. in switch"
|
||||||
case float64(10):
|
case float64(10):
|
||||||
case float64(10): // ERROR "duplicate case float64\(10\) .value 10\.0. in switch"
|
case float64(10): // ERROR "duplicate case float64\(10\) .value 10. in switch"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue