2008-11-17 13:34:03 -07:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2011-04-19 17:57:05 -06:00
|
|
|
package strconv
|
|
|
|
|
2009-01-15 18:22:17 -07:00
|
|
|
// decimal to binary floating point conversion.
|
2008-11-17 13:34:03 -07:00
|
|
|
// Algorithm:
|
|
|
|
// 1) Store input in multiprecision decimal.
|
|
|
|
// 2) Multiply/divide decimal by powers of two until in range [0.5, 1)
|
|
|
|
// 3) Multiply by 2^precision and round to get mantissa.
|
|
|
|
|
2011-11-01 20:05:34 -06:00
|
|
|
import "math"
|
2008-11-17 13:34:03 -07:00
|
|
|
|
2010-10-25 22:25:13 -06:00
|
|
|
var optimize = true // can change for testing
|
2008-11-19 13:50:34 -07:00
|
|
|
|
2010-11-30 13:53:42 -07:00
|
|
|
func equalIgnoreCase(s1, s2 string) bool {
|
|
|
|
if len(s1) != len(s2) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for i := 0; i < len(s1); i++ {
|
|
|
|
c1 := s1[i]
|
|
|
|
if 'A' <= c1 && c1 <= 'Z' {
|
|
|
|
c1 += 'a' - 'A'
|
|
|
|
}
|
|
|
|
c2 := s2[i]
|
|
|
|
if 'A' <= c2 && c2 <= 'Z' {
|
|
|
|
c2 += 'a' - 'A'
|
|
|
|
}
|
|
|
|
if c1 != c2 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func special(s string) (f float64, ok bool) {
|
2012-04-21 05:56:51 -06:00
|
|
|
if len(s) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
switch s[0] {
|
|
|
|
default:
|
|
|
|
return
|
|
|
|
case '+':
|
|
|
|
if equalIgnoreCase(s, "+inf") || equalIgnoreCase(s, "+infinity") {
|
|
|
|
return math.Inf(1), true
|
|
|
|
}
|
|
|
|
case '-':
|
|
|
|
if equalIgnoreCase(s, "-inf") || equalIgnoreCase(s, "-infinity") {
|
|
|
|
return math.Inf(-1), true
|
|
|
|
}
|
|
|
|
case 'n', 'N':
|
|
|
|
if equalIgnoreCase(s, "nan") {
|
|
|
|
return math.NaN(), true
|
|
|
|
}
|
|
|
|
case 'i', 'I':
|
|
|
|
if equalIgnoreCase(s, "inf") || equalIgnoreCase(s, "infinity") {
|
|
|
|
return math.Inf(1), true
|
|
|
|
}
|
2010-11-30 13:53:42 -07:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2011-08-25 15:54:14 -06:00
|
|
|
func (b *decimal) set(s string) (ok bool) {
|
2009-12-15 16:40:16 -07:00
|
|
|
i := 0
|
2011-08-25 15:54:14 -06:00
|
|
|
b.neg = false
|
2012-02-07 21:37:15 -07:00
|
|
|
b.trunc = false
|
2008-11-17 13:34:03 -07:00
|
|
|
|
|
|
|
// optional sign
|
|
|
|
if i >= len(s) {
|
2009-11-09 13:07:39 -07:00
|
|
|
return
|
2008-11-17 13:34:03 -07:00
|
|
|
}
|
|
|
|
switch {
|
|
|
|
case s[i] == '+':
|
2009-11-09 13:07:39 -07:00
|
|
|
i++
|
2008-11-17 13:34:03 -07:00
|
|
|
case s[i] == '-':
|
2011-08-25 15:54:14 -06:00
|
|
|
b.neg = true
|
2009-12-15 16:40:16 -07:00
|
|
|
i++
|
2008-11-17 13:34:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// digits
|
2009-12-15 16:40:16 -07:00
|
|
|
sawdot := false
|
|
|
|
sawdigits := false
|
2008-11-17 13:34:03 -07:00
|
|
|
for ; i < len(s); i++ {
|
|
|
|
switch {
|
|
|
|
case s[i] == '.':
|
|
|
|
if sawdot {
|
2009-11-09 13:07:39 -07:00
|
|
|
return
|
2008-11-17 13:34:03 -07:00
|
|
|
}
|
2009-12-15 16:40:16 -07:00
|
|
|
sawdot = true
|
|
|
|
b.dp = b.nd
|
|
|
|
continue
|
2008-11-17 13:34:03 -07:00
|
|
|
|
|
|
|
case '0' <= s[i] && s[i] <= '9':
|
2009-12-15 16:40:16 -07:00
|
|
|
sawdigits = true
|
|
|
|
if s[i] == '0' && b.nd == 0 { // ignore leading zeros
|
|
|
|
b.dp--
|
|
|
|
continue
|
2008-11-17 13:34:03 -07:00
|
|
|
}
|
2012-02-07 21:37:15 -07:00
|
|
|
if b.nd < len(b.d) {
|
|
|
|
b.d[b.nd] = s[i]
|
|
|
|
b.nd++
|
|
|
|
} else if s[i] != '0' {
|
|
|
|
b.trunc = true
|
|
|
|
}
|
2009-12-15 16:40:16 -07:00
|
|
|
continue
|
2008-11-17 13:34:03 -07:00
|
|
|
}
|
2009-12-15 16:40:16 -07:00
|
|
|
break
|
2008-11-17 13:34:03 -07:00
|
|
|
}
|
|
|
|
if !sawdigits {
|
2009-11-09 13:07:39 -07:00
|
|
|
return
|
2008-11-17 13:34:03 -07:00
|
|
|
}
|
|
|
|
if !sawdot {
|
2009-11-09 13:07:39 -07:00
|
|
|
b.dp = b.nd
|
2008-11-17 13:34:03 -07:00
|
|
|
}
|
|
|
|
|
2008-11-17 18:22:51 -07:00
|
|
|
// 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.
|
2010-12-27 11:12:10 -07:00
|
|
|
if i < len(s) && (s[i] == 'e' || s[i] == 'E') {
|
2009-12-15 16:40:16 -07:00
|
|
|
i++
|
2008-11-17 13:34:03 -07:00
|
|
|
if i >= len(s) {
|
2009-11-09 13:07:39 -07:00
|
|
|
return
|
2008-11-17 13:34:03 -07:00
|
|
|
}
|
2009-12-15 16:40:16 -07:00
|
|
|
esign := 1
|
2008-11-17 13:34:03 -07:00
|
|
|
if s[i] == '+' {
|
2009-11-09 13:07:39 -07:00
|
|
|
i++
|
2008-11-17 13:34:03 -07:00
|
|
|
} else if s[i] == '-' {
|
2009-12-15 16:40:16 -07:00
|
|
|
i++
|
|
|
|
esign = -1
|
2008-11-17 13:34:03 -07:00
|
|
|
}
|
|
|
|
if i >= len(s) || s[i] < '0' || s[i] > '9' {
|
2009-11-09 13:07:39 -07:00
|
|
|
return
|
2008-11-17 13:34:03 -07:00
|
|
|
}
|
2009-12-15 16:40:16 -07:00
|
|
|
e := 0
|
2008-11-17 13:34:03 -07:00
|
|
|
for ; i < len(s) && '0' <= s[i] && s[i] <= '9'; i++ {
|
2008-11-17 18:22:51 -07:00
|
|
|
if e < 10000 {
|
2009-11-09 13:07:39 -07:00
|
|
|
e = e*10 + int(s[i]) - '0'
|
2008-11-17 18:22:51 -07:00
|
|
|
}
|
2008-11-17 13:34:03 -07:00
|
|
|
}
|
2009-12-15 16:40:16 -07:00
|
|
|
b.dp += e * esign
|
2008-11-17 13:34:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if i != len(s) {
|
2009-11-09 13:07:39 -07:00
|
|
|
return
|
2008-11-17 13:34:03 -07:00
|
|
|
}
|
|
|
|
|
2009-12-15 16:40:16 -07:00
|
|
|
ok = true
|
|
|
|
return
|
2008-11-17 13:34:03 -07:00
|
|
|
}
|
|
|
|
|
2012-04-21 05:56:51 -06:00
|
|
|
// readFloat reads a decimal mantissa and exponent from a float
|
|
|
|
// string representation. It sets ok to 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
|
|
|
|
i := 0
|
|
|
|
|
|
|
|
// optional sign
|
|
|
|
if i >= len(s) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
switch {
|
|
|
|
case s[i] == '+':
|
|
|
|
i++
|
|
|
|
case s[i] == '-':
|
|
|
|
neg = true
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
// digits
|
|
|
|
sawdot := false
|
|
|
|
sawdigits := false
|
|
|
|
nd := 0
|
|
|
|
ndMant := 0
|
|
|
|
dp := 0
|
|
|
|
for ; i < len(s); i++ {
|
|
|
|
switch c := s[i]; true {
|
|
|
|
case c == '.':
|
|
|
|
if sawdot {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
sawdot = true
|
|
|
|
dp = nd
|
|
|
|
continue
|
|
|
|
|
|
|
|
case '0' <= c && c <= '9':
|
|
|
|
sawdigits = true
|
|
|
|
if c == '0' && nd == 0 { // ignore leading zeros
|
|
|
|
dp--
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
nd++
|
|
|
|
if ndMant < uint64digits {
|
|
|
|
mantissa *= 10
|
|
|
|
mantissa += uint64(c - '0')
|
|
|
|
ndMant++
|
|
|
|
} else if s[i] != '0' {
|
|
|
|
trunc = true
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if !sawdigits {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !sawdot {
|
|
|
|
dp = nd
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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') {
|
|
|
|
i++
|
|
|
|
if i >= len(s) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
esign := 1
|
|
|
|
if s[i] == '+' {
|
|
|
|
i++
|
|
|
|
} else if s[i] == '-' {
|
|
|
|
i++
|
|
|
|
esign = -1
|
|
|
|
}
|
|
|
|
if i >= len(s) || s[i] < '0' || s[i] > '9' {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
e := 0
|
|
|
|
for ; i < len(s) && '0' <= s[i] && s[i] <= '9'; i++ {
|
|
|
|
if e < 10000 {
|
|
|
|
e = e*10 + int(s[i]) - '0'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dp += e * esign
|
|
|
|
}
|
|
|
|
|
|
|
|
if i != len(s) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-04-19 15:42:15 -06:00
|
|
|
if mantissa != 0 {
|
|
|
|
exp = dp - ndMant
|
|
|
|
}
|
2012-04-21 05:56:51 -06:00
|
|
|
ok = true
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2009-01-15 18:22:17 -07:00
|
|
|
// decimal power of ten to binary power of two.
|
2009-10-07 12:55:06 -06:00
|
|
|
var powtab = []int{1, 3, 6, 9, 13, 16, 19, 23, 26}
|
2008-11-17 13:34:03 -07:00
|
|
|
|
2011-08-25 15:54:14 -06:00
|
|
|
func (d *decimal) floatBits(flt *floatInfo) (b uint64, overflow bool) {
|
2009-12-15 16:40:16 -07:00
|
|
|
var exp int
|
|
|
|
var mant uint64
|
2008-12-03 14:29:13 -07:00
|
|
|
|
2008-11-17 13:34:03 -07:00
|
|
|
// Zero is always a special case.
|
|
|
|
if d.nd == 0 {
|
2009-12-15 16:40:16 -07:00
|
|
|
mant = 0
|
|
|
|
exp = flt.bias
|
|
|
|
goto out
|
2008-11-17 13:34:03 -07:00
|
|
|
}
|
|
|
|
|
2008-11-17 18:22:51 -07:00
|
|
|
// Obvious overflow/underflow.
|
|
|
|
// These bounds are for 64-bit floats.
|
|
|
|
// Will have to change if we want to support 80-bit floats in the future.
|
|
|
|
if d.dp > 310 {
|
2009-11-09 13:07:39 -07:00
|
|
|
goto overflow
|
2008-11-17 18:22:51 -07:00
|
|
|
}
|
|
|
|
if d.dp < -330 {
|
|
|
|
// zero
|
2009-12-15 16:40:16 -07:00
|
|
|
mant = 0
|
|
|
|
exp = flt.bias
|
|
|
|
goto out
|
2008-11-17 18:22:51 -07:00
|
|
|
}
|
2008-11-17 13:34:03 -07:00
|
|
|
|
|
|
|
// Scale by powers of two until in range [0.5, 1.0)
|
2009-12-15 16:40:16 -07:00
|
|
|
exp = 0
|
2008-11-17 13:34:03 -07:00
|
|
|
for d.dp > 0 {
|
2009-12-15 16:40:16 -07:00
|
|
|
var n int
|
2008-11-17 13:34:03 -07:00
|
|
|
if d.dp >= len(powtab) {
|
2009-11-09 13:07:39 -07:00
|
|
|
n = 27
|
2008-11-17 13:34:03 -07:00
|
|
|
} else {
|
2009-11-09 13:07:39 -07:00
|
|
|
n = powtab[d.dp]
|
2008-11-17 13:34:03 -07:00
|
|
|
}
|
2009-12-15 16:40:16 -07:00
|
|
|
d.Shift(-n)
|
|
|
|
exp += n
|
2008-11-17 13:34:03 -07:00
|
|
|
}
|
|
|
|
for d.dp < 0 || d.dp == 0 && d.d[0] < '5' {
|
2009-12-15 16:40:16 -07:00
|
|
|
var n int
|
2008-11-17 13:34:03 -07:00
|
|
|
if -d.dp >= len(powtab) {
|
2009-11-09 13:07:39 -07:00
|
|
|
n = 27
|
2008-11-17 13:34:03 -07:00
|
|
|
} else {
|
2009-11-09 13:07:39 -07:00
|
|
|
n = powtab[-d.dp]
|
2008-11-17 13:34:03 -07:00
|
|
|
}
|
2009-12-15 16:40:16 -07:00
|
|
|
d.Shift(n)
|
|
|
|
exp -= n
|
2008-11-17 13:34:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Our range is [0.5,1) but floating point range is [1,2).
|
2009-12-15 16:40:16 -07:00
|
|
|
exp--
|
2008-11-17 13:34:03 -07:00
|
|
|
|
|
|
|
// Minimum representable exponent is flt.bias+1.
|
|
|
|
// If the exponent is smaller, move it up and
|
|
|
|
// adjust d accordingly.
|
2009-11-09 22:23:52 -07:00
|
|
|
if exp < flt.bias+1 {
|
2009-12-15 16:40:16 -07:00
|
|
|
n := flt.bias + 1 - exp
|
|
|
|
d.Shift(-n)
|
|
|
|
exp += n
|
2008-11-17 13:34:03 -07:00
|
|
|
}
|
|
|
|
|
2009-11-09 22:23:52 -07:00
|
|
|
if exp-flt.bias >= 1<<flt.expbits-1 {
|
2009-11-09 13:07:39 -07:00
|
|
|
goto overflow
|
2008-11-17 18:22:51 -07:00
|
|
|
}
|
2008-11-17 13:34:03 -07:00
|
|
|
|
|
|
|
// Extract 1+flt.mantbits bits.
|
2011-08-25 15:54:14 -06:00
|
|
|
d.Shift(int(1 + flt.mantbits))
|
|
|
|
mant = d.RoundedInteger()
|
2008-11-17 18:22:51 -07:00
|
|
|
|
|
|
|
// Rounding might have added a bit; shift down.
|
2009-11-09 22:23:52 -07:00
|
|
|
if mant == 2<<flt.mantbits {
|
2009-12-15 16:40:16 -07:00
|
|
|
mant >>= 1
|
|
|
|
exp++
|
2009-11-09 22:23:52 -07:00
|
|
|
if exp-flt.bias >= 1<<flt.expbits-1 {
|
2009-11-09 13:07:39 -07:00
|
|
|
goto overflow
|
2008-11-17 18:22:51 -07:00
|
|
|
}
|
|
|
|
}
|
2008-11-17 13:34:03 -07:00
|
|
|
|
|
|
|
// Denormalized?
|
2009-11-09 22:23:52 -07:00
|
|
|
if mant&(1<<flt.mantbits) == 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
exp = flt.bias
|
2008-11-17 13:34:03 -07:00
|
|
|
}
|
2009-12-15 16:40:16 -07:00
|
|
|
goto out
|
2008-11-17 13:34:03 -07:00
|
|
|
|
2008-11-17 18:22:51 -07:00
|
|
|
overflow:
|
|
|
|
// ±Inf
|
2009-12-15 16:40:16 -07:00
|
|
|
mant = 0
|
|
|
|
exp = 1<<flt.expbits - 1 + flt.bias
|
|
|
|
overflow = true
|
2008-11-17 18:22:51 -07:00
|
|
|
|
|
|
|
out:
|
2008-11-17 13:34:03 -07:00
|
|
|
// Assemble bits.
|
2009-12-15 16:40:16 -07:00
|
|
|
bits := mant & (uint64(1)<<flt.mantbits - 1)
|
|
|
|
bits |= uint64((exp-flt.bias)&(1<<flt.expbits-1)) << flt.mantbits
|
2011-08-25 15:54:14 -06:00
|
|
|
if d.neg {
|
2009-11-09 13:07:39 -07:00
|
|
|
bits |= 1 << flt.mantbits << flt.expbits
|
2008-11-17 13:34:03 -07:00
|
|
|
}
|
2009-12-15 16:40:16 -07:00
|
|
|
return bits, overflow
|
2008-11-17 18:22:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Exact powers of 10.
|
2009-10-07 12:55:06 -06:00
|
|
|
var float64pow10 = []float64{
|
2008-11-17 18:22:51 -07:00
|
|
|
1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
|
|
|
|
1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
|
2009-10-07 12:55:06 -06:00
|
|
|
1e20, 1e21, 1e22,
|
2009-03-03 09:39:12 -07:00
|
|
|
}
|
2009-10-07 12:55:06 -06:00
|
|
|
var float32pow10 = []float32{1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10}
|
2008-11-17 13:34:03 -07:00
|
|
|
|
2012-04-21 05:56:51 -06:00
|
|
|
// If possible to convert decimal representation to 64-bit float f exactly,
|
2009-01-15 18:22:17 -07:00
|
|
|
// entirely in floating-point math, do so, avoiding the expense of decimalToFloatBits.
|
2008-11-17 18:22:51 -07:00
|
|
|
// Three common cases:
|
|
|
|
// value is exact integer
|
|
|
|
// value is exact integer * exact power of ten
|
|
|
|
// value is exact integer / exact power of ten
|
|
|
|
// These all produce potentially inexact but correctly rounded answers.
|
2012-04-21 05:56:51 -06:00
|
|
|
func atof64exact(mantissa uint64, exp int, neg bool) (f float64, ok bool) {
|
|
|
|
if mantissa>>float64info.mantbits != 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
return
|
2008-11-17 18:22:51 -07:00
|
|
|
}
|
2012-04-21 05:56:51 -06:00
|
|
|
f = float64(mantissa)
|
|
|
|
if neg {
|
|
|
|
f = -f
|
|
|
|
}
|
2008-11-17 18:22:51 -07:00
|
|
|
switch {
|
2012-04-21 05:56:51 -06:00
|
|
|
case exp == 0:
|
|
|
|
// an integer.
|
2009-12-15 16:40:16 -07:00
|
|
|
return f, true
|
2012-04-21 05:56:51 -06:00
|
|
|
// Exact integers are <= 10^15.
|
|
|
|
// Exact powers of ten are <= 10^22.
|
|
|
|
case exp > 0 && exp <= 15+22: // int * 10^k
|
2008-11-17 18:22:51 -07:00
|
|
|
// If exponent is big but number of digits is not,
|
|
|
|
// can move a few zeros into the integer part.
|
2012-04-21 05:56:51 -06:00
|
|
|
if exp > 22 {
|
|
|
|
f *= float64pow10[exp-22]
|
|
|
|
exp = 22
|
2008-11-17 18:22:51 -07:00
|
|
|
}
|
2012-04-21 05:56:51 -06:00
|
|
|
if f > 1e15 || f < -1e15 {
|
|
|
|
// the exponent was really too large.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return f * float64pow10[exp], true
|
|
|
|
case exp < 0 && exp >= -22: // int / 10^k
|
|
|
|
return f / float64pow10[-exp], true
|
2008-11-17 18:22:51 -07:00
|
|
|
}
|
2009-12-15 16:40:16 -07:00
|
|
|
return
|
2008-11-17 13:34:03 -07:00
|
|
|
}
|
|
|
|
|
2012-06-13 15:52:00 -06:00
|
|
|
// If possible to compute mantissa*10^exp to 32-bit float f exactly,
|
2008-11-17 13:34:03 -07:00
|
|
|
// entirely in floating-point math, do so, avoiding the machinery above.
|
2012-06-13 15:52:00 -06:00
|
|
|
func atof32exact(mantissa uint64, exp int, neg bool) (f float32, ok bool) {
|
|
|
|
if mantissa>>float32info.mantbits != 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
return
|
2008-11-17 18:22:51 -07:00
|
|
|
}
|
2012-06-13 15:52:00 -06:00
|
|
|
f = float32(mantissa)
|
|
|
|
if neg {
|
|
|
|
f = -f
|
|
|
|
}
|
2008-11-17 18:22:51 -07:00
|
|
|
switch {
|
2012-06-13 15:52:00 -06:00
|
|
|
case exp == 0:
|
2009-12-15 16:40:16 -07:00
|
|
|
return f, true
|
2012-06-13 15:52:00 -06:00
|
|
|
// Exact integers are <= 10^7.
|
|
|
|
// Exact powers of ten are <= 10^10.
|
|
|
|
case exp > 0 && exp <= 7+10: // int * 10^k
|
2008-11-17 18:22:51 -07:00
|
|
|
// If exponent is big but number of digits is not,
|
|
|
|
// can move a few zeros into the integer part.
|
2012-06-13 15:52:00 -06:00
|
|
|
if exp > 10 {
|
|
|
|
f *= float32pow10[exp-10]
|
|
|
|
exp = 10
|
2008-11-17 18:22:51 -07:00
|
|
|
}
|
2012-06-13 15:52:00 -06:00
|
|
|
if f > 1e7 || f < -1e7 {
|
|
|
|
// the exponent was really too large.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return f * float32pow10[exp], true
|
|
|
|
case exp < 0 && exp >= -10: // int / 10^k
|
|
|
|
return f / float32pow10[-exp], true
|
2008-11-17 18:22:51 -07:00
|
|
|
}
|
2009-12-15 16:40:16 -07:00
|
|
|
return
|
2008-11-17 13:34:03 -07:00
|
|
|
}
|
|
|
|
|
2011-12-13 11:42:05 -07:00
|
|
|
const fnParseFloat = "ParseFloat"
|
|
|
|
|
2011-12-05 13:48:21 -07:00
|
|
|
func atof32(s string) (f float32, err error) {
|
2010-11-30 13:53:42 -07:00
|
|
|
if val, ok := special(s); ok {
|
|
|
|
return float32(val), nil
|
|
|
|
}
|
|
|
|
|
2012-06-13 15:52:00 -06:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-08-25 15:54:14 -06:00
|
|
|
var d decimal
|
|
|
|
if !d.set(s) {
|
2011-12-13 11:42:05 -07:00
|
|
|
return 0, syntaxError(fnParseFloat, s)
|
2008-11-17 13:34:03 -07:00
|
|
|
}
|
2011-08-25 15:54:14 -06:00
|
|
|
b, ovf := d.floatBits(&float32info)
|
2009-12-15 16:40:16 -07:00
|
|
|
f = math.Float32frombits(uint32(b))
|
2008-11-18 18:12:07 -07:00
|
|
|
if ovf {
|
2011-12-13 11:42:05 -07:00
|
|
|
err = rangeError(fnParseFloat, s)
|
2008-11-18 18:12:07 -07:00
|
|
|
}
|
2009-12-15 16:40:16 -07:00
|
|
|
return f, err
|
2008-11-17 13:34:03 -07:00
|
|
|
}
|
|
|
|
|
2011-12-05 13:48:21 -07:00
|
|
|
func atof64(s string) (f float64, err error) {
|
2010-11-30 13:53:42 -07:00
|
|
|
if val, ok := special(s); ok {
|
|
|
|
return val, nil
|
|
|
|
}
|
|
|
|
|
2008-11-19 13:50:34 -07:00
|
|
|
if optimize {
|
2012-04-21 05:56:51 -06:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Try another fast path.
|
|
|
|
ext := new(extFloat)
|
2012-06-13 15:52:00 -06:00
|
|
|
if ok := ext.AssignDecimal(mantissa, exp, neg, trunc, &float64info); ok {
|
|
|
|
b, ovf := ext.floatBits(&float64info)
|
2012-04-21 05:56:51 -06:00
|
|
|
f = math.Float64frombits(b)
|
|
|
|
if ovf {
|
|
|
|
err = rangeError(fnParseFloat, s)
|
|
|
|
}
|
|
|
|
return f, err
|
2011-12-19 14:45:51 -07:00
|
|
|
}
|
|
|
|
}
|
2008-11-18 18:12:07 -07:00
|
|
|
}
|
2012-04-21 05:56:51 -06:00
|
|
|
var d decimal
|
|
|
|
if !d.set(s) {
|
|
|
|
return 0, syntaxError(fnParseFloat, s)
|
|
|
|
}
|
2011-08-25 15:54:14 -06:00
|
|
|
b, ovf := d.floatBits(&float64info)
|
2009-12-15 16:40:16 -07:00
|
|
|
f = math.Float64frombits(b)
|
2008-11-18 18:12:07 -07:00
|
|
|
if ovf {
|
2011-12-13 11:42:05 -07:00
|
|
|
err = rangeError(fnParseFloat, s)
|
2008-11-17 13:34:03 -07:00
|
|
|
}
|
2009-12-15 16:40:16 -07:00
|
|
|
return f, err
|
2008-11-17 13:34:03 -07:00
|
|
|
}
|
|
|
|
|
2011-12-05 13:48:21 -07:00
|
|
|
// ParseFloat converts the string s to a floating-point number
|
|
|
|
// with the precision specified by bitSize: 32 for float32, or 64 for float64.
|
|
|
|
// 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
|
|
|
|
// using IEEE754 unbiased rounding.
|
|
|
|
//
|
|
|
|
// The errors that ParseFloat returns have concrete type *NumError
|
|
|
|
// and include err.Num = s.
|
|
|
|
//
|
2013-06-13 18:04:47 -06:00
|
|
|
// If s is not syntactically well-formed, ParseFloat returns err.Err = ErrSyntax.
|
2011-12-05 13:48:21 -07:00
|
|
|
//
|
|
|
|
// If s is syntactically well-formed but is more than 1/2 ULP
|
|
|
|
// away from the largest floating point number of the given size,
|
2013-06-13 18:04:47 -06:00
|
|
|
// ParseFloat returns f = ±Inf, err.Err = ErrRange.
|
2016-02-28 16:52:49 -07:00
|
|
|
func ParseFloat(s string, bitSize int) (float64, error) {
|
2011-12-05 13:48:21 -07:00
|
|
|
if bitSize == 32 {
|
2016-02-28 16:52:49 -07:00
|
|
|
f, err := atof32(s)
|
|
|
|
return float64(f), err
|
2010-06-18 23:43:37 -06:00
|
|
|
}
|
2016-02-28 16:52:49 -07:00
|
|
|
return atof64(s)
|
2010-06-18 23:43:37 -06:00
|
|
|
}
|