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 5package 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 13import "math" 14import "runtime" 15 16var optimize = true // can change for testing 17 18func equalIgnoreCase(s1, s2 string) bool { 19 if len(s1) != len(s2) { 20 return false 21 } 22 for i := 0; i < len(s1); i++ { 23 c1 := s1[i] 24 if 'A' <= c1 && c1 <= 'Z' { 25 c1 += 'a' - 'A' 26 } 27 c2 := s2[i] 28 if 'A' <= c2 && c2 <= 'Z' { 29 c2 += 'a' - 'A' 30 } 31 if c1 != c2 { 32 return false 33 } 34 } 35 return true 36} 37 38func special(s string) (f float64, ok bool) { 39 if len(s) == 0 { 40 return 41 } 42 switch s[0] { 43 default: 44 return 45 case '+': 46 if equalIgnoreCase(s, "+inf") || equalIgnoreCase(s, "+infinity") { 47 return math.Inf(1), true 48 } 49 case '-': 50 if equalIgnoreCase(s, "-inf") || equalIgnoreCase(s, "-infinity") { 51 return math.Inf(-1), true 52 } 53 case 'n', 'N': 54 if equalIgnoreCase(s, "nan") { 55 return math.NaN(), true 56 } 57 case 'i', 'I': 58 if equalIgnoreCase(s, "inf") || equalIgnoreCase(s, "infinity") { 59 return math.Inf(1), true 60 } 61 } 62 return 63} 64 65func (b *decimal) set(s string) (ok bool) { 66 i := 0 67 b.neg = false 68 b.trunc = false 69 70 // optional sign 71 if i >= len(s) { 72 return 73 } 74 switch { 75 case s[i] == '+': 76 i++ 77 case s[i] == '-': 78 b.neg = true 79 i++ 80 } 81 82 // digits 83 sawdot := false 84 sawdigits := false 85 for ; i < len(s); i++ { 86 switch { 87 case s[i] == '.': 88 if sawdot { 89 return 90 } 91 sawdot = true 92 b.dp = b.nd 93 continue 94 95 case '0' <= s[i] && s[i] <= '9': 96 sawdigits = true 97 if s[i] == '0' && b.nd == 0 { // ignore leading zeros 98 b.dp-- 99 continue 100 } 101 if b.nd < len(b.d) { 102 b.d[b.nd] = s[i] 103 b.nd++ 104 } else if s[i] != '0' { 105 b.trunc = true 106 } 107 continue 108 } 109 break 110 } 111 if !sawdigits { 112 return 113 } 114 if !sawdot { 115 b.dp = b.nd 116 } 117 118 // optional exponent moves decimal point. 119 // if we read a very large, very long number, 120 // just be sure to move the decimal point by 121 // a lot (say, 100000). it doesn't matter if it's 122 // not the exact number. 123 if i < len(s) && (s[i] == 'e' || s[i] == 'E') { 124 i++ 125 if i >= len(s) { 126 return 127 } 128 esign := 1 129 if s[i] == '+' { 130 i++ 131 } else if s[i] == '-' { 132 i++ 133 esign = -1 134 } 135 if i >= len(s) || s[i] < '0' || s[i] > '9' { 136 return 137 } 138 e := 0 139 for ; i < len(s) && '0' <= s[i] && s[i] <= '9'; i++ { 140 if e < 10000 { 141 e = e*10 + int(s[i]) - '0' 142 } 143 } 144 b.dp += e * esign 145 } 146 147 if i != len(s) { 148 return 149 } 150 151 ok = true 152 return 153} 154 155// readFloat reads a decimal mantissa and exponent from a float 156// string representation. It sets ok to false if the number could 157// not fit return types or is invalid. 158func readFloat(s string) (mantissa uint64, exp int, neg, trunc, ok bool) { 159 const uint64digits = 19 160 i := 0 161 162 // optional sign 163 if i >= len(s) { 164 return 165 } 166 switch { 167 case s[i] == '+': 168 i++ 169 case s[i] == '-': 170 neg = true 171 i++ 172 } 173 174 // digits 175 sawdot := false 176 sawdigits := false 177 nd := 0 178 ndMant := 0 179 dp := 0 180 for ; i < len(s); i++ { 181 switch c := s[i]; true { 182 case c == '.': 183 if sawdot { 184 return 185 } 186 sawdot = true 187 dp = nd 188 continue 189 190 case '0' <= c && c <= '9': 191 sawdigits = true 192 if c == '0' && nd == 0 { // ignore leading zeros 193 dp-- 194 continue 195 } 196 nd++ 197 if ndMant < uint64digits { 198 mantissa *= 10 199 mantissa += uint64(c - '0') 200 ndMant++ 201 } else if s[i] != '0' { 202 trunc = true 203 } 204 continue 205 } 206 break 207 } 208 if !sawdigits { 209 return 210 } 211 if !sawdot { 212 dp = nd 213 } 214 215 // optional exponent moves decimal point. 216 // if we read a very large, very long number, 217 // just be sure to move the decimal point by 218 // a lot (say, 100000). it doesn't matter if it's 219 // not the exact number. 220 if i < len(s) && (s[i] == 'e' || s[i] == 'E') { 221 i++ 222 if i >= len(s) { 223 return 224 } 225 esign := 1 226 if s[i] == '+' { 227 i++ 228 } else if s[i] == '-' { 229 i++ 230 esign = -1 231 } 232 if i >= len(s) || s[i] < '0' || s[i] > '9' { 233 return 234 } 235 e := 0 236 for ; i < len(s) && '0' <= s[i] && s[i] <= '9'; i++ { 237 if e < 10000 { 238 e = e*10 + int(s[i]) - '0' 239 } 240 } 241 dp += e * esign 242 } 243 244 if i != len(s) { 245 return 246 } 247 248 exp = dp - ndMant 249 ok = true 250 return 251 252} 253 254// decimal power of ten to binary power of two. 255var powtab = []int{1, 3, 6, 9, 13, 16, 19, 23, 26} 256 257func (d *decimal) floatBits(flt *floatInfo) (b uint64, overflow bool) { 258 var exp int 259 var mant uint64 260 261 // Zero is always a special case. 262 if d.nd == 0 { 263 mant = 0 264 exp = flt.bias 265 goto out 266 } 267 268 // Obvious overflow/underflow. 269 // These bounds are for 64-bit floats. 270 // Will have to change if we want to support 80-bit floats in the future. 271 if d.dp > 310 { 272 goto overflow 273 } 274 if d.dp < -330 { 275 // zero 276 mant = 0 277 exp = flt.bias 278 goto out 279 } 280 281 // Scale by powers of two until in range [0.5, 1.0) 282 exp = 0 283 for d.dp > 0 { 284 var n int 285 if d.dp >= len(powtab) { 286 n = 27 287 } else { 288 n = powtab[d.dp] 289 } 290 d.Shift(-n) 291 exp += n 292 } 293 for d.dp < 0 || d.dp == 0 && d.d[0] < '5' { 294 var n int 295 if -d.dp >= len(powtab) { 296 n = 27 297 } else { 298 n = powtab[-d.dp] 299 } 300 d.Shift(n) 301 exp -= n 302 } 303 304 // Our range is [0.5,1) but floating point range is [1,2). 305 exp-- 306 307 // Minimum representable exponent is flt.bias+1. 308 // If the exponent is smaller, move it up and 309 // adjust d accordingly. 310 if exp < flt.bias+1 { 311 n := flt.bias + 1 - exp 312 d.Shift(-n) 313 exp += n 314 } 315 316 if exp-flt.bias >= 1<<flt.expbits-1 { 317 goto overflow 318 } 319 320 // Extract 1+flt.mantbits bits. 321 d.Shift(int(1 + flt.mantbits)) 322 mant = d.RoundedInteger() 323 324 // Rounding might have added a bit; shift down. 325 if mant == 2<<flt.mantbits { 326 mant >>= 1 327 exp++ 328 if exp-flt.bias >= 1<<flt.expbits-1 { 329 goto overflow 330 } 331 } 332 333 // Denormalized? 334 if mant&(1<<flt.mantbits) == 0 { 335 exp = flt.bias 336 } 337 goto out 338 339overflow: 340 // ±Inf 341 mant = 0 342 exp = 1<<flt.expbits - 1 + flt.bias 343 overflow = true 344 345out: 346 // Assemble bits. 347 bits := mant & (uint64(1)<<flt.mantbits - 1) 348 bits |= uint64((exp-flt.bias)&(1<<flt.expbits-1)) << flt.mantbits 349 if d.neg { 350 bits |= 1 << flt.mantbits << flt.expbits 351 } 352 return bits, overflow 353} 354 355// Exact powers of 10. 356var float64pow10 = []float64{ 357 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 358 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19, 359 1e20, 1e21, 1e22, 360} 361var float32pow10 = []float32{1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10} 362 363// If possible to convert decimal representation to 64-bit float f exactly, 364// entirely in floating-point math, do so, avoiding the expense of decimalToFloatBits. 365// Three common cases: 366// value is exact integer 367// value is exact integer * exact power of ten 368// value is exact integer / exact power of ten 369// These all produce potentially inexact but correctly rounded answers. 370func atof64exact(mantissa uint64, exp int, neg bool) (f float64, ok bool) { 371 if mantissa>>float64info.mantbits != 0 { 372 return 373 } 374 // gccgo gets this wrong on 32-bit i386 when not using -msse. 375 // See TestRoundTrip in atof_test.go for a test case. 376 if runtime.GOARCH == "386" { 377 return 378 } 379 f = float64(mantissa) 380 if neg { 381 f = -f 382 } 383 switch { 384 case exp == 0: 385 // an integer. 386 return f, true 387 // Exact integers are <= 10^15. 388 // Exact powers of ten are <= 10^22. 389 case exp > 0 && exp <= 15+22: // int * 10^k 390 // If exponent is big but number of digits is not, 391 // can move a few zeros into the integer part. 392 if exp > 22 { 393 f *= float64pow10[exp-22] 394 exp = 22 395 } 396 if f > 1e15 || f < -1e15 { 397 // the exponent was really too large. 398 return 399 } 400 return f * float64pow10[exp], true 401 case exp < 0 && exp >= -22: // int / 10^k 402 return f / float64pow10[-exp], true 403 } 404 return 405} 406 407// If possible to compute mantissa*10^exp to 32-bit float f exactly, 408// entirely in floating-point math, do so, avoiding the machinery above. 409func atof32exact(mantissa uint64, exp int, neg bool) (f float32, ok bool) { 410 if mantissa>>float32info.mantbits != 0 { 411 return 412 } 413 f = float32(mantissa) 414 if neg { 415 f = -f 416 } 417 switch { 418 case exp == 0: 419 return f, true 420 // Exact integers are <= 10^7. 421 // Exact powers of ten are <= 10^10. 422 case exp > 0 && exp <= 7+10: // int * 10^k 423 // If exponent is big but number of digits is not, 424 // can move a few zeros into the integer part. 425 if exp > 10 { 426 f *= float32pow10[exp-10] 427 exp = 10 428 } 429 if f > 1e7 || f < -1e7 { 430 // the exponent was really too large. 431 return 432 } 433 return f * float32pow10[exp], true 434 case exp < 0 && exp >= -10: // int / 10^k 435 return f / float32pow10[-exp], true 436 } 437 return 438} 439 440const fnParseFloat = "ParseFloat" 441 442func atof32(s string) (f float32, err error) { 443 if val, ok := special(s); ok { 444 return float32(val), nil 445 } 446 447 if optimize { 448 // Parse mantissa and exponent. 449 mantissa, exp, neg, trunc, ok := readFloat(s) 450 if ok { 451 // Try pure floating-point arithmetic conversion. 452 if !trunc { 453 if f, ok := atof32exact(mantissa, exp, neg); ok { 454 return f, nil 455 } 456 } 457 // Try another fast path. 458 ext := new(extFloat) 459 if ok := ext.AssignDecimal(mantissa, exp, neg, trunc, &float32info); ok { 460 b, ovf := ext.floatBits(&float32info) 461 f = math.Float32frombits(uint32(b)) 462 if ovf { 463 err = rangeError(fnParseFloat, s) 464 } 465 return f, err 466 } 467 } 468 } 469 var d decimal 470 if !d.set(s) { 471 return 0, syntaxError(fnParseFloat, s) 472 } 473 b, ovf := d.floatBits(&float32info) 474 f = math.Float32frombits(uint32(b)) 475 if ovf { 476 err = rangeError(fnParseFloat, s) 477 } 478 return f, err 479} 480 481func atof64(s string) (f float64, err error) { 482 if val, ok := special(s); ok { 483 return val, nil 484 } 485 486 if optimize { 487 // Parse mantissa and exponent. 488 mantissa, exp, neg, trunc, ok := readFloat(s) 489 if ok { 490 // Try pure floating-point arithmetic conversion. 491 if !trunc { 492 if f, ok := atof64exact(mantissa, exp, neg); ok { 493 return f, nil 494 } 495 } 496 // Try another fast path. 497 ext := new(extFloat) 498 if ok := ext.AssignDecimal(mantissa, exp, neg, trunc, &float64info); ok { 499 b, ovf := ext.floatBits(&float64info) 500 f = math.Float64frombits(b) 501 if ovf { 502 err = rangeError(fnParseFloat, s) 503 } 504 return f, err 505 } 506 } 507 } 508 var d decimal 509 if !d.set(s) { 510 return 0, syntaxError(fnParseFloat, s) 511 } 512 b, ovf := d.floatBits(&float64info) 513 f = math.Float64frombits(b) 514 if ovf { 515 err = rangeError(fnParseFloat, s) 516 } 517 return f, err 518} 519 520// ParseFloat converts the string s to a floating-point number 521// with the precision specified by bitSize: 32 for float32, or 64 for float64. 522// When bitSize=32, the result still has type float64, but it will be 523// convertible to float32 without changing its value. 524// 525// If s is well-formed and near a valid floating point number, 526// ParseFloat returns the nearest floating point number rounded 527// using IEEE754 unbiased rounding. 528// 529// The errors that ParseFloat returns have concrete type *NumError 530// and include err.Num = s. 531// 532// If s is not syntactically well-formed, ParseFloat returns err.Err = ErrSyntax. 533// 534// If s is syntactically well-formed but is more than 1/2 ULP 535// away from the largest floating point number of the given size, 536// ParseFloat returns f = ±Inf, err.Err = ErrRange. 537func ParseFloat(s string, bitSize int) (f float64, err error) { 538 if bitSize == 32 { 539 f1, err1 := atof32(s) 540 return float64(f1), err1 541 } 542 f1, err1 := atof64(s) 543 return f1, err1 544} 545