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