1
2
3
4
5 package strconv
6
7 type floatInfo struct {
8 mantbits uint
9 expbits uint
10 bias int
11 }
12
13 var (
14 float32info = floatInfo{float32MantBits, float32ExpBits, float32Bias}
15 float64info = floatInfo{float64MantBits, float64ExpBits, float64Bias}
16 )
17
18
19
20
21
22
23
24 var optimize = true
25
26
27
28
29 func commonPrefixLenIgnoreCase(s, prefix string) int {
30 n := min(len(prefix), len(s))
31 for i := 0; i < n; i++ {
32 c := s[i]
33 if 'A' <= c && c <= 'Z' {
34 c += 'a' - 'A'
35 }
36 if c != prefix[i] {
37 return i
38 }
39 }
40 return n
41 }
42
43
44
45
46
47
48 func special(s string) (f float64, n int, ok bool) {
49 if len(s) == 0 {
50 return 0, 0, false
51 }
52 sign := 1
53 nsign := 0
54 switch s[0] {
55 case '+', '-':
56 if s[0] == '-' {
57 sign = -1
58 }
59 nsign = 1
60 s = s[1:]
61 fallthrough
62 case 'i', 'I':
63 n := commonPrefixLenIgnoreCase(s, "infinity")
64
65
66 if 3 < n && n < 8 {
67 n = 3
68 }
69 if n == 3 || n == 8 {
70 return inf(sign), nsign + n, true
71 }
72 case 'n', 'N':
73 if commonPrefixLenIgnoreCase(s, "nan") == 3 {
74 return nan(), 3, true
75 }
76 }
77 return 0, 0, false
78 }
79
80 func (b *decimal) set(s string) (ok bool) {
81 i := 0
82 b.neg = false
83 b.trunc = false
84
85
86 if i >= len(s) {
87 return
88 }
89 switch s[i] {
90 case '+':
91 i++
92 case '-':
93 i++
94 b.neg = true
95 }
96
97
98 sawdot := false
99 sawdigits := false
100 for ; i < len(s); i++ {
101 switch {
102 case s[i] == '_':
103
104 continue
105 case s[i] == '.':
106 if sawdot {
107 return
108 }
109 sawdot = true
110 b.dp = b.nd
111 continue
112
113 case '0' <= s[i] && s[i] <= '9':
114 sawdigits = true
115 if s[i] == '0' && b.nd == 0 {
116 b.dp--
117 continue
118 }
119 if b.nd < len(b.d) {
120 b.d[b.nd] = s[i]
121 b.nd++
122 } else if s[i] != '0' {
123 b.trunc = true
124 }
125 continue
126 }
127 break
128 }
129 if !sawdigits {
130 return
131 }
132 if !sawdot {
133 b.dp = b.nd
134 }
135
136
137
138
139
140
141 if i < len(s) && lower(s[i]) == 'e' {
142 i++
143 if i >= len(s) {
144 return
145 }
146 esign := 1
147 switch s[i] {
148 case '+':
149 i++
150 case '-':
151 i++
152 esign = -1
153 }
154 if i >= len(s) || s[i] < '0' || s[i] > '9' {
155 return
156 }
157 e := 0
158 for ; i < len(s) && ('0' <= s[i] && s[i] <= '9' || s[i] == '_'); i++ {
159 if s[i] == '_' {
160
161 continue
162 }
163 if e < 10000 {
164 e = e*10 + int(s[i]) - '0'
165 }
166 }
167 b.dp += e * esign
168 }
169
170 if i != len(s) {
171 return
172 }
173
174 ok = true
175 return
176 }
177
178
179
180
181
182 func readFloat(s string) (mantissa uint64, exp int, neg, trunc, hex bool, i int, ok bool) {
183 underscores := false
184
185
186 if i >= len(s) {
187 return
188 }
189 switch s[i] {
190 case '+':
191 i++
192 case '-':
193 i++
194 neg = true
195 }
196
197
198 base := uint64(10)
199 maxMantDigits := 19
200 expChar := byte('e')
201 if i+2 < len(s) && s[i] == '0' && lower(s[i+1]) == 'x' {
202 base = 16
203 maxMantDigits = 16
204 i += 2
205 expChar = 'p'
206 hex = true
207 }
208 sawdot := false
209 sawdigits := false
210 nd := 0
211 ndMant := 0
212 dp := 0
213 loop:
214 for ; i < len(s); i++ {
215 switch c := s[i]; true {
216 case c == '_':
217 underscores = true
218 continue
219
220 case c == '.':
221 if sawdot {
222 break loop
223 }
224 sawdot = true
225 dp = nd
226 continue
227
228 case '0' <= c && c <= '9':
229 sawdigits = true
230 if c == '0' && nd == 0 {
231 dp--
232 continue
233 }
234 nd++
235 if ndMant < maxMantDigits {
236 mantissa *= base
237 mantissa += uint64(c - '0')
238 ndMant++
239 } else if c != '0' {
240 trunc = true
241 }
242 continue
243
244 case base == 16 && 'a' <= lower(c) && lower(c) <= 'f':
245 sawdigits = true
246 nd++
247 if ndMant < maxMantDigits {
248 mantissa *= 16
249 mantissa += uint64(lower(c) - 'a' + 10)
250 ndMant++
251 } else {
252 trunc = true
253 }
254 continue
255 }
256 break
257 }
258 if !sawdigits {
259 return
260 }
261 if !sawdot {
262 dp = nd
263 }
264
265 if base == 16 {
266 dp *= 4
267 ndMant *= 4
268 }
269
270
271
272
273
274
275 if i < len(s) && lower(s[i]) == expChar {
276 i++
277 if i >= len(s) {
278 return
279 }
280 esign := 1
281 switch s[i] {
282 case '+':
283 i++
284 case '-':
285 i++
286 esign = -1
287 }
288 if i >= len(s) || s[i] < '0' || s[i] > '9' {
289 return
290 }
291 e := 0
292 for ; i < len(s) && ('0' <= s[i] && s[i] <= '9' || s[i] == '_'); i++ {
293 if s[i] == '_' {
294 underscores = true
295 continue
296 }
297 if e < 10000 {
298 e = e*10 + int(s[i]) - '0'
299 }
300 }
301 dp += e * esign
302 } else if base == 16 {
303
304 return
305 }
306
307 if mantissa != 0 {
308 exp = dp - ndMant
309 }
310
311 if underscores && !underscoreOK(s[:i]) {
312 return
313 }
314
315 ok = true
316 return
317 }
318
319
320 var powtab = []int{1, 3, 6, 9, 13, 16, 19, 23, 26}
321
322 func (d *decimal) floatBits(flt *floatInfo) (b uint64, overflow bool) {
323 var exp int
324 var mant uint64
325
326
327 if d.nd == 0 {
328 mant = 0
329 exp = flt.bias
330 goto out
331 }
332
333
334
335
336 if d.dp > 310 {
337 goto overflow
338 }
339 if d.dp < -330 {
340
341 mant = 0
342 exp = flt.bias
343 goto out
344 }
345
346
347 exp = 0
348 for d.dp > 0 {
349 var n int
350 if d.dp >= len(powtab) {
351 n = 27
352 } else {
353 n = powtab[d.dp]
354 }
355 d.Shift(-n)
356 exp += n
357 }
358 for d.dp < 0 || d.dp == 0 && d.d[0] < '5' {
359 var n int
360 if -d.dp >= len(powtab) {
361 n = 27
362 } else {
363 n = powtab[-d.dp]
364 }
365 d.Shift(n)
366 exp -= n
367 }
368
369
370 exp--
371
372
373
374
375 if exp < flt.bias+1 {
376 n := flt.bias + 1 - exp
377 d.Shift(-n)
378 exp += n
379 }
380
381 if exp-flt.bias >= 1<<flt.expbits-1 {
382 goto overflow
383 }
384
385
386 d.Shift(int(1 + flt.mantbits))
387 mant = d.RoundedInteger()
388
389
390 if mant == 2<<flt.mantbits {
391 mant >>= 1
392 exp++
393 if exp-flt.bias >= 1<<flt.expbits-1 {
394 goto overflow
395 }
396 }
397
398
399 if mant&(1<<flt.mantbits) == 0 {
400 exp = flt.bias
401 }
402 goto out
403
404 overflow:
405
406 mant = 0
407 exp = 1<<flt.expbits - 1 + flt.bias
408 overflow = true
409
410 out:
411
412 bits := mant & (uint64(1)<<flt.mantbits - 1)
413 bits |= uint64((exp-flt.bias)&(1<<flt.expbits-1)) << flt.mantbits
414 if d.neg {
415 bits |= 1 << flt.mantbits << flt.expbits
416 }
417 return bits, overflow
418 }
419
420
421 var float64pow10 = []float64{
422 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
423 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
424 1e20, 1e21, 1e22,
425 }
426 var float32pow10 = []float32{1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10}
427
428
429
430
431
432
433
434
435
436
437 func atof64exact(mantissa uint64, exp int, neg bool) (f float64, ok bool) {
438 if mantissa>>float64info.mantbits != 0 {
439 return
440 }
441 f = float64(mantissa)
442 if neg {
443 f = -f
444 }
445 switch {
446 case exp == 0:
447
448 return f, true
449
450
451 case exp > 0 && exp <= 15+22:
452
453
454 if exp > 22 {
455 f *= float64pow10[exp-22]
456 exp = 22
457 }
458 if f > 1e15 || f < -1e15 {
459
460 return
461 }
462 return f * float64pow10[exp], true
463 case exp < 0 && exp >= -22:
464 return f / float64pow10[-exp], true
465 }
466 return
467 }
468
469
470
471 func atof32exact(mantissa uint64, exp int, neg bool) (f float32, ok bool) {
472 if mantissa>>float32MantBits != 0 {
473 return
474 }
475 f = float32(mantissa)
476 if neg {
477 f = -f
478 }
479 switch {
480 case exp == 0:
481 return f, true
482
483
484 case exp > 0 && exp <= 7+10:
485
486
487 if exp > 10 {
488 f *= float32pow10[exp-10]
489 exp = 10
490 }
491 if f > 1e7 || f < -1e7 {
492
493 return
494 }
495 return f * float32pow10[exp], true
496 case exp < 0 && exp >= -10:
497 return f / float32pow10[-exp], true
498 }
499 return
500 }
501
502
503
504
505
506
507 func atofHex(s string, flt *floatInfo, mantissa uint64, exp int, neg, trunc bool) (float64, error) {
508 maxExp := 1<<flt.expbits + flt.bias - 2
509 minExp := flt.bias + 1
510 exp += int(flt.mantbits)
511
512
513
514
515
516
517
518 for mantissa != 0 && mantissa>>(flt.mantbits+2) == 0 {
519 mantissa <<= 1
520 exp--
521 }
522 if trunc {
523 mantissa |= 1
524 }
525 for mantissa>>(1+flt.mantbits+2) != 0 {
526 mantissa = mantissa>>1 | mantissa&1
527 exp++
528 }
529
530
531
532
533 for mantissa > 1 && exp < minExp-2 {
534 mantissa = mantissa>>1 | mantissa&1
535 exp++
536 }
537
538
539 round := mantissa & 3
540 mantissa >>= 2
541 round |= mantissa & 1
542 exp += 2
543 if round == 3 {
544 mantissa++
545 if mantissa == 1<<(1+flt.mantbits) {
546 mantissa >>= 1
547 exp++
548 }
549 }
550
551 if mantissa>>flt.mantbits == 0 {
552 exp = flt.bias
553 }
554 var err error
555 if exp > maxExp {
556 mantissa = 1 << flt.mantbits
557 exp = maxExp + 1
558 err = ErrRange
559 }
560
561 bits := mantissa & (1<<flt.mantbits - 1)
562 bits |= uint64((exp-flt.bias)&(1<<flt.expbits-1)) << flt.mantbits
563 if neg {
564 bits |= 1 << flt.mantbits << flt.expbits
565 }
566 if flt == &float32info {
567 return float64(float32frombits(uint32(bits))), err
568 }
569 return float64frombits(bits), err
570 }
571
572 const fnParseFloat = "ParseFloat"
573
574 func atof32(s string) (f float32, n int, err error) {
575 if val, n, ok := special(s); ok {
576 return float32(val), n, nil
577 }
578
579 d, p, neg, trunc, hex, n, ok := readFloat(s)
580 if !ok {
581 return 0, n, ErrSyntax
582 }
583
584 if hex {
585 f, err := atofHex(s[:n], &float32info, d, p, neg, trunc)
586 return float32(f), n, err
587 }
588
589 if optimize {
590 sign := bool2[uint32](neg) << 31
591 if d == 0 {
592 return float32frombits(sign | 0), n, nil
593 }
594 if p > 40 {
595 return float32frombits(sign | 0xff<<23), n, ErrRange
596 }
597 if p < -70 {
598 return float32frombits(sign | 0), n, nil
599 }
600 if !trunc {
601
602 if f, ok := atof32exact(d, p, neg); ok {
603 return f, n, nil
604 }
605 }
606
607
608 f, err := parseFloat32(d, p, sign)
609 if !trunc {
610 return f, n, err
611 }
612
613
614
615 f1, _ := parseFloat32(d+1, p, sign)
616 if f == f1 {
617 return f, n, err
618 }
619 }
620
621
622 var dec decimal
623 if !dec.set(s[:n]) {
624 return 0, n, ErrSyntax
625 }
626 b, ovf := dec.floatBits(&float32info)
627 f = float32frombits(uint32(b))
628 if ovf {
629 err = ErrRange
630 }
631 return f, n, err
632 }
633
634 func atof64(s string) (f float64, n int, err error) {
635 if val, n, ok := special(s); ok {
636 return val, n, nil
637 }
638
639 d, p, neg, trunc, hex, n, ok := readFloat(s)
640 if !ok {
641 return 0, n, ErrSyntax
642 }
643 if hex {
644 f, err := atofHex(s[:n], &float64info, d, p, neg, trunc)
645 return f, n, err
646 }
647 if optimize {
648 sign := bool2[uint64](neg) << 63
649 if d == 0 {
650 return float64frombits(sign | 0), n, nil
651 }
652 if p > 310 {
653 return float64frombits(sign | 0x7ff<<52), n, ErrRange
654 }
655 if p < -345 {
656 return float64frombits(sign | 0), n, nil
657 }
658 if !trunc {
659
660 if f, ok := atof64exact(d, p, neg); ok {
661 return f, n, nil
662 }
663 }
664
665
666 f, err := parseFloat64(d, p, sign)
667 if !trunc {
668 return f, n, err
669 }
670
671
672
673 f1, _ := parseFloat64(d+1, p, sign)
674 if f == f1 {
675 return f, n, err
676 }
677 }
678
679
680 var dec decimal
681 if !dec.set(s[:n]) {
682 return 0, n, ErrSyntax
683 }
684 b, ovf := dec.floatBits(&float64info)
685 f = float64frombits(b)
686 if ovf {
687 err = ErrRange
688 }
689 return f, n, err
690 }
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719 func ParseFloat(s string, bitSize int) (float64, error) {
720 f, n, err := parseFloatPrefix(s, bitSize)
721 if n != len(s) {
722 return 0, ErrSyntax
723 }
724 return f, err
725 }
726
727 func parseFloatPrefix(s string, bitSize int) (float64, int, error) {
728 if bitSize == 32 {
729 f, n, err := atof32(s)
730 return float64(f), n, err
731 }
732 return atof64(s)
733 }
734
View as plain text