atof.go 11 KB

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