| |
| |
| |
|
|
| package strconv |
|
|
| |
| |
| |
| |
| |
|
|
| var optimize = true |
|
|
| |
| |
| |
| func commonPrefixLenIgnoreCase(s, prefix string) int { |
| n := min(len(prefix), len(s)) |
| for i := 0; i < n; i++ { |
| c := s[i] |
| if 'A' <= c && c <= 'Z' { |
| c += 'a' - 'A' |
| } |
| if c != prefix[i] { |
| return i |
| } |
| } |
| return n |
| } |
|
|
| |
| |
| |
| |
| |
| func special(s string) (f float64, n int, ok bool) { |
| if len(s) == 0 { |
| return 0, 0, false |
| } |
| sign := 1 |
| nsign := 0 |
| switch s[0] { |
| case '+', '-': |
| if s[0] == '-' { |
| sign = -1 |
| } |
| nsign = 1 |
| s = s[1:] |
| fallthrough |
| case 'i', 'I': |
| n := commonPrefixLenIgnoreCase(s, "infinity") |
| |
| |
| if 3 < n && n < 8 { |
| n = 3 |
| } |
| if n == 3 || n == 8 { |
| return inf(sign), nsign + n, true |
| } |
| case 'n', 'N': |
| if commonPrefixLenIgnoreCase(s, "nan") == 3 { |
| return nan(), 3, true |
| } |
| } |
| return 0, 0, false |
| } |
|
|
| func (b *decimal) set(s string) (ok bool) { |
| i := 0 |
| b.neg = false |
| b.trunc = false |
|
|
| |
| if i >= len(s) { |
| return |
| } |
| switch s[i] { |
| case '+': |
| i++ |
| case '-': |
| i++ |
| b.neg = true |
| } |
|
|
| |
| sawdot := false |
| sawdigits := false |
| for ; i < len(s); i++ { |
| switch { |
| case s[i] == '_': |
| |
| continue |
| case s[i] == '.': |
| if sawdot { |
| return |
| } |
| sawdot = true |
| b.dp = b.nd |
| continue |
|
|
| case '0' <= s[i] && s[i] <= '9': |
| sawdigits = true |
| if s[i] == '0' && b.nd == 0 { |
| b.dp-- |
| continue |
| } |
| if b.nd < len(b.d) { |
| b.d[b.nd] = s[i] |
| b.nd++ |
| } else if s[i] != '0' { |
| b.trunc = true |
| } |
| continue |
| } |
| break |
| } |
| if !sawdigits { |
| return |
| } |
| if !sawdot { |
| b.dp = b.nd |
| } |
|
|
| |
| |
| |
| |
| |
| if i < len(s) && lower(s[i]) == 'e' { |
| i++ |
| if i >= len(s) { |
| return |
| } |
| esign := 1 |
| switch s[i] { |
| case '+': |
| i++ |
| case '-': |
| 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' || s[i] == '_'); i++ { |
| if s[i] == '_' { |
| |
| continue |
| } |
| if e < 10000 { |
| e = e*10 + int(s[i]) - '0' |
| } |
| } |
| b.dp += e * esign |
| } |
|
|
| if i != len(s) { |
| return |
| } |
|
|
| ok = true |
| return |
| } |
|
|
| |
| |
| |
| |
| func readFloat(s string) (mantissa uint64, exp int, neg, trunc, hex bool, i int, ok bool) { |
| underscores := false |
|
|
| |
| if i >= len(s) { |
| return |
| } |
| switch s[i] { |
| case '+': |
| i++ |
| case '-': |
| i++ |
| neg = true |
| } |
|
|
| |
| base := uint64(10) |
| maxMantDigits := 19 |
| expChar := byte('e') |
| if i+2 < len(s) && s[i] == '0' && lower(s[i+1]) == 'x' { |
| base = 16 |
| maxMantDigits = 16 |
| i += 2 |
| expChar = 'p' |
| hex = true |
| } |
| sawdot := false |
| sawdigits := false |
| nd := 0 |
| ndMant := 0 |
| dp := 0 |
| loop: |
| for ; i < len(s); i++ { |
| switch c := s[i]; true { |
| case c == '_': |
| underscores = true |
| continue |
|
|
| case c == '.': |
| if sawdot { |
| break loop |
| } |
| sawdot = true |
| dp = nd |
| continue |
|
|
| case '0' <= c && c <= '9': |
| sawdigits = true |
| if c == '0' && nd == 0 { |
| dp-- |
| continue |
| } |
| nd++ |
| if ndMant < maxMantDigits { |
| mantissa *= base |
| mantissa += uint64(c - '0') |
| ndMant++ |
| } 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 |
| } |
| break |
| } |
| if !sawdigits { |
| return |
| } |
| if !sawdot { |
| dp = nd |
| } |
|
|
| if base == 16 { |
| dp *= 4 |
| ndMant *= 4 |
| } |
|
|
| |
| |
| |
| |
| |
| if i < len(s) && lower(s[i]) == expChar { |
| i++ |
| if i >= len(s) { |
| return |
| } |
| esign := 1 |
| switch s[i] { |
| case '+': |
| i++ |
| case '-': |
| 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' || s[i] == '_'); i++ { |
| if s[i] == '_' { |
| underscores = true |
| continue |
| } |
| if e < 10000 { |
| e = e*10 + int(s[i]) - '0' |
| } |
| } |
| dp += e * esign |
| } else if base == 16 { |
| |
| return |
| } |
|
|
| if mantissa != 0 { |
| exp = dp - ndMant |
| } |
|
|
| if underscores && !underscoreOK(s[:i]) { |
| return |
| } |
|
|
| ok = true |
| return |
| } |
|
|
| |
| var powtab = []int{1, 3, 6, 9, 13, 16, 19, 23, 26} |
|
|
| func (d *decimal) floatBits(flt *floatInfo) (b uint64, overflow bool) { |
| var exp int |
| var mant uint64 |
|
|
| |
| if d.nd == 0 { |
| mant = 0 |
| exp = flt.bias |
| goto out |
| } |
|
|
| |
| |
| |
| if d.dp > 310 { |
| goto overflow |
| } |
| if d.dp < -330 { |
| |
| mant = 0 |
| exp = flt.bias |
| goto out |
| } |
|
|
| |
| exp = 0 |
| for d.dp > 0 { |
| var n int |
| if d.dp >= len(powtab) { |
| n = 27 |
| } else { |
| n = powtab[d.dp] |
| } |
| d.Shift(-n) |
| exp += n |
| } |
| for d.dp < 0 || d.dp == 0 && d.d[0] < '5' { |
| var n int |
| if -d.dp >= len(powtab) { |
| n = 27 |
| } else { |
| n = powtab[-d.dp] |
| } |
| d.Shift(n) |
| exp -= n |
| } |
|
|
| |
| exp-- |
|
|
| |
| |
| |
| if exp < flt.bias+1 { |
| n := flt.bias + 1 - exp |
| d.Shift(-n) |
| exp += n |
| } |
|
|
| if exp-flt.bias >= 1<<flt.expbits-1 { |
| goto overflow |
| } |
|
|
| |
| d.Shift(int(1 + flt.mantbits)) |
| mant = d.RoundedInteger() |
|
|
| |
| if mant == 2<<flt.mantbits { |
| mant >>= 1 |
| exp++ |
| if exp-flt.bias >= 1<<flt.expbits-1 { |
| goto overflow |
| } |
| } |
|
|
| |
| if mant&(1<<flt.mantbits) == 0 { |
| exp = flt.bias |
| } |
| goto out |
|
|
| overflow: |
| |
| mant = 0 |
| exp = 1<<flt.expbits - 1 + flt.bias |
| overflow = true |
|
|
| out: |
| |
| bits := mant & (uint64(1)<<flt.mantbits - 1) |
| bits |= uint64((exp-flt.bias)&(1<<flt.expbits-1)) << flt.mantbits |
| if d.neg { |
| bits |= 1 << flt.mantbits << flt.expbits |
| } |
| return bits, overflow |
| } |
|
|
| |
| var float64pow10 = []float64{ |
| 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, |
| 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19, |
| 1e20, 1e21, 1e22, |
| } |
| var float32pow10 = []float32{1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10} |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| func atof64exact(mantissa uint64, exp int, neg bool) (f float64, ok bool) { |
| if mantissa>>float64info.mantbits != 0 { |
| return |
| } |
| f = float64(mantissa) |
| if neg { |
| f = -f |
| } |
| switch { |
| case exp == 0: |
| |
| return f, true |
| |
| |
| case exp > 0 && exp <= 15+22: |
| |
| |
| if exp > 22 { |
| f *= float64pow10[exp-22] |
| exp = 22 |
| } |
| if f > 1e15 || f < -1e15 { |
| |
| return |
| } |
| return f * float64pow10[exp], true |
| case exp < 0 && exp >= -22: |
| return f / float64pow10[-exp], true |
| } |
| return |
| } |
|
|
| |
| |
| func atof32exact(mantissa uint64, exp int, neg bool) (f float32, ok bool) { |
| if mantissa>>float32MantBits != 0 { |
| return |
| } |
| f = float32(mantissa) |
| if neg { |
| f = -f |
| } |
| switch { |
| case exp == 0: |
| return f, true |
| |
| |
| case exp > 0 && exp <= 7+10: |
| |
| |
| if exp > 10 { |
| f *= float32pow10[exp-10] |
| exp = 10 |
| } |
| if f > 1e7 || f < -1e7 { |
| |
| return |
| } |
| return f * float32pow10[exp], true |
| case exp < 0 && exp >= -10: |
| return f / float32pow10[-exp], true |
| } |
| return |
| } |
|
|
| |
| |
| |
| |
| |
| 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) |
|
|
| |
| |
| |
| |
| |
| |
| 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++ |
| } |
|
|
| |
| |
| |
| for mantissa > 1 && exp < minExp-2 { |
| mantissa = mantissa>>1 | mantissa&1 |
| exp++ |
| } |
|
|
| |
| round := mantissa & 3 |
| mantissa >>= 2 |
| round |= mantissa & 1 |
| exp += 2 |
| if round == 3 { |
| mantissa++ |
| if mantissa == 1<<(1+flt.mantbits) { |
| mantissa >>= 1 |
| exp++ |
| } |
| } |
|
|
| if mantissa>>flt.mantbits == 0 { |
| exp = flt.bias |
| } |
| var err error |
| if exp > maxExp { |
| mantissa = 1 << flt.mantbits |
| exp = maxExp + 1 |
| err = ErrRange |
| } |
|
|
| 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(float32frombits(uint32(bits))), err |
| } |
| return float64frombits(bits), err |
| } |
|
|
| const fnParseFloat = "ParseFloat" |
|
|
| func atof32(s string) (f float32, n int, err error) { |
| if val, n, ok := special(s); ok { |
| return float32(val), n, nil |
| } |
|
|
| mantissa, exp, neg, trunc, hex, n, ok := readFloat(s) |
| if !ok { |
| return 0, n, ErrSyntax |
| } |
|
|
| if hex { |
| f, err := atofHex(s[:n], &float32info, mantissa, exp, neg, trunc) |
| return float32(f), n, err |
| } |
|
|
| if optimize { |
| |
| |
| if !trunc { |
| if f, ok := atof32exact(mantissa, exp, neg); ok { |
| return f, n, nil |
| } |
| } |
| f, ok := eiselLemire32(mantissa, exp, neg) |
| if ok { |
| if !trunc { |
| return f, n, nil |
| } |
| |
| |
| |
| fUp, ok := eiselLemire32(mantissa+1, exp, neg) |
| if ok && f == fUp { |
| return f, n, nil |
| } |
| } |
| } |
|
|
| |
| var d decimal |
| if !d.set(s[:n]) { |
| return 0, n, ErrSyntax |
| } |
| b, ovf := d.floatBits(&float32info) |
| f = float32frombits(uint32(b)) |
| if ovf { |
| err = ErrRange |
| } |
| return f, n, err |
| } |
|
|
| func atof64(s string) (f float64, n int, err error) { |
| if val, n, ok := special(s); ok { |
| return val, n, nil |
| } |
|
|
| mantissa, exp, neg, trunc, hex, n, ok := readFloat(s) |
| if !ok { |
| return 0, n, ErrSyntax |
| } |
|
|
| if hex { |
| f, err := atofHex(s[:n], &float64info, mantissa, exp, neg, trunc) |
| return f, n, err |
| } |
|
|
| if optimize { |
| |
| |
| if !trunc { |
| if f, ok := atof64exact(mantissa, exp, neg); ok { |
| return f, n, nil |
| } |
| } |
| f, ok := eiselLemire64(mantissa, exp, neg) |
| if ok { |
| if !trunc { |
| return f, n, nil |
| } |
| |
| |
| |
| fUp, ok := eiselLemire64(mantissa+1, exp, neg) |
| if ok && f == fUp { |
| return f, n, nil |
| } |
| } |
| } |
|
|
| |
| var d decimal |
| if !d.set(s[:n]) { |
| return 0, n, ErrSyntax |
| } |
| b, ovf := d.floatBits(&float64info) |
| f = float64frombits(b) |
| if ovf { |
| err = ErrRange |
| } |
| return f, n, err |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| func ParseFloat(s string, bitSize int) (float64, error) { |
| f, n, err := parseFloatPrefix(s, bitSize) |
| if n != len(s) { |
| return 0, ErrSyntax |
| } |
| return f, err |
| } |
|
|
| func parseFloatPrefix(s string, bitSize int) (float64, int, error) { |
| if bitSize == 32 { |
| f, n, err := atof32(s) |
| return float64(f), n, err |
| } |
| return atof64(s) |
| } |
|
|