mirror of
https://github.com/golang/go
synced 2024-11-25 04:27:56 -07:00
fmt.Scan: scan binary-exponent floating format, 2.4p-3
R=rsc, rog, r2 CC=golang-dev https://golang.org/cl/4128049
This commit is contained in:
parent
bc874ec0dd
commit
029bbe18e0
@ -7,6 +7,7 @@ package fmt
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"io"
|
"io"
|
||||||
|
"math"
|
||||||
"os"
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
@ -459,7 +460,7 @@ const (
|
|||||||
hexadecimalDigits = "0123456789aAbBcCdDeEfF"
|
hexadecimalDigits = "0123456789aAbBcCdDeEfF"
|
||||||
sign = "+-"
|
sign = "+-"
|
||||||
period = "."
|
period = "."
|
||||||
exponent = "eE"
|
exponent = "eEp"
|
||||||
)
|
)
|
||||||
|
|
||||||
// getBase returns the numeric base represented by the verb and its digit string.
|
// getBase returns the numeric base represented by the verb and its digit string.
|
||||||
@ -617,6 +618,27 @@ func (s *ss) complexTokens() (real, imag string) {
|
|||||||
|
|
||||||
// convertFloat converts the string to a float64value.
|
// convertFloat converts the string to a float64value.
|
||||||
func (s *ss) convertFloat(str string, n int) float64 {
|
func (s *ss) convertFloat(str string, n int) float64 {
|
||||||
|
if p := strings.Index(str, "p"); p >= 0 {
|
||||||
|
// Atof doesn't handle power-of-2 exponents,
|
||||||
|
// but they're easy to evaluate.
|
||||||
|
f, err := strconv.AtofN(str[:p], n)
|
||||||
|
if err != nil {
|
||||||
|
// Put full string into error.
|
||||||
|
if e, ok := err.(*strconv.NumError); ok {
|
||||||
|
e.Num = str
|
||||||
|
}
|
||||||
|
s.error(err)
|
||||||
|
}
|
||||||
|
n, err := strconv.Atoi(str[p+1:])
|
||||||
|
if err != nil {
|
||||||
|
// Put full string into error.
|
||||||
|
if e, ok := err.(*strconv.NumError); ok {
|
||||||
|
e.Num = str
|
||||||
|
}
|
||||||
|
s.error(err)
|
||||||
|
}
|
||||||
|
return math.Ldexp(f, n)
|
||||||
|
}
|
||||||
f, err := strconv.AtofN(str, n)
|
f, err := strconv.AtofN(str, n)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.error(err)
|
s.error(err)
|
||||||
@ -747,7 +769,7 @@ func (s *ss) hexString() string {
|
|||||||
return s.buf.String()
|
return s.buf.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
const floatVerbs = "eEfFgGv"
|
const floatVerbs = "beEfFgGv"
|
||||||
|
|
||||||
// scanOne scans a single value, deriving the scanner from the type of the argument.
|
// scanOne scans a single value, deriving the scanner from the type of the argument.
|
||||||
func (s *ss) scanOne(verb int, field interface{}) {
|
func (s *ss) scanOne(verb int, field interface{}) {
|
||||||
|
@ -160,6 +160,10 @@ var scanTests = []ScanTest{
|
|||||||
{"2.3\n", &float64Val, 2.3},
|
{"2.3\n", &float64Val, 2.3},
|
||||||
{"2.3e1\n", &float32Val, float32(2.3e1)},
|
{"2.3e1\n", &float32Val, float32(2.3e1)},
|
||||||
{"2.3e2\n", &float64Val, 2.3e2},
|
{"2.3e2\n", &float64Val, 2.3e2},
|
||||||
|
{"2.3p2\n", &float64Val, 2.3 * 4},
|
||||||
|
{"2.3p+2\n", &float64Val, 2.3 * 4},
|
||||||
|
{"2.3p+66\n", &float64Val, 2.3 * (1 << 32) * (1 << 32) * 4},
|
||||||
|
{"2.3p-66\n", &float64Val, 2.3 / ((1 << 32) * (1 << 32) * 4)},
|
||||||
{"2.35\n", &stringVal, "2.35"},
|
{"2.35\n", &stringVal, "2.35"},
|
||||||
{"2345678\n", &bytesVal, []byte("2345678")},
|
{"2345678\n", &bytesVal, []byte("2345678")},
|
||||||
{"(3.4e1-2i)\n", &complex128Val, 3.4e1 - 2i},
|
{"(3.4e1-2i)\n", &complex128Val, 3.4e1 - 2i},
|
||||||
|
Loading…
Reference in New Issue
Block a user