aboutsummaryrefslogtreecommitdiff
path: root/src/strconv
diff options
context:
space:
mode:
authorRuss Cox <rsc@golang.org>2019-01-29 16:25:54 -0500
committerRuss Cox <rsc@golang.org>2019-02-12 14:48:20 +0000
commit07717247d8decf5a5793f04c368eab3f43fad44f (patch)
treeb4403ad4877fef465674717caacfc45178d26b32 /src/strconv
parent1edd2a34c1bcf2133b659878e8b59e401eb8cc24 (diff)
downloadgo-07717247d8decf5a5793f04c368eab3f43fad44f.tar.gz
go-07717247d8decf5a5793f04c368eab3f43fad44f.zip
strconv: parse hex floats
This CL updates ParseFloat to recognize standard hexadecimal floating-point constants. See golang.org/design/19308-number-literals for background. For #29008. Change-Id: I45f3b0c36b5d92c0e8a4b35c05443a83d7a6d4b3 Reviewed-on: https://go-review.googlesource.com/c/160241 Run-TryBot: Russ Cox <rsc@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
Diffstat (limited to 'src/strconv')
-rw-r--r--src/strconv/atof.go205
-rw-r--r--src/strconv/atof_test.go176
-rw-r--r--src/strconv/atoi.go8
3 files changed, 341 insertions, 48 deletions
diff --git a/src/strconv/atof.go b/src/strconv/atof.go
index ada85e9fed..3ced3c7167 100644
--- a/src/strconv/atof.go
+++ b/src/strconv/atof.go
@@ -12,7 +12,7 @@ package strconv
import "math"
-var optimize = true // can change for testing
+var optimize = true // set to false to force slow-path conversions for testing
func equalIgnoreCase(s1, s2 string) bool {
if len(s1) != len(s2) {
@@ -119,7 +119,7 @@ func (b *decimal) set(s string) (ok bool) {
// just be sure to move the decimal point by
// a lot (say, 100000). it doesn't matter if it's
// not the exact number.
- if i < len(s) && (s[i] == 'e' || s[i] == 'E') {
+ if i < len(s) && lower(s[i]) == 'e' {
i++
if i >= len(s) {
return
@@ -152,10 +152,9 @@ func (b *decimal) set(s string) (ok bool) {
}
// readFloat reads a decimal mantissa and exponent from a float
-// string representation. It sets ok to false if the number could
+// string representation. It returns ok==false if the number could
// not fit return types or is invalid.
-func readFloat(s string) (mantissa uint64, exp int, neg, trunc, ok bool) {
- const uint64digits = 19
+func readFloat(s string) (mantissa uint64, exp int, neg, trunc, hex, ok bool) {
i := 0
// optional sign
@@ -171,6 +170,16 @@ func readFloat(s string) (mantissa uint64, exp int, neg, trunc, ok bool) {
}
// digits
+ base := uint64(10)
+ maxMantDigits := 19 // 10^19 fits in uint64
+ expChar := byte('e')
+ if i+2 < len(s) && s[i] == '0' && lower(s[i+1]) == 'x' {
+ base = 16
+ maxMantDigits = 16 // 16^16 fits in uint64
+ i += 2
+ expChar = 'p'
+ hex = true
+ }
sawdot := false
sawdigits := false
nd := 0
@@ -193,11 +202,23 @@ func readFloat(s string) (mantissa uint64, exp int, neg, trunc, ok bool) {
continue
}
nd++
- if ndMant < uint64digits {
- mantissa *= 10
+ if ndMant < maxMantDigits {
+ mantissa *= base
mantissa += uint64(c - '0')
ndMant++
- } else if s[i] != '0' {
+ } else if c != '0' {
+ trunc = true
+ }
+ continue
+
+ case base == 16 && 'a' <= lower(c) && lower(c) <= 'f':
+ sawdigits = true
+ nd++
+ if ndMant < maxMantDigits {
+ mantissa *= 16
+ mantissa += uint64(lower(c) - 'a' + 10)
+ ndMant++
+ } else {
trunc = true
}
continue
@@ -211,12 +232,17 @@ func readFloat(s string) (mantissa uint64, exp int, neg, trunc, ok bool) {
dp = nd
}
+ if base == 16 {
+ dp *= 4
+ ndMant *= 4
+ }
+
// optional exponent moves decimal point.
// if we read a very large, very long number,
// just be sure to move the decimal point by
// a lot (say, 100000). it doesn't matter if it's
// not the exact number.
- if i < len(s) && (s[i] == 'e' || s[i] == 'E') {
+ if i < len(s) && lower(s[i]) == expChar {
i++
if i >= len(s) {
return
@@ -238,6 +264,9 @@ func readFloat(s string) (mantissa uint64, exp int, neg, trunc, ok bool) {
}
}
dp += e * esign
+ } else if base == 16 {
+ // Must have exponent.
+ return
}
if i != len(s) {
@@ -249,7 +278,6 @@ func readFloat(s string) (mantissa uint64, exp int, neg, trunc, ok bool) {
}
ok = true
return
-
}
// decimal power of ten to binary power of two.
@@ -433,6 +461,76 @@ func atof32exact(mantissa uint64, exp int, neg bool) (f float32, ok bool) {
return
}
+// atofHex converts the hex floating-point string s
+// to a rounded float32 or float64 value (depending on flt==&float32info or flt==&float64info)
+// and returns it as a float64.
+// The string s has already been parsed into a mantissa, exponent, and sign (neg==true for negative).
+// If trunc is true, trailing non-zero bits have been omitted from the mantissa.
+func atofHex(s string, flt *floatInfo, mantissa uint64, exp int, neg, trunc bool) (float64, error) {
+ maxExp := 1<<flt.expbits + flt.bias - 2
+ minExp := flt.bias + 1
+ exp += int(flt.mantbits) // mantissa now implicitly divided by 2^mantbits.
+
+ // Shift mantissa and exponent to bring representation into float range.
+ // Eventually we want a mantissa with a leading 1-bit followed by mantbits other bits.
+ // For rounding, we need two more, where the bottom bit represents
+ // whether that bit or any later bit was non-zero.
+ // (If the mantissa has already lost non-zero bits, trunc is true,
+ // and we OR in a 1 below after shifting left appropriately.)
+ for mantissa != 0 && mantissa>>(flt.mantbits+2) == 0 {
+ mantissa <<= 1
+ exp--
+ }
+ if trunc {
+ mantissa |= 1
+ }
+ for mantissa>>(1+flt.mantbits+2) != 0 {
+ mantissa = mantissa>>1 | mantissa&1
+ exp++
+ }
+
+ // If exponent is too negative,
+ // denormalize in hopes of making it representable.
+ // (The -2 is for the rounding bits.)
+ for mantissa > 1 && exp < minExp-2 {
+ mantissa = mantissa>>1 | mantissa&1
+ exp++
+ }
+
+ // Round using two bottom bits.
+ round := mantissa & 3
+ mantissa >>= 2
+ round |= mantissa & 1 // round to even (round up if mantissa is odd)
+ exp += 2
+ if round == 3 {
+ mantissa++
+ if mantissa == 1<<(1+flt.mantbits) {
+ mantissa >>= 1
+ exp++
+ }
+ }
+
+ if mantissa>>flt.mantbits == 0 { // Denormal or zero.
+ exp = flt.bias
+ }
+ var err error
+ if exp > maxExp { // infinity and range error
+ mantissa = 1 << flt.mantbits
+ exp = maxExp + 1
+ err = rangeError(fnParseFloat, s)
+ }
+
+ bits := mantissa & (1<<flt.mantbits - 1)
+ bits |= uint64((exp-flt.bias)&(1<<flt.expbits-1)) << flt.mantbits
+ if neg {
+ bits |= 1 << flt.mantbits << flt.expbits
+ }
+ if flt == &float32info {
+ return float64(math.Float32frombits(uint32(bits))), err
+ }
+ return math.Float64frombits(bits), err
+}
+
const fnParseFloat = "ParseFloat"
func atof32(s string) (f float32, err error) {
@@ -440,28 +538,32 @@ func atof32(s string) (f float32, err error) {
return float32(val), nil
}
- if optimize {
- // Parse mantissa and exponent.
- mantissa, exp, neg, trunc, ok := readFloat(s)
- if ok {
- // Try pure floating-point arithmetic conversion.
- if !trunc {
- if f, ok := atof32exact(mantissa, exp, neg); ok {
- return f, nil
- }
+ mantissa, exp, neg, trunc, hex, ok := readFloat(s)
+ if hex && ok {
+ f, err := atofHex(s, &float32info, mantissa, exp, neg, trunc)
+ return float32(f), err
+ }
+
+ if optimize && ok {
+ // Try pure floating-point arithmetic conversion.
+ if !trunc {
+ if f, ok := atof32exact(mantissa, exp, neg); ok {
+ return f, nil
}
- // Try another fast path.
- ext := new(extFloat)
- if ok := ext.AssignDecimal(mantissa, exp, neg, trunc, &float32info); ok {
- b, ovf := ext.floatBits(&float32info)
- f = math.Float32frombits(uint32(b))
- if ovf {
- err = rangeError(fnParseFloat, s)
- }
- return f, err
+ }
+ // Try another fast path.
+ ext := new(extFloat)
+ if ok := ext.AssignDecimal(mantissa, exp, neg, trunc, &float32info); ok {
+ b, ovf := ext.floatBits(&float32info)
+ f = math.Float32frombits(uint32(b))
+ if ovf {
+ err = rangeError(fnParseFloat, s)
}
+ return f, err
}
}
+
+ // Slow fallback.
var d decimal
if !d.set(s) {
return 0, syntaxError(fnParseFloat, s)
@@ -479,28 +581,31 @@ func atof64(s string) (f float64, err error) {
return val, nil
}
- if optimize {
- // Parse mantissa and exponent.
- mantissa, exp, neg, trunc, ok := readFloat(s)
- if ok {
- // Try pure floating-point arithmetic conversion.
- if !trunc {
- if f, ok := atof64exact(mantissa, exp, neg); ok {
- return f, nil
- }
+ mantissa, exp, neg, trunc, hex, ok := readFloat(s)
+ if hex && ok {
+ return atofHex(s, &float64info, mantissa, exp, neg, trunc)
+ }
+
+ if optimize && ok {
+ // Try pure floating-point arithmetic conversion.
+ if !trunc {
+ if f, ok := atof64exact(mantissa, exp, neg); ok {
+ return f, nil
}
- // Try another fast path.
- ext := new(extFloat)
- if ok := ext.AssignDecimal(mantissa, exp, neg, trunc, &float64info); ok {
- b, ovf := ext.floatBits(&float64info)
- f = math.Float64frombits(b)
- if ovf {
- err = rangeError(fnParseFloat, s)
- }
- return f, err
+ }
+ // Try another fast path.
+ ext := new(extFloat)
+ if ok := ext.AssignDecimal(mantissa, exp, neg, trunc, &float64info); ok {
+ b, ovf := ext.floatBits(&float64info)
+ f = math.Float64frombits(b)
+ if ovf {
+ err = rangeError(fnParseFloat, s)
}
+ return f, err
}
}
+
+ // Slow fallback.
var d decimal
if !d.set(s) {
return 0, syntaxError(fnParseFloat, s)
@@ -518,9 +623,13 @@ func atof64(s string) (f float64, err error) {
// When bitSize=32, the result still has type float64, but it will be
// convertible to float32 without changing its value.
//
-// If s is well-formed and near a valid floating point number,
-// ParseFloat returns the nearest floating point number rounded
+// ParseFloat accepts decimal and hexadecimal floating-point number syntax.
+// If s is well-formed and near a valid floating-point number,
+// ParseFloat returns the nearest floating-point number rounded
// using IEEE754 unbiased rounding.
+// (Parsing a hexadecimal floating-point value only rounds when
+// there are more bits in the hexadecimal representatiton than
+// will fit in the mantissa.)
//
// The errors that ParseFloat returns have concrete type *NumError
// and include err.Num = s.
diff --git a/src/strconv/atof_test.go b/src/strconv/atof_test.go
index cf4d47c8b9..f67386a000 100644
--- a/src/strconv/atof_test.go
+++ b/src/strconv/atof_test.go
@@ -43,6 +43,20 @@ var atoftests = []atofTest{
{"1e-20", "1e-20", nil},
{"625e-3", "0.625", nil},
+ // Hexadecimal floating-point.
+ {"0x1p0", "1", nil},
+ {"0x1p1", "2", nil},
+ {"0x1p-1", "0.5", nil},
+ {"0x1p-200", "6.223015277861142e-61", nil},
+ {"0x1p200", "1.6069380442589903e+60", nil},
+ {"0x1fFe2.p0", "131042", nil},
+ {"0x1fFe2.P0", "131042", nil},
+ {"-0x2p3", "-16", nil},
+ {"0x0.fp4", "15", nil},
+ {"0x0.fp0", "0.9375", nil},
+ {"0x1e2", "0", ErrSyntax},
+ {"1p2", "0", ErrSyntax},
+
// zeros
{"0", "0", nil},
{"0e0", "0", nil},
@@ -58,6 +72,11 @@ var atoftests = []atofTest{
{"0.00e-01234567890123456789", "0", nil},
{"-0e+01234567890123456789", "-0", nil},
{"-0.00e-01234567890123456789", "-0", nil},
+ {"0x0p+01234567890123456789", "0", nil},
+ {"0x0.00p-01234567890123456789", "0", nil},
+ {"-0x0p+01234567890123456789", "-0", nil},
+ {"-0x0.00p-01234567890123456789", "-0", nil},
+
{"0e291", "0", nil}, // issue 15364
{"0e292", "0", nil}, // issue 15364
{"0e347", "0", nil}, // issue 15364
@@ -66,6 +85,26 @@ var atoftests = []atofTest{
{"-0e292", "-0", nil},
{"-0e347", "-0", nil},
{"-0e348", "-0", nil},
+ {"0x0p126", "0", nil},
+ {"0x0p127", "0", nil},
+ {"0x0p128", "0", nil},
+ {"0x0p129", "0", nil},
+ {"0x0p130", "0", nil},
+ {"0x0p1022", "0", nil},
+ {"0x0p1023", "0", nil},
+ {"0x0p1024", "0", nil},
+ {"0x0p1025", "0", nil},
+ {"0x0p1026", "0", nil},
+ {"-0x0p126", "-0", nil},
+ {"-0x0p127", "-0", nil},
+ {"-0x0p128", "-0", nil},
+ {"-0x0p129", "-0", nil},
+ {"-0x0p130", "-0", nil},
+ {"-0x0p1022", "-0", nil},
+ {"-0x0p1023", "-0", nil},
+ {"-0x0p1024", "-0", nil},
+ {"-0x0p1025", "-0", nil},
+ {"-0x0p1026", "-0", nil},
// NaNs
{"nan", "NaN", nil},
@@ -83,21 +122,46 @@ var atoftests = []atofTest{
// largest float64
{"1.7976931348623157e308", "1.7976931348623157e+308", nil},
{"-1.7976931348623157e308", "-1.7976931348623157e+308", nil},
+ {"0x1.fffffffffffffp1023", "1.7976931348623157e+308", nil},
+ {"-0x1.fffffffffffffp1023", "-1.7976931348623157e+308", nil},
+ {"0x1fffffffffffffp+971", "1.7976931348623157e+308", nil},
+ {"-0x1fffffffffffffp+971", "-1.7976931348623157e+308", nil},
+ {"0x.1fffffffffffffp1027", "1.7976931348623157e+308", nil},
+ {"-0x.1fffffffffffffp1027", "-1.7976931348623157e+308", nil},
+
// next float64 - too large
{"1.7976931348623159e308", "+Inf", ErrRange},
{"-1.7976931348623159e308", "-Inf", ErrRange},
+ {"0x1p1024", "+Inf", ErrRange},
+ {"-0x1p1024", "-Inf", ErrRange},
+ {"0x2p1023", "+Inf", ErrRange},
+ {"-0x2p1023", "-Inf", ErrRange},
+ {"0x.1p1028", "+Inf", ErrRange},
+ {"-0x.1p1028", "-Inf", ErrRange},
+ {"0x.2p1027", "+Inf", ErrRange},
+ {"-0x.2p1027", "-Inf", ErrRange},
+
// the border is ...158079
// borderline - okay
{"1.7976931348623158e308", "1.7976931348623157e+308", nil},
{"-1.7976931348623158e308", "-1.7976931348623157e+308", nil},
+ {"0x1.fffffffffffff7fffp1023", "1.7976931348623157e+308", nil},
+ {"-0x1.fffffffffffff7fffp1023", "-1.7976931348623157e+308", nil},
// borderline - too large
{"1.797693134862315808e308", "+Inf", ErrRange},
{"-1.797693134862315808e308", "-Inf", ErrRange},
+ {"0x1.fffffffffffff8p1023", "+Inf", ErrRange},
+ {"-0x1.fffffffffffff8p1023", "-Inf", ErrRange},
+ {"0x1fffffffffffff.8p+971", "+Inf", ErrRange},
+ {"-0x1fffffffffffff8p+967", "-Inf", ErrRange},
+ {"0x.1fffffffffffff8p1027", "+Inf", ErrRange},
+ {"-0x.1fffffffffffff9p1027", "-Inf", ErrRange},
// a little too large
{"1e308", "1e+308", nil},
{"2e308", "+Inf", ErrRange},
{"1e309", "+Inf", ErrRange},
+ {"0x1p1025", "+Inf", ErrRange},
// way too large
{"1e310", "+Inf", ErrRange},
@@ -106,6 +170,12 @@ var atoftests = []atofTest{
{"-1e400", "-Inf", ErrRange},
{"1e400000", "+Inf", ErrRange},
{"-1e400000", "-Inf", ErrRange},
+ {"0x1p1030", "+Inf", ErrRange},
+ {"0x1p2000", "+Inf", ErrRange},
+ {"0x1p2000000000", "+Inf", ErrRange},
+ {"-0x1p1030", "-Inf", ErrRange},
+ {"-0x1p2000", "-Inf", ErrRange},
+ {"-0x1p2000000000", "-Inf", ErrRange},
// denormalized
{"1e-305", "1e-305", nil},
@@ -125,17 +195,75 @@ var atoftests = []atofTest{
{"1e-350", "0", nil},
{"1e-400000", "0", nil},
+ // Near denormals and denormals.
+ {"0x2.00000000000000p-1010", "1.8227805048890994e-304", nil}, // 0x00e0000000000000
+ {"0x1.fffffffffffff0p-1010", "1.8227805048890992e-304", nil}, // 0x00dfffffffffffff
+ {"0x1.fffffffffffff7p-1010", "1.8227805048890992e-304", nil}, // rounded down
+ {"0x1.fffffffffffff8p-1010", "1.8227805048890994e-304", nil}, // rounded up
+ {"0x1.fffffffffffff9p-1010", "1.8227805048890994e-304", nil}, // rounded up
+
+ {"0x2.00000000000000p-1022", "4.450147717014403e-308", nil}, // 0x0020000000000000
+ {"0x1.fffffffffffff0p-1022", "4.4501477170144023e-308", nil}, // 0x001fffffffffffff
+ {"0x1.fffffffffffff7p-1022", "4.4501477170144023e-308", nil}, // rounded down
+ {"0x1.fffffffffffff8p-1022", "4.450147717014403e-308", nil}, // rounded up
+ {"0x1.fffffffffffff9p-1022", "4.450147717014403e-308", nil}, // rounded up
+
+ {"0x1.00000000000000p-1022", "2.2250738585072014e-308", nil}, // 0x0010000000000000
+ {"0x0.fffffffffffff0p-1022", "2.225073858507201e-308", nil}, // 0x000fffffffffffff
+ {"0x0.ffffffffffffe0p-1022", "2.2250738585072004e-308", nil}, // 0x000ffffffffffffe
+ {"0x0.ffffffffffffe7p-1022", "2.2250738585072004e-308", nil}, // rounded down
+ {"0x1.ffffffffffffe8p-1023", "2.225073858507201e-308", nil}, // rounded up
+ {"0x1.ffffffffffffe9p-1023", "2.225073858507201e-308", nil}, // rounded up
+
+ {"0x0.00000003fffff0p-1022", "2.072261e-317", nil}, // 0x00000000003fffff
+ {"0x0.00000003456780p-1022", "1.694649e-317", nil}, // 0x0000000000345678
+ {"0x0.00000003456787p-1022", "1.694649e-317", nil}, // rounded down
+ {"0x0.00000003456788p-1022", "1.694649e-317", nil}, // rounded down (half to even)
+ {"0x0.00000003456790p-1022", "1.6946496e-317", nil}, // 0x0000000000345679
+ {"0x0.00000003456789p-1022", "1.6946496e-317", nil}, // rounded up
+
+ {"0x0.0000000345678800000000000000000000000001p-1022", "1.6946496e-317", nil}, // rounded up
+
+ {"0x0.000000000000f0p-1022", "7.4e-323", nil}, // 0x000000000000000f
+ {"0x0.00000000000060p-1022", "3e-323", nil}, // 0x0000000000000006
+ {"0x0.00000000000058p-1022", "3e-323", nil}, // rounded up
+ {"0x0.00000000000057p-1022", "2.5e-323", nil}, // rounded down
+ {"0x0.00000000000050p-1022", "2.5e-323", nil}, // 0x0000000000000005
+
+ {"0x0.00000000000010p-1022", "5e-324", nil}, // 0x0000000000000001
+ {"0x0.000000000000081p-1022", "5e-324", nil}, // rounded up
+ {"0x0.00000000000008p-1022", "0", nil}, // rounded down
+ {"0x0.00000000000007fp-1022", "0", nil}, // rounded down
+
// try to overflow exponent
{"1e-4294967296", "0", nil},
{"1e+4294967296", "+Inf", ErrRange},
{"1e-18446744073709551616", "0", nil},
{"1e+18446744073709551616", "+Inf", ErrRange},
+ {"0x1p-4294967296", "0", nil},
+ {"0x1p+4294967296", "+Inf", ErrRange},
+ {"0x1p-18446744073709551616", "0", nil},
+ {"0x1p+18446744073709551616", "+Inf", ErrRange},
// Parse errors
{"1e", "0", ErrSyntax},
{"1e-", "0", ErrSyntax},
{".e-1", "0", ErrSyntax},
{"1\x00.2", "0", ErrSyntax},
+ {"0x", "0", ErrSyntax},
+ {"0x.", "0", ErrSyntax},
+ {"0x1", "0", ErrSyntax},
+ {"0x.1", "0", ErrSyntax},
+ {"0x1p", "0", ErrSyntax},
+ {"0x.1p", "0", ErrSyntax},
+ {"0x1p+", "0", ErrSyntax},
+ {"0x.1p+", "0", ErrSyntax},
+ {"0x1p-", "0", ErrSyntax},
+ {"0x.1p-", "0", ErrSyntax},
+ {"0x1p+2", "4", nil},
+ {"0x.1p+2", "0.25", nil},
+ {"0x1p-2", "0.25", nil},
+ {"0x.1p-2", "0.015625", nil},
// https://www.exploringbinary.com/java-hangs-when-converting-2-2250738585072012e-308/
{"2.2250738585072012e-308", "2.2250738585072014e-308", nil},
@@ -148,42 +276,75 @@ var atoftests = []atofTest{
// A different kind of very large number.
{"22.222222222222222", "22.22222222222222", nil},
{"2." + strings.Repeat("2", 4000) + "e+1", "22.22222222222222", nil},
+ {"0x1.1111111111111p222", "7.18931911124017e+66", nil},
+ {"0x2.2222222222222p221", "7.18931911124017e+66", nil},
+ {"0x2." + strings.Repeat("2", 4000) + "p221", "7.18931911124017e+66", nil},
// Exactly halfway between 1 and math.Nextafter(1, 2).
// Round to even (down).
{"1.00000000000000011102230246251565404236316680908203125", "1", nil},
+ {"0x1.00000000000008p0", "1", nil},
// Slightly lower; still round down.
{"1.00000000000000011102230246251565404236316680908203124", "1", nil},
+ {"0x1.00000000000007Fp0", "1", nil},
// Slightly higher; round up.
{"1.00000000000000011102230246251565404236316680908203126", "1.0000000000000002", nil},
+ {"0x1.000000000000081p0", "1.0000000000000002", nil},
+ {"0x1.00000000000009p0", "1.0000000000000002", nil},
// Slightly higher, but you have to read all the way to the end.
{"1.00000000000000011102230246251565404236316680908203125" + strings.Repeat("0", 10000) + "1", "1.0000000000000002", nil},
+ {"0x1.00000000000008" + strings.Repeat("0", 10000) + "1p0", "1.0000000000000002", nil},
+
+ // Halfway between x := math.Nextafter(1, 2) and math.Nextafter(x, 2)
+ // Round to even (up).
+ {"1.00000000000000033306690738754696212708950042724609375", "1.0000000000000004", nil},
+ {"0x1.00000000000018p0", "1.0000000000000004", nil},
}
var atof32tests = []atofTest{
+ // Hex
+ {"0x1p-100", "7.888609e-31", nil},
+ {"0x1p100", "1.2676506e+30", nil},
+
// Exactly halfway between 1 and the next float32.
// Round to even (down).
{"1.000000059604644775390625", "1", nil},
+ {"0x1.000001p0", "1", nil},
// Slightly lower.
{"1.000000059604644775390624", "1", nil},
+ {"0x1.0000008p0", "1", nil},
+ {"0x1.000000fp0", "1", nil},
// Slightly higher.
{"1.000000059604644775390626", "1.0000001", nil},
+ {"0x1.000002p0", "1.0000001", nil},
+ {"0x1.0000018p0", "1.0000001", nil},
+ {"0x1.0000011p0", "1.0000001", nil},
// Slightly higher, but you have to read all the way to the end.
{"1.000000059604644775390625" + strings.Repeat("0", 10000) + "1", "1.0000001", nil},
+ {"0x1.000001" + strings.Repeat("0", 10000) + "1p0", "1.0000001", nil},
// largest float32: (1<<128) * (1 - 2^-24)
{"340282346638528859811704183484516925440", "3.4028235e+38", nil},
{"-340282346638528859811704183484516925440", "-3.4028235e+38", nil},
+ {"0x.ffffffp128", "3.4028235e+38", nil},
+ {"-340282346638528859811704183484516925440", "-3.4028235e+38", nil},
+ {"-0x.ffffffp128", "-3.4028235e+38", nil},
// next float32 - too large
{"3.4028236e38", "+Inf", ErrRange},
{"-3.4028236e38", "-Inf", ErrRange},
+ {"0x1.0p128", "+Inf", ErrRange},
+ {"-0x1.0p128", "-Inf", ErrRange},
// the border is 3.40282356779...e+38
// borderline - okay
{"3.402823567e38", "3.4028235e+38", nil},
{"-3.402823567e38", "-3.4028235e+38", nil},
+ {"0x.ffffff7fp128", "3.4028235e+38", nil},
+ {"-0x.ffffff7fp128", "-3.4028235e+38", nil},
// borderline - too large
{"3.4028235678e38", "+Inf", ErrRange},
{"-3.4028235678e38", "-Inf", ErrRange},
+ {"0x.ffffff8p128", "+Inf", ErrRange},
+ {"-0x.ffffff8p128", "-Inf", ErrRange},
// Denormals: less than 2^-126
{"1e-38", "1e-38", nil},
@@ -195,9 +356,24 @@ var atof32tests = []atofTest{
{"1e-44", "1e-44", nil},
{"6e-45", "6e-45", nil}, // 4p-149 = 5.6e-45
{"5e-45", "6e-45", nil},
+
// Smallest denormal
{"1e-45", "1e-45", nil}, // 1p-149 = 1.4e-45
{"2e-45", "1e-45", nil},
+ {"3e-45", "3e-45", nil},
+
+ // Near denormals and denormals.
+ {"0x0.89aBcDp-125", "1.2643093e-38", nil}, // 0x0089abcd
+ {"0x0.8000000p-125", "1.1754944e-38", nil}, // 0x00800000
+ {"0x0.1234560p-125", "1.671814e-39", nil}, // 0x00123456
+ {"0x0.1234567p-125", "1.671814e-39", nil}, // rounded down
+ {"0x0.1234568p-125", "1.671814e-39", nil}, // rounded down
+ {"0x0.1234569p-125", "1.671815e-39", nil}, // rounded up
+ {"0x0.1234570p-125", "1.671815e-39", nil}, // 0x00123457
+ {"0x0.0000010p-125", "1e-45", nil}, // 0x00000001
+ {"0x0.00000081p-125", "1e-45", nil}, // rounded up
+ {"0x0.0000008p-125", "0", nil}, // rounded down
+ {"0x0.0000007p-125", "0", nil}, // rounded down
// 2^92 = 8388608p+69 = 4951760157141521099596496896 (4.9517602e27)
// is an exact power of two that needs 8 decimal digits to be correctly
diff --git a/src/strconv/atoi.go b/src/strconv/atoi.go
index ff33d555e4..186c9b3f86 100644
--- a/src/strconv/atoi.go
+++ b/src/strconv/atoi.go
@@ -6,6 +6,14 @@ package strconv
import "errors"
+// lower(c) is a lower-case letter if and only if
+// c is either that lower-case letter or the equivalent upper-case letter.
+// Instead of writing c == 'x' || c == 'X' one can write lower(c) == 'x'.
+// Note that lower of non-letters can produce other non-letters.
+func lower(c byte) byte {
+ return c | ('x' - 'X')
+}
+
// ErrRange indicates that a value is out of range for the target type.
var ErrRange = errors.New("value out of range")