decimal.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. // Multiprecision decimal numbers.
  5. // For floating-point formatting only; not general purpose.
  6. // Only operations are assign and (binary) left/right shift.
  7. // Can do binary floating point in multiprecision decimal precisely
  8. // because 2 divides 10; cannot do decimal floating point
  9. // in multiprecision binary precisely.
  10. package fastprinter
  11. type decimal struct {
  12. d [800]byte // digits, big-endian representation
  13. nd int // number of digits used
  14. dp int // decimal point
  15. neg bool
  16. trunc bool // discarded nonzero digits beyond d[:nd]
  17. }
  18. // trim trailing zeros from number.
  19. // (They are meaningless; the decimal point is tracked
  20. // independent of the number of digits.)
  21. func trim(a *decimal) {
  22. for a.nd > 0 && a.d[a.nd-1] == '0' {
  23. a.nd--
  24. }
  25. if a.nd == 0 {
  26. a.dp = 0
  27. }
  28. }
  29. // Assign v to a.
  30. func (a *decimal) Assign(v uint64) {
  31. var buf [24]byte
  32. // Write reversed decimal in buf.
  33. n := 0
  34. for v > 0 {
  35. v1 := v / 10
  36. v -= 10 * v1
  37. buf[n] = byte(v + '0')
  38. n++
  39. v = v1
  40. }
  41. // Reverse again to produce forward decimal in a.d.
  42. a.nd = 0
  43. for n--; n >= 0; n-- {
  44. a.d[a.nd] = buf[n]
  45. a.nd++
  46. }
  47. a.dp = a.nd
  48. trim(a)
  49. }
  50. // Maximum shift that we can do in one pass without overflow.
  51. // A uint has 32 or 64 bits, and we have to be able to accommodate 9<<k.
  52. const uintSize = 32 << (^uint(0) >> 63)
  53. const maxShift = uintSize - 4
  54. // Binary shift right (/ 2) by k bits. k <= maxShift to avoid overflow.
  55. func rightShift(a *decimal, k uint) {
  56. r := 0 // read pointer
  57. w := 0 // write pointer
  58. // Pick up enough leading digits to cover first shift.
  59. var n uint
  60. for ; n>>k == 0; r++ {
  61. if r >= a.nd {
  62. if n == 0 {
  63. // a == 0; shouldn't get here, but handle anyway.
  64. a.nd = 0
  65. return
  66. }
  67. for n>>k == 0 {
  68. n = n * 10
  69. r++
  70. }
  71. break
  72. }
  73. c := uint(a.d[r])
  74. n = n*10 + c - '0'
  75. }
  76. a.dp -= r - 1
  77. // Pick up a digit, put down a digit.
  78. for ; r < a.nd; r++ {
  79. c := uint(a.d[r])
  80. dig := n >> k
  81. n -= dig << k
  82. a.d[w] = byte(dig + '0')
  83. w++
  84. n = n*10 + c - '0'
  85. }
  86. // Put down extra digits.
  87. for n > 0 {
  88. dig := n >> k
  89. n -= dig << k
  90. if w < len(a.d) {
  91. a.d[w] = byte(dig + '0')
  92. w++
  93. } else if dig > 0 {
  94. a.trunc = true
  95. }
  96. n = n * 10
  97. }
  98. a.nd = w
  99. trim(a)
  100. }
  101. // Cheat sheet for left shift: table indexed by shift count giving
  102. // number of new digits that will be introduced by that shift.
  103. //
  104. // For example, leftcheats[4] = {2, "625"}. That means that
  105. // if we are shifting by 4 (multiplying by 16), it will add 2 digits
  106. // when the string prefix is "625" through "999", and one fewer digit
  107. // if the string prefix is "000" through "624".
  108. //
  109. // Credit for this trick goes to Ken.
  110. type leftCheat struct {
  111. delta int // number of new digits
  112. cutoff string // minus one digit if original < a.
  113. }
  114. var leftcheats = []leftCheat{
  115. // Leading digits of 1/2^i = 5^i.
  116. // 5^23 is not an exact 64-bit floating point number,
  117. // so have to use bc for the math.
  118. // Go up to 60 to be large enough for 32bit and 64bit platforms.
  119. /*
  120. seq 60 | sed 's/^/5^/' | bc |
  121. awk 'BEGIN{ print "\t{ 0, \"\" }," }
  122. {
  123. log2 = log(2)/log(10)
  124. printf("\t{ %d, \"%s\" },\t// * %d\n",
  125. int(log2*NR+1), $0, 2**NR)
  126. }'
  127. */
  128. {0, ""},
  129. {1, "5"}, // * 2
  130. {1, "25"}, // * 4
  131. {1, "125"}, // * 8
  132. {2, "625"}, // * 16
  133. {2, "3125"}, // * 32
  134. {2, "15625"}, // * 64
  135. {3, "78125"}, // * 128
  136. {3, "390625"}, // * 256
  137. {3, "1953125"}, // * 512
  138. {4, "9765625"}, // * 1024
  139. {4, "48828125"}, // * 2048
  140. {4, "244140625"}, // * 4096
  141. {4, "1220703125"}, // * 8192
  142. {5, "6103515625"}, // * 16384
  143. {5, "30517578125"}, // * 32768
  144. {5, "152587890625"}, // * 65536
  145. {6, "762939453125"}, // * 131072
  146. {6, "3814697265625"}, // * 262144
  147. {6, "19073486328125"}, // * 524288
  148. {7, "95367431640625"}, // * 1048576
  149. {7, "476837158203125"}, // * 2097152
  150. {7, "2384185791015625"}, // * 4194304
  151. {7, "11920928955078125"}, // * 8388608
  152. {8, "59604644775390625"}, // * 16777216
  153. {8, "298023223876953125"}, // * 33554432
  154. {8, "1490116119384765625"}, // * 67108864
  155. {9, "7450580596923828125"}, // * 134217728
  156. {9, "37252902984619140625"}, // * 268435456
  157. {9, "186264514923095703125"}, // * 536870912
  158. {10, "931322574615478515625"}, // * 1073741824
  159. {10, "4656612873077392578125"}, // * 2147483648
  160. {10, "23283064365386962890625"}, // * 4294967296
  161. {10, "116415321826934814453125"}, // * 8589934592
  162. {11, "582076609134674072265625"}, // * 17179869184
  163. {11, "2910383045673370361328125"}, // * 34359738368
  164. {11, "14551915228366851806640625"}, // * 68719476736
  165. {12, "72759576141834259033203125"}, // * 137438953472
  166. {12, "363797880709171295166015625"}, // * 274877906944
  167. {12, "1818989403545856475830078125"}, // * 549755813888
  168. {13, "9094947017729282379150390625"}, // * 1099511627776
  169. {13, "45474735088646411895751953125"}, // * 2199023255552
  170. {13, "227373675443232059478759765625"}, // * 4398046511104
  171. {13, "1136868377216160297393798828125"}, // * 8796093022208
  172. {14, "5684341886080801486968994140625"}, // * 17592186044416
  173. {14, "28421709430404007434844970703125"}, // * 35184372088832
  174. {14, "142108547152020037174224853515625"}, // * 70368744177664
  175. {15, "710542735760100185871124267578125"}, // * 140737488355328
  176. {15, "3552713678800500929355621337890625"}, // * 281474976710656
  177. {15, "17763568394002504646778106689453125"}, // * 562949953421312
  178. {16, "88817841970012523233890533447265625"}, // * 1125899906842624
  179. {16, "444089209850062616169452667236328125"}, // * 2251799813685248
  180. {16, "2220446049250313080847263336181640625"}, // * 4503599627370496
  181. {16, "11102230246251565404236316680908203125"}, // * 9007199254740992
  182. {17, "55511151231257827021181583404541015625"}, // * 18014398509481984
  183. {17, "277555756156289135105907917022705078125"}, // * 36028797018963968
  184. {17, "1387778780781445675529539585113525390625"}, // * 72057594037927936
  185. {18, "6938893903907228377647697925567626953125"}, // * 144115188075855872
  186. {18, "34694469519536141888238489627838134765625"}, // * 288230376151711744
  187. {18, "173472347597680709441192448139190673828125"}, // * 576460752303423488
  188. {19, "867361737988403547205962240695953369140625"}, // * 1152921504606846976
  189. }
  190. // Is the leading prefix of b lexicographically less than s?
  191. func prefixIsLessThan(b []byte, s string) bool {
  192. for i := 0; i < len(s); i++ {
  193. if i >= len(b) {
  194. return true
  195. }
  196. if b[i] != s[i] {
  197. return b[i] < s[i]
  198. }
  199. }
  200. return false
  201. }
  202. // Binary shift left (* 2) by k bits. k <= maxShift to avoid overflow.
  203. func leftShift(a *decimal, k uint) {
  204. delta := leftcheats[k].delta
  205. if prefixIsLessThan(a.d[0:a.nd], leftcheats[k].cutoff) {
  206. delta--
  207. }
  208. r := a.nd // read index
  209. w := a.nd + delta // write index
  210. // Pick up a digit, put down a digit.
  211. var n uint
  212. for r--; r >= 0; r-- {
  213. n += (uint(a.d[r]) - '0') << k
  214. quo := n / 10
  215. rem := n - 10*quo
  216. w--
  217. if w < len(a.d) {
  218. a.d[w] = byte(rem + '0')
  219. } else if rem != 0 {
  220. a.trunc = true
  221. }
  222. n = quo
  223. }
  224. // Put down extra digits.
  225. for n > 0 {
  226. quo := n / 10
  227. rem := n - 10*quo
  228. w--
  229. if w < len(a.d) {
  230. a.d[w] = byte(rem + '0')
  231. } else if rem != 0 {
  232. a.trunc = true
  233. }
  234. n = quo
  235. }
  236. a.nd += delta
  237. if a.nd >= len(a.d) {
  238. a.nd = len(a.d)
  239. }
  240. a.dp += delta
  241. trim(a)
  242. }
  243. // Binary shift left (k > 0) or right (k < 0).
  244. func (a *decimal) Shift(k int) {
  245. switch {
  246. case a.nd == 0:
  247. // nothing to do: a == 0
  248. case k > 0:
  249. for k > maxShift {
  250. leftShift(a, maxShift)
  251. k -= maxShift
  252. }
  253. leftShift(a, uint(k))
  254. case k < 0:
  255. for k < -maxShift {
  256. rightShift(a, maxShift)
  257. k += maxShift
  258. }
  259. rightShift(a, uint(-k))
  260. }
  261. }
  262. // If we chop a at nd digits, should we round up?
  263. func shouldRoundUp(a *decimal, nd int) bool {
  264. if nd < 0 || nd >= a.nd {
  265. return false
  266. }
  267. if a.d[nd] == '5' && nd+1 == a.nd {
  268. // exactly halfway - round to even
  269. // if we truncated, a little higher than what's recorded - always round up
  270. if a.trunc {
  271. return true
  272. }
  273. return nd > 0 && (a.d[nd-1]-'0')%2 != 0
  274. }
  275. // not halfway - digit tells all
  276. return a.d[nd] >= '5'
  277. }
  278. // Round a to nd digits (or fewer).
  279. // If nd is zero, it means we're rounding
  280. // just to the left of the digits, as in
  281. // 0.09 -> 0.1.
  282. func (a *decimal) Round(nd int) {
  283. if nd < 0 || nd >= a.nd {
  284. return
  285. }
  286. if shouldRoundUp(a, nd) {
  287. a.RoundUp(nd)
  288. } else {
  289. a.RoundDown(nd)
  290. }
  291. }
  292. // Round a down to nd digits (or fewer).
  293. func (a *decimal) RoundDown(nd int) {
  294. if nd < 0 || nd >= a.nd {
  295. return
  296. }
  297. a.nd = nd
  298. trim(a)
  299. }
  300. // Round a up to nd digits (or fewer).
  301. func (a *decimal) RoundUp(nd int) {
  302. if nd < 0 || nd >= a.nd {
  303. return
  304. }
  305. // round up
  306. for i := nd - 1; i >= 0; i-- {
  307. c := a.d[i]
  308. if c < '9' {
  309. // can stop after this digit
  310. a.d[i]++
  311. a.nd = i + 1
  312. return
  313. }
  314. }
  315. // Number is all 9s.
  316. // Change to single 1 with adjusted decimal point.
  317. a.d[0] = '1'
  318. a.nd = 1
  319. a.dp++
  320. }
  321. // Extract integer part, rounded appropriately.
  322. // No guarantees about overflow.
  323. func (a *decimal) RoundedInteger() uint64 {
  324. if a.dp > 20 {
  325. return 0xFFFFFFFFFFFFFFFF
  326. }
  327. var i int
  328. n := uint64(0)
  329. for i = 0; i < a.dp && i < a.nd; i++ {
  330. n = n*10 + uint64(a.d[i]-'0')
  331. }
  332. for ; i < a.dp; i++ {
  333. n *= 10
  334. }
  335. if shouldRoundUp(a, a.dp) {
  336. n++
  337. }
  338. return n
  339. }