github.com/rafaeltorres324/go/src@v0.0.0-20210519164414-9fdf653a9838/strconv/atof.go (about) 1 // Copyright 2009 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package strconv 6 7 // decimal to binary floating point conversion. 8 // Algorithm: 9 // 1) Store input in multiprecision decimal. 10 // 2) Multiply/divide decimal by powers of two until in range [0.5, 1) 11 // 3) Multiply by 2^precision and round to get mantissa. 12 13 import "math" 14 15 var optimize = true // set to false to force slow-path conversions for testing 16 17 // commonPrefixLenIgnoreCase returns the length of the common 18 // prefix of s and prefix, with the character case of s ignored. 19 // The prefix argument must be all lower-case. 20 func commonPrefixLenIgnoreCase(s, prefix string) int { 21 n := len(prefix) 22 if n > len(s) { 23 n = len(s) 24 } 25 for i := 0; i < n; i++ { 26 c := s[i] 27 if 'A' <= c && c <= 'Z' { 28 c += 'a' - 'A' 29 } 30 if c != prefix[i] { 31 return i 32 } 33 } 34 return n 35 } 36 37 // special returns the floating-point value for the special, 38 // possibly signed floating-point representations inf, infinity, 39 // and NaN. The result is ok if a prefix of s contains one 40 // of these representations and n is the length of that prefix. 41 // The character case is ignored. 42 func special(s string) (f float64, n int, ok bool) { 43 if len(s) == 0 { 44 return 0, 0, false 45 } 46 sign := 1 47 nsign := 0 48 switch s[0] { 49 case '+', '-': 50 if s[0] == '-' { 51 sign = -1 52 } 53 nsign = 1 54 s = s[1:] 55 fallthrough 56 case 'i', 'I': 57 n := commonPrefixLenIgnoreCase(s, "infinity") 58 // Anything longer than "inf" is ok, but if we 59 // don't have "infinity", only consume "inf". 60 if 3 < n && n < 8 { 61 n = 3 62 } 63 if n == 3 || n == 8 { 64 return math.Inf(sign), nsign + n, true 65 } 66 case 'n', 'N': 67 if commonPrefixLenIgnoreCase(s, "nan") == 3 { 68 return math.NaN(), 3, true 69 } 70 } 71 return 0, 0, false 72 } 73 74 func (b *decimal) set(s string) (ok bool) { 75 i := 0 76 b.neg = false 77 b.trunc = false 78 79 // optional sign 80 if i >= len(s) { 81 return 82 } 83 switch { 84 case s[i] == '+': 85 i++ 86 case s[i] == '-': 87 b.neg = true 88 i++ 89 } 90 91 // digits 92 sawdot := false 93 sawdigits := false 94 for ; i < len(s); i++ { 95 switch { 96 case s[i] == '_': 97 // readFloat already checked underscores 98 continue 99 case s[i] == '.': 100 if sawdot { 101 return 102 } 103 sawdot = true 104 b.dp = b.nd 105 continue 106 107 case '0' <= s[i] && s[i] <= '9': 108 sawdigits = true 109 if s[i] == '0' && b.nd == 0 { // ignore leading zeros 110 b.dp-- 111 continue 112 } 113 if b.nd < len(b.d) { 114 b.d[b.nd] = s[i] 115 b.nd++ 116 } else if s[i] != '0' { 117 b.trunc = true 118 } 119 continue 120 } 121 break 122 } 123 if !sawdigits { 124 return 125 } 126 if !sawdot { 127 b.dp = b.nd 128 } 129 130 // optional exponent moves decimal point. 131 // if we read a very large, very long number, 132 // just be sure to move the decimal point by 133 // a lot (say, 100000). it doesn't matter if it's 134 // not the exact number. 135 if i < len(s) && lower(s[i]) == 'e' { 136 i++ 137 if i >= len(s) { 138 return 139 } 140 esign := 1 141 if s[i] == '+' { 142 i++ 143 } else if s[i] == '-' { 144 i++ 145 esign = -1 146 } 147 if i >= len(s) || s[i] < '0' || s[i] > '9' { 148 return 149 } 150 e := 0 151 for ; i < len(s) && ('0' <= s[i] && s[i] <= '9' || s[i] == '_'); i++ { 152 if s[i] == '_' { 153 // readFloat already checked underscores 154 continue 155 } 156 if e < 10000 { 157 e = e*10 + int(s[i]) - '0' 158 } 159 } 160 b.dp += e * esign 161 } 162 163 if i != len(s) { 164 return 165 } 166 167 ok = true 168 return 169 } 170 171 // readFloat reads a decimal or hexadecimal mantissa and exponent from a float 172 // string representation in s; the number may be followed by other characters. 173 // readFloat reports the number of bytes consumed (i), and whether the number 174 // is valid (ok). 175 func readFloat(s string) (mantissa uint64, exp int, neg, trunc, hex bool, i int, ok bool) { 176 underscores := false 177 178 // optional sign 179 if i >= len(s) { 180 return 181 } 182 switch { 183 case s[i] == '+': 184 i++ 185 case s[i] == '-': 186 neg = true 187 i++ 188 } 189 190 // digits 191 base := uint64(10) 192 maxMantDigits := 19 // 10^19 fits in uint64 193 expChar := byte('e') 194 if i+2 < len(s) && s[i] == '0' && lower(s[i+1]) == 'x' { 195 base = 16 196 maxMantDigits = 16 // 16^16 fits in uint64 197 i += 2 198 expChar = 'p' 199 hex = true 200 } 201 sawdot := false 202 sawdigits := false 203 nd := 0 204 ndMant := 0 205 dp := 0 206 loop: 207 for ; i < len(s); i++ { 208 switch c := s[i]; true { 209 case c == '_': 210 underscores = true 211 continue 212 213 case c == '.': 214 if sawdot { 215 break loop 216 } 217 sawdot = true 218 dp = nd 219 continue 220 221 case '0' <= c && c <= '9': 222 sawdigits = true 223 if c == '0' && nd == 0 { // ignore leading zeros 224 dp-- 225 continue 226 } 227 nd++ 228 if ndMant < maxMantDigits { 229 mantissa *= base 230 mantissa += uint64(c - '0') 231 ndMant++ 232 } else if c != '0' { 233 trunc = true 234 } 235 continue 236 237 case base == 16 && 'a' <= lower(c) && lower(c) <= 'f': 238 sawdigits = true 239 nd++ 240 if ndMant < maxMantDigits { 241 mantissa *= 16 242 mantissa += uint64(lower(c) - 'a' + 10) 243 ndMant++ 244 } else { 245 trunc = true 246 } 247 continue 248 } 249 break 250 } 251 if !sawdigits { 252 return 253 } 254 if !sawdot { 255 dp = nd 256 } 257 258 if base == 16 { 259 dp *= 4 260 ndMant *= 4 261 } 262 263 // optional exponent moves decimal point. 264 // if we read a very large, very long number, 265 // just be sure to move the decimal point by 266 // a lot (say, 100000). it doesn't matter if it's 267 // not the exact number. 268 if i < len(s) && lower(s[i]) == expChar { 269 i++ 270 if i >= len(s) { 271 return 272 } 273 esign := 1 274 if s[i] == '+' { 275 i++ 276 } else if s[i] == '-' { 277 i++ 278 esign = -1 279 } 280 if i >= len(s) || s[i] < '0' || s[i] > '9' { 281 return 282 } 283 e := 0 284 for ; i < len(s) && ('0' <= s[i] && s[i] <= '9' || s[i] == '_'); i++ { 285 if s[i] == '_' { 286 underscores = true 287 continue 288 } 289 if e < 10000 { 290 e = e*10 + int(s[i]) - '0' 291 } 292 } 293 dp += e * esign 294 } else if base == 16 { 295 // Must have exponent. 296 return 297 } 298 299 if mantissa != 0 { 300 exp = dp - ndMant 301 } 302 303 if underscores && !underscoreOK(s[:i]) { 304 return 305 } 306 307 ok = true 308 return 309 } 310 311 // decimal power of ten to binary power of two. 312 var powtab = []int{1, 3, 6, 9, 13, 16, 19, 23, 26} 313 314 func (d *decimal) floatBits(flt *floatInfo) (b uint64, overflow bool) { 315 var exp int 316 var mant uint64 317 318 // Zero is always a special case. 319 if d.nd == 0 { 320 mant = 0 321 exp = flt.bias 322 goto out 323 } 324 325 // Obvious overflow/underflow. 326 // These bounds are for 64-bit floats. 327 // Will have to change if we want to support 80-bit floats in the future. 328 if d.dp > 310 { 329 goto overflow 330 } 331 if d.dp < -330 { 332 // zero 333 mant = 0 334 exp = flt.bias 335 goto out 336 } 337 338 // Scale by powers of two until in range [0.5, 1.0) 339 exp = 0 340 for d.dp > 0 { 341 var n int 342 if d.dp >= len(powtab) { 343 n = 27 344 } else { 345 n = powtab[d.dp] 346 } 347 d.Shift(-n) 348 exp += n 349 } 350 for d.dp < 0 || d.dp == 0 && d.d[0] < '5' { 351 var n int 352 if -d.dp >= len(powtab) { 353 n = 27 354 } else { 355 n = powtab[-d.dp] 356 } 357 d.Shift(n) 358 exp -= n 359 } 360 361 // Our range is [0.5,1) but floating point range is [1,2). 362 exp-- 363 364 // Minimum representable exponent is flt.bias+1. 365 // If the exponent is smaller, move it up and 366 // adjust d accordingly. 367 if exp < flt.bias+1 { 368 n := flt.bias + 1 - exp 369 d.Shift(-n) 370 exp += n 371 } 372 373 if exp-flt.bias >= 1<<flt.expbits-1 { 374 goto overflow 375 } 376 377 // Extract 1+flt.mantbits bits. 378 d.Shift(int(1 + flt.mantbits)) 379 mant = d.RoundedInteger() 380 381 // Rounding might have added a bit; shift down. 382 if mant == 2<<flt.mantbits { 383 mant >>= 1 384 exp++ 385 if exp-flt.bias >= 1<<flt.expbits-1 { 386 goto overflow 387 } 388 } 389 390 // Denormalized? 391 if mant&(1<<flt.mantbits) == 0 { 392 exp = flt.bias 393 } 394 goto out 395 396 overflow: 397 // ±Inf 398 mant = 0 399 exp = 1<<flt.expbits - 1 + flt.bias 400 overflow = true 401 402 out: 403 // Assemble bits. 404 bits := mant & (uint64(1)<<flt.mantbits - 1) 405 bits |= uint64((exp-flt.bias)&(1<<flt.expbits-1)) << flt.mantbits 406 if d.neg { 407 bits |= 1 << flt.mantbits << flt.expbits 408 } 409 return bits, overflow 410 } 411 412 // Exact powers of 10. 413 var float64pow10 = []float64{ 414 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 415 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19, 416 1e20, 1e21, 1e22, 417 } 418 var float32pow10 = []float32{1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10} 419 420 // If possible to convert decimal representation to 64-bit float f exactly, 421 // entirely in floating-point math, do so, avoiding the expense of decimalToFloatBits. 422 // Three common cases: 423 // value is exact integer 424 // value is exact integer * exact power of ten 425 // value is exact integer / exact power of ten 426 // These all produce potentially inexact but correctly rounded answers. 427 func atof64exact(mantissa uint64, exp int, neg bool) (f float64, ok bool) { 428 if mantissa>>float64info.mantbits != 0 { 429 return 430 } 431 f = float64(mantissa) 432 if neg { 433 f = -f 434 } 435 switch { 436 case exp == 0: 437 // an integer. 438 return f, true 439 // Exact integers are <= 10^15. 440 // Exact powers of ten are <= 10^22. 441 case exp > 0 && exp <= 15+22: // int * 10^k 442 // If exponent is big but number of digits is not, 443 // can move a few zeros into the integer part. 444 if exp > 22 { 445 f *= float64pow10[exp-22] 446 exp = 22 447 } 448 if f > 1e15 || f < -1e15 { 449 // the exponent was really too large. 450 return 451 } 452 return f * float64pow10[exp], true 453 case exp < 0 && exp >= -22: // int / 10^k 454 return f / float64pow10[-exp], true 455 } 456 return 457 } 458 459 // If possible to compute mantissa*10^exp to 32-bit float f exactly, 460 // entirely in floating-point math, do so, avoiding the machinery above. 461 func atof32exact(mantissa uint64, exp int, neg bool) (f float32, ok bool) { 462 if mantissa>>float32info.mantbits != 0 { 463 return 464 } 465 f = float32(mantissa) 466 if neg { 467 f = -f 468 } 469 switch { 470 case exp == 0: 471 return f, true 472 // Exact integers are <= 10^7. 473 // Exact powers of ten are <= 10^10. 474 case exp > 0 && exp <= 7+10: // int * 10^k 475 // If exponent is big but number of digits is not, 476 // can move a few zeros into the integer part. 477 if exp > 10 { 478 f *= float32pow10[exp-10] 479 exp = 10 480 } 481 if f > 1e7 || f < -1e7 { 482 // the exponent was really too large. 483 return 484 } 485 return f * float32pow10[exp], true 486 case exp < 0 && exp >= -10: // int / 10^k 487 return f / float32pow10[-exp], true 488 } 489 return 490 } 491 492 // atofHex converts the hex floating-point string s 493 // to a rounded float32 or float64 value (depending on flt==&float32info or flt==&float64info) 494 // and returns it as a float64. 495 // The string s has already been parsed into a mantissa, exponent, and sign (neg==true for negative). 496 // If trunc is true, trailing non-zero bits have been omitted from the mantissa. 497 func atofHex(s string, flt *floatInfo, mantissa uint64, exp int, neg, trunc bool) (float64, error) { 498 maxExp := 1<<flt.expbits + flt.bias - 2 499 minExp := flt.bias + 1 500 exp += int(flt.mantbits) // mantissa now implicitly divided by 2^mantbits. 501 502 // Shift mantissa and exponent to bring representation into float range. 503 // Eventually we want a mantissa with a leading 1-bit followed by mantbits other bits. 504 // For rounding, we need two more, where the bottom bit represents 505 // whether that bit or any later bit was non-zero. 506 // (If the mantissa has already lost non-zero bits, trunc is true, 507 // and we OR in a 1 below after shifting left appropriately.) 508 for mantissa != 0 && mantissa>>(flt.mantbits+2) == 0 { 509 mantissa <<= 1 510 exp-- 511 } 512 if trunc { 513 mantissa |= 1 514 } 515 for mantissa>>(1+flt.mantbits+2) != 0 { 516 mantissa = mantissa>>1 | mantissa&1 517 exp++ 518 } 519 520 // If exponent is too negative, 521 // denormalize in hopes of making it representable. 522 // (The -2 is for the rounding bits.) 523 for mantissa > 1 && exp < minExp-2 { 524 mantissa = mantissa>>1 | mantissa&1 525 exp++ 526 } 527 528 // Round using two bottom bits. 529 round := mantissa & 3 530 mantissa >>= 2 531 round |= mantissa & 1 // round to even (round up if mantissa is odd) 532 exp += 2 533 if round == 3 { 534 mantissa++ 535 if mantissa == 1<<(1+flt.mantbits) { 536 mantissa >>= 1 537 exp++ 538 } 539 } 540 541 if mantissa>>flt.mantbits == 0 { // Denormal or zero. 542 exp = flt.bias 543 } 544 var err error 545 if exp > maxExp { // infinity and range error 546 mantissa = 1 << flt.mantbits 547 exp = maxExp + 1 548 err = rangeError(fnParseFloat, s) 549 } 550 551 bits := mantissa & (1<<flt.mantbits - 1) 552 bits |= uint64((exp-flt.bias)&(1<<flt.expbits-1)) << flt.mantbits 553 if neg { 554 bits |= 1 << flt.mantbits << flt.expbits 555 } 556 if flt == &float32info { 557 return float64(math.Float32frombits(uint32(bits))), err 558 } 559 return math.Float64frombits(bits), err 560 } 561 562 const fnParseFloat = "ParseFloat" 563 564 func atof32(s string) (f float32, n int, err error) { 565 if val, n, ok := special(s); ok { 566 return float32(val), n, nil 567 } 568 569 mantissa, exp, neg, trunc, hex, n, ok := readFloat(s) 570 if !ok { 571 return 0, n, syntaxError(fnParseFloat, s) 572 } 573 574 if hex { 575 f, err := atofHex(s[:n], &float32info, mantissa, exp, neg, trunc) 576 return float32(f), n, err 577 } 578 579 if optimize { 580 // Try pure floating-point arithmetic conversion, and if that fails, 581 // the Eisel-Lemire algorithm. 582 if !trunc { 583 if f, ok := atof32exact(mantissa, exp, neg); ok { 584 return f, n, nil 585 } 586 } 587 f, ok := eiselLemire32(mantissa, exp, neg) 588 if ok { 589 if !trunc { 590 return f, n, nil 591 } 592 // Even if the mantissa was truncated, we may 593 // have found the correct result. Confirm by 594 // converting the upper mantissa bound. 595 fUp, ok := eiselLemire32(mantissa+1, exp, neg) 596 if ok && f == fUp { 597 return f, n, nil 598 } 599 } 600 } 601 602 // Slow fallback. 603 var d decimal 604 if !d.set(s[:n]) { 605 return 0, n, syntaxError(fnParseFloat, s) 606 } 607 b, ovf := d.floatBits(&float32info) 608 f = math.Float32frombits(uint32(b)) 609 if ovf { 610 err = rangeError(fnParseFloat, s) 611 } 612 return f, n, err 613 } 614 615 func atof64(s string) (f float64, n int, err error) { 616 if val, n, ok := special(s); ok { 617 return val, n, nil 618 } 619 620 mantissa, exp, neg, trunc, hex, n, ok := readFloat(s) 621 if !ok { 622 return 0, n, syntaxError(fnParseFloat, s) 623 } 624 625 if hex { 626 f, err := atofHex(s[:n], &float64info, mantissa, exp, neg, trunc) 627 return f, n, err 628 } 629 630 if optimize { 631 // Try pure floating-point arithmetic conversion, and if that fails, 632 // the Eisel-Lemire algorithm. 633 if !trunc { 634 if f, ok := atof64exact(mantissa, exp, neg); ok { 635 return f, n, nil 636 } 637 } 638 f, ok := eiselLemire64(mantissa, exp, neg) 639 if ok { 640 if !trunc { 641 return f, n, nil 642 } 643 // Even if the mantissa was truncated, we may 644 // have found the correct result. Confirm by 645 // converting the upper mantissa bound. 646 fUp, ok := eiselLemire64(mantissa+1, exp, neg) 647 if ok && f == fUp { 648 return f, n, nil 649 } 650 } 651 } 652 653 // Slow fallback. 654 var d decimal 655 if !d.set(s[:n]) { 656 return 0, n, syntaxError(fnParseFloat, s) 657 } 658 b, ovf := d.floatBits(&float64info) 659 f = math.Float64frombits(b) 660 if ovf { 661 err = rangeError(fnParseFloat, s) 662 } 663 return f, n, err 664 } 665 666 // ParseFloat converts the string s to a floating-point number 667 // with the precision specified by bitSize: 32 for float32, or 64 for float64. 668 // When bitSize=32, the result still has type float64, but it will be 669 // convertible to float32 without changing its value. 670 // 671 // ParseFloat accepts decimal and hexadecimal floating-point number syntax. 672 // If s is well-formed and near a valid floating-point number, 673 // ParseFloat returns the nearest floating-point number rounded 674 // using IEEE754 unbiased rounding. 675 // (Parsing a hexadecimal floating-point value only rounds when 676 // there are more bits in the hexadecimal representation than 677 // will fit in the mantissa.) 678 // 679 // The errors that ParseFloat returns have concrete type *NumError 680 // and include err.Num = s. 681 // 682 // If s is not syntactically well-formed, ParseFloat returns err.Err = ErrSyntax. 683 // 684 // If s is syntactically well-formed but is more than 1/2 ULP 685 // away from the largest floating point number of the given size, 686 // ParseFloat returns f = ±Inf, err.Err = ErrRange. 687 // 688 // ParseFloat recognizes the strings "NaN", and the (possibly signed) strings "Inf" and "Infinity" 689 // as their respective special floating point values. It ignores case when matching. 690 func ParseFloat(s string, bitSize int) (float64, error) { 691 f, n, err := parseFloatPrefix(s, bitSize) 692 if err == nil && n != len(s) { 693 return 0, syntaxError(fnParseFloat, s) 694 } 695 return f, err 696 } 697 698 func parseFloatPrefix(s string, bitSize int) (float64, int, error) { 699 if bitSize == 32 { 700 f, n, err := atof32(s) 701 return float64(f), n, err 702 } 703 return atof64(s) 704 }