From: Ken Thompson Date: Sat, 6 Mar 2010 19:26:33 +0000 (-0800) Subject: PTAL X-Git-Tag: weekly.2010-03-15~45 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=788b41751e1098b52f43b74dc26954ad97437e8f;p=gostls13.git PTAL R=r CC=golang-dev https://golang.org/cl/254043 --- diff --git a/src/pkg/fmt/format.go b/src/pkg/fmt/format.go index 88ef3504e7..1567609cf4 100644 --- a/src/pkg/fmt/format.go +++ b/src/pkg/fmt/format.go @@ -420,6 +420,60 @@ func (f *fmt) fmt_G32(v float32) { f.plusSpace(strconv.Ftoa32(v, 'G', doPrec(f, // fmt_fb32 formats a float32 in the form -123p3 (exponent is power of 2). func (f *fmt) fmt_fb32(v float32) { f.padString(strconv.Ftoa32(v, 'b', 0)) } +// fmt_c64 formats a complex64 according to its fmt_x argument. +// TODO pass in a method rather than a byte when the compilers mature. +func (f *fmt) fmt_c64(v complex64, fmt_x byte) { + f.buf.WriteByte('(') + r := real(v) + for i := 0; ; i++ { + switch fmt_x { + case 'e': + f.fmt_e32(r) + case 'E': + f.fmt_E32(r) + case 'f': + f.fmt_f32(r) + case 'g': + f.fmt_g32(r) + case 'G': + f.fmt_G32(r) + } + if i != 0 { + break + } + f.plus = true + r = imag(v) + } + f.buf.Write(irparenBytes) +} + +// fmt_c128 formats a complex128 according to its fmt_x argument. +// TODO pass in a method rather than a byte when the compilers mature. +func (f *fmt) fmt_c128(v complex128, fmt_x byte) { + f.buf.WriteByte('(') + r := real(v) + for i := 0; ; i++ { + switch fmt_x { + case 'e': + f.fmt_e64(r) + case 'E': + f.fmt_E64(r) + case 'f': + f.fmt_f64(r) + case 'g': + f.fmt_g64(r) + case 'G': + f.fmt_G64(r) + } + if i != 0 { + break + } + f.plus = true + r = imag(v) + } + f.buf.Write(irparenBytes) +} + // float func (x *fmt) f(a float) { if strconv.FloatSize == 32 { diff --git a/src/pkg/fmt/print.go b/src/pkg/fmt/print.go index 6918f63cde..23f924b60f 100644 --- a/src/pkg/fmt/print.go +++ b/src/pkg/fmt/print.go @@ -24,7 +24,7 @@ %o base 8 %x base 16, with lower-case letters for a-f %X base 16, with upper-case letters for A-F - Floating-point: + Floating-point and complex constituents: %e scientific notation, e.g. -1234.456e+78 %E scientific notation, e.g. -1234.456E+78 %f decimal point but no exponent, e.g. 123.456 @@ -558,19 +558,19 @@ func (p *pp) printField(field interface{}, plus, sharp bool, depth int) (was_str p.fmt.fmt_g64(float64(f)) } return false - // case complex64: - // p.fmt.fmt_c64(f) - // return false - // case complex128: - // p.fmt.fmt_c128(f) - // return false - // case complex: - // if complexBits == 128 { - // p.fmt.fmt_c128(complex128(f)) - // } else { - // p.fmt.fmt_c64(complex64(f)) - // } - // return false + case complex64: + p.fmt.fmt_c64(f, 'g') + return false + case complex128: + p.fmt.fmt_c128(f, 'g') + return false + case complex: + if complexBits == 64 { + p.fmt.fmt_c64(complex64(f), 'g') + } else { + p.fmt.fmt_c128(complex128(f), 'g') + } + return false case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, uintptr: v, signed, ok := getInt(field) if !ok { @@ -917,24 +917,17 @@ func (p *pp) doprintf(format string, a []interface{}) { goto badtype } - // float + // float/complex case 'e': if v, ok := getFloat32(field); ok { p.fmt.fmt_e32(v) } else if v, ok := getFloat64(field); ok { p.fmt.fmt_e64(v) } else if v, ok := getComplex64(field); ok { - p.buf.WriteByte('(') - p.fmt.fmt_e32(real(v)) - p.fmt.plus = true - p.fmt.fmt_e32(imag(v)) - p.buf.Write(irparenBytes) + p.fmt.fmt_c64(v, 'e') } else if v, ok := getComplex128(field); ok { - p.buf.WriteByte('(') - p.fmt.fmt_e64(real(v)) - p.fmt.plus = true - p.fmt.fmt_e64(imag(v)) - p.buf.Write(irparenBytes) + p.fmt.fmt_c128(v, 'e') + } else { goto badtype } @@ -944,17 +937,9 @@ func (p *pp) doprintf(format string, a []interface{}) { } else if v, ok := getFloat64(field); ok { p.fmt.fmt_E64(v) } else if v, ok := getComplex64(field); ok { - p.buf.WriteByte('(') - p.fmt.fmt_E32(real(v)) - p.fmt.plus = true - p.fmt.fmt_E32(imag(v)) - p.buf.Write(irparenBytes) + p.fmt.fmt_c64(v, 'E') } else if v, ok := getComplex128(field); ok { - p.buf.WriteByte('(') - p.fmt.fmt_E64(real(v)) - p.fmt.plus = true - p.fmt.fmt_E64(imag(v)) - p.buf.Write(irparenBytes) + p.fmt.fmt_c128(v, 'E') } else { goto badtype } @@ -964,17 +949,9 @@ func (p *pp) doprintf(format string, a []interface{}) { } else if v, ok := getFloat64(field); ok { p.fmt.fmt_f64(v) } else if v, ok := getComplex64(field); ok { - p.buf.WriteByte('(') - p.fmt.fmt_f32(real(v)) - p.fmt.plus = true - p.fmt.fmt_f32(imag(v)) - p.buf.Write(irparenBytes) + p.fmt.fmt_c64(v, 'f') } else if v, ok := getComplex128(field); ok { - p.buf.WriteByte('(') - p.fmt.fmt_f64(real(v)) - p.fmt.plus = true - p.fmt.fmt_f64(imag(v)) - p.buf.Write(irparenBytes) + p.fmt.fmt_c128(v, 'f') } else { goto badtype } @@ -984,17 +961,9 @@ func (p *pp) doprintf(format string, a []interface{}) { } else if v, ok := getFloat64(field); ok { p.fmt.fmt_g64(v) } else if v, ok := getComplex64(field); ok { - p.buf.WriteByte('(') - p.fmt.fmt_g32(real(v)) - p.fmt.plus = true - p.fmt.fmt_g32(imag(v)) - p.buf.Write(irparenBytes) + p.fmt.fmt_c64(v, 'g') } else if v, ok := getComplex128(field); ok { - p.buf.WriteByte('(') - p.fmt.fmt_g64(real(v)) - p.fmt.plus = true - p.fmt.fmt_g64(imag(v)) - p.buf.Write(irparenBytes) + p.fmt.fmt_c128(v, 'g') } else { goto badtype } @@ -1004,17 +973,9 @@ func (p *pp) doprintf(format string, a []interface{}) { } else if v, ok := getFloat64(field); ok { p.fmt.fmt_G64(v) } else if v, ok := getComplex64(field); ok { - p.buf.WriteByte('(') - p.fmt.fmt_G32(real(v)) - p.fmt.plus = true - p.fmt.fmt_G32(imag(v)) - p.buf.Write(irparenBytes) + p.fmt.fmt_c64(v, 'G') } else if v, ok := getComplex128(field); ok { - p.buf.WriteByte('(') - p.fmt.fmt_G64(real(v)) - p.fmt.plus = true - p.fmt.fmt_G64(imag(v)) - p.buf.Write(irparenBytes) + p.fmt.fmt_c128(v, 'G') } else { goto badtype }