| 1 |
package strconv |
| 2 |
|
| 3 |
import ( |
| 4 |
"math" |
| 5 |
) |
| 6 |
|
| 7 |
var float64pow10 = []float64{ |
| 8 |
1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, |
| 9 |
1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19, |
| 10 |
1e20, 1e21, 1e22, |
| 11 |
} |
| 12 |
|
| 13 |
// ParseFloat parses a byte-slice and returns the float it represents. |
| 14 |
// If an invalid character is encountered, it will stop there. |
| 15 |
func ParseFloat(b []byte) (float64, int) { |
| 16 |
i := 0 |
| 17 |
neg := false |
| 18 |
if i < len(b) && (b[i] == '+' || b[i] == '-') { |
| 19 |
neg = b[i] == '-' |
| 20 |
i++ |
| 21 |
} |
| 22 |
start := i |
| 23 |
dot := -1 |
| 24 |
trunk := -1 |
| 25 |
n := uint64(0) |
| 26 |
for ; i < len(b); i++ { |
| 27 |
c := b[i] |
| 28 |
if '0' <= c && c <= '9' { |
| 29 |
if trunk == -1 { |
| 30 |
if math.MaxUint64/10 < n || math.MaxUint64-uint64(c-'0') < n*10 { |
| 31 |
trunk = i |
| 32 |
} else { |
| 33 |
n *= 10 |
| 34 |
n += uint64(c - '0') |
| 35 |
} |
| 36 |
} |
| 37 |
} else if dot == -1 && c == '.' { |
| 38 |
dot = i |
| 39 |
} else { |
| 40 |
break |
| 41 |
} |
| 42 |
} |
| 43 |
if i == start || i == start+1 && dot == start { |
| 44 |
return 0.0, 0 |
| 45 |
} |
| 46 |
|
| 47 |
f := float64(n) |
| 48 |
if neg { |
| 49 |
f = -f |
| 50 |
} |
| 51 |
|
| 52 |
mantExp := int64(0) |
| 53 |
if dot != -1 { |
| 54 |
if trunk == -1 { |
| 55 |
trunk = i |
| 56 |
} |
| 57 |
if trunk < dot { |
| 58 |
// truncated before the dot, so no dot lies between trunk and dot |
| 59 |
mantExp = int64(trunk - dot) |
| 60 |
} else { |
| 61 |
mantExp = int64(trunk - dot - 1) |
| 62 |
} |
| 63 |
} else if trunk != -1 { |
| 64 |
mantExp = int64(trunk - i) |
| 65 |
} |
| 66 |
expExp := int64(0) |
| 67 |
if i < len(b) && (b[i] == 'e' || b[i] == 'E') { |
| 68 |
startExp := i |
| 69 |
i++ |
| 70 |
if e, expLen := ParseInt(b[i:]); 0 < expLen { |
| 71 |
expExp = e |
| 72 |
i += expLen |
| 73 |
} else { |
| 74 |
i = startExp |
| 75 |
} |
| 76 |
} |
| 77 |
exp := expExp - mantExp |
| 78 |
|
| 79 |
// copied from strconv/atof.go |
| 80 |
if exp == 0 { |
| 81 |
return f, i |
| 82 |
} else if 0 < exp && exp <= 15+22 { // int * 10^k |
| 83 |
// If exponent is big but number of digits is not, |
| 84 |
// can move a few zeros into the integer part. |
| 85 |
// Scale a copy, the fall-through below rescales f from scratch. |
| 86 |
g, gexp := f, exp |
| 87 |
if 22 < gexp { |
| 88 |
g *= float64pow10[gexp-22] |
| 89 |
gexp = 22 |
| 90 |
} |
| 91 |
if -1e15 <= g && g <= 1e15 { |
| 92 |
return g * float64pow10[gexp], i |
| 93 |
} |
| 94 |
} else if -22 <= exp && exp < 0 { // int / 10^k |
| 95 |
return f / float64pow10[-exp], i |
| 96 |
} |
| 97 |
if f == 0.0 { |
| 98 |
// 0 * math.Pow10(out of range) is NaN |
| 99 |
return f, i |
| 100 |
} |
| 101 |
h := f * math.Pow10(int(-mantExp)) |
| 102 |
h *= math.Pow10(int(expExp)) |
| 103 |
if h == 0.0 || math.IsInf(h, 0) { |
| 104 |
// either factor alone can leave math.Pow10's [-323,308] domain |
| 105 |
if exp < -308 { |
| 106 |
f *= math.Pow10(-308) |
| 107 |
exp += 308 |
| 108 |
} else if 308 < exp { |
| 109 |
f *= math.Pow10(308) |
| 110 |
exp -= 308 |
| 111 |
} |
| 112 |
h = f * math.Pow10(int(exp)) |
| 113 |
} |
| 114 |
return h, i |
| 115 |
} |
| 116 |
|
| 117 |
const log2 = 0.3010299956639812 |
| 118 |
|
| 119 |
func float64exp(f float64) int { |
| 120 |
exp2 := 0 |
| 121 |
if f != 0.0 { |
| 122 |
x := math.Float64bits(f) |
| 123 |
exp2 = int(x>>(64-11-1))&0x7FF - 1023 + 1 |
| 124 |
} |
| 125 |
|
| 126 |
exp10 := float64(exp2) * log2 |
| 127 |
if exp10 < 0 { |
| 128 |
exp10 -= 1.0 |
| 129 |
} |
| 130 |
return int(exp10) |
| 131 |
} |
| 132 |
|
| 133 |
// AppendFloat appends a float to `b` with precision `prec`. It returns the new slice and whether successful or not. Precision is the number of decimals to display, thus prec + 1 == number of significant digits. |
| 134 |
func AppendFloat(b []byte, f float64, prec int) []byte { |
| 135 |
if math.IsNaN(f) || math.IsInf(f, 0) { |
| 136 |
return b |
| 137 |
} |
| 138 |
|
| 139 |
neg := false |
| 140 |
if f < 0.0 { |
| 141 |
f = -f |
| 142 |
neg = true |
| 143 |
} |
| 144 |
if prec < 0 || 17 < prec { |
| 145 |
prec = 17 // maximum number of significant digits in double |
| 146 |
} |
| 147 |
prec -= float64exp(f) // number of digits in front of the dot |
| 148 |
f *= math.Pow10(prec) |
| 149 |
|
| 150 |
// calculate mantissa and exponent |
| 151 |
mant := int64(f) |
| 152 |
mantLen := LenInt(mant) |
| 153 |
mantExp := mantLen - prec - 1 |
| 154 |
if mant == 0 { |
| 155 |
return append(b, '0') |
| 156 |
} |
| 157 |
|
| 158 |
// expLen is zero for positive exponents, because positive exponents are determined later on in the big conversion loop |
| 159 |
exp := 0 |
| 160 |
expLen := 0 |
| 161 |
if 0 < mantExp { |
| 162 |
// positive exponent is determined in the loop below |
| 163 |
// but if we initially decreased the exponent to fit in an integer, we can't set the new exponent in the loop alone, |
| 164 |
// since the number of zeros at the end determines the positive exponent in the loop, and we just artificially lost zeros |
| 165 |
if prec < 0 { |
| 166 |
exp = mantExp |
| 167 |
} |
| 168 |
expLen = 1 + LenInt(int64(exp)) // e + digits |
| 169 |
} else if mantExp < -3 { |
| 170 |
exp = mantExp |
| 171 |
expLen = 1 + LenInt(int64(exp)) // e + minus + digits |
| 172 |
} else if mantExp < -1 { |
| 173 |
mantLen += -mantExp - 1 // extra zero between dot and first digit |
| 174 |
} |
| 175 |
|
| 176 |
// reserve space in b |
| 177 |
i := len(b) |
| 178 |
maxLen := 1 + mantLen + expLen // dot + mantissa digits + exponent |
| 179 |
if neg { |
| 180 |
maxLen++ |
| 181 |
} |
| 182 |
if cap(b) < i+maxLen { |
| 183 |
b = append(b, make([]byte, maxLen)...) |
| 184 |
} else { |
| 185 |
b = b[:i+maxLen] |
| 186 |
} |
| 187 |
|
| 188 |
// write to string representation |
| 189 |
if neg { |
| 190 |
b[i] = '-' |
| 191 |
i++ |
| 192 |
} |
| 193 |
|
| 194 |
// big conversion loop, start at the end and move to the front |
| 195 |
// initially print trailing zeros and remove them later on |
| 196 |
// for example if the first non-zero digit is three positions in front of the dot, it will overwrite the zeros with a positive exponent |
| 197 |
zero := true |
| 198 |
last := i + mantLen // right-most position of digit that is non-zero + dot |
| 199 |
dot := last - prec - exp // position of dot |
| 200 |
j := last |
| 201 |
for 0 < mant { |
| 202 |
if j == dot { |
| 203 |
b[j] = '.' |
| 204 |
j-- |
| 205 |
} |
| 206 |
newMant := mant / 10 |
| 207 |
digit := mant - 10*newMant |
| 208 |
if zero && 0 < digit { |
| 209 |
// first non-zero digit, if we are still behind the dot we can trim the end to this position |
| 210 |
// otherwise trim to the dot (including the dot) |
| 211 |
if dot < j { |
| 212 |
i = j + 1 |
| 213 |
// decrease negative exponent further to get rid of dot |
| 214 |
if exp < 0 { |
| 215 |
newExp := exp - (j - dot) |
| 216 |
// getting rid of the dot shouldn't lower the exponent to more digits (e.g. -9 -> -10) |
| 217 |
if LenInt(int64(newExp)) == LenInt(int64(exp)) { |
| 218 |
exp = newExp |
| 219 |
dot = j |
| 220 |
j-- |
| 221 |
i-- |
| 222 |
} |
| 223 |
} |
| 224 |
} else { |
| 225 |
i = dot |
| 226 |
} |
| 227 |
last = j |
| 228 |
zero = false |
| 229 |
} |
| 230 |
b[j] = '0' + byte(digit) |
| 231 |
j-- |
| 232 |
mant = newMant |
| 233 |
} |
| 234 |
|
| 235 |
if dot < j { |
| 236 |
// extra zeros behind the dot |
| 237 |
for dot < j { |
| 238 |
b[j] = '0' |
| 239 |
j-- |
| 240 |
} |
| 241 |
b[j] = '.' |
| 242 |
} else if last+3 < dot { |
| 243 |
// add positive exponent because we have 3 or more zeros in front of the dot |
| 244 |
i = last + 1 |
| 245 |
exp = dot - last - 1 |
| 246 |
} else if j == dot { |
| 247 |
// handle 0.1 |
| 248 |
b[j] = '.' |
| 249 |
} |
| 250 |
|
| 251 |
// exponent |
| 252 |
if exp != 0 { |
| 253 |
if exp == 1 { |
| 254 |
b[i] = '0' |
| 255 |
i++ |
| 256 |
} else if exp == 2 { |
| 257 |
b[i] = '0' |
| 258 |
b[i+1] = '0' |
| 259 |
i += 2 |
| 260 |
} else { |
| 261 |
b[i] = 'e' |
| 262 |
i++ |
| 263 |
if exp < 0 { |
| 264 |
b[i] = '-' |
| 265 |
i++ |
| 266 |
exp = -exp |
| 267 |
} |
| 268 |
i += LenInt(int64(exp)) |
| 269 |
j := i |
| 270 |
for 0 < exp { |
| 271 |
newExp := exp / 10 |
| 272 |
digit := exp - 10*newExp |
| 273 |
j-- |
| 274 |
b[j] = '0' + byte(digit) |
| 275 |
exp = newExp |
| 276 |
} |
| 277 |
} |
| 278 |
} |
| 279 |
return b[:i] |
| 280 |
} |