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.
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.
16 nd
int // number of digits used
17 dp
int // decimal point
19 trunc
bool // discarded nonzero digits beyond d[:nd]
22 func (a
*decimal
) String() string {
31 buf
:= make([]byte, n
)
38 // zeros fill space between decimal point and digits
43 w
+= digitZero(buf
[w
: w
+-a
.dp
])
44 w
+= copy(buf
[w
:], a
.d
[0:a
.nd
])
47 // decimal point in middle of digits
48 w
+= copy(buf
[w
:], a
.d
[0:a
.dp
])
51 w
+= copy(buf
[w
:], a
.d
[a
.dp
:a
.nd
])
54 // zeros fill space between digits and decimal point
55 w
+= copy(buf
[w
:], a
.d
[0:a
.nd
])
56 w
+= digitZero(buf
[w
: w
+a
.dp
-a
.nd
])
58 return string(buf
[0:w
])
61 func digitZero(dst
[]byte) int {
68 // trim trailing zeros from number.
69 // (They are meaningless; the decimal point is tracked
70 // independent of the number of digits.)
71 func trim(a
*decimal
) {
72 for a
.nd
> 0 && a
.d
[a
.nd
-1] == '0' {
81 func (a
*decimal
) Assign(v
uint64) {
84 // Write reversed decimal in buf.
89 buf
[n
] = byte(v
+ '0')
94 // Reverse again to produce forward decimal in a.d.
96 for n
--; n
>= 0; n
-- {
104 // Maximum shift that we can do in one pass without overflow.
105 // Signed int has 31 bits, and we have to be able to accommodate 9<<k.
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
113 // Pick up enough leading digits to cover first shift.
115 for ; n
>>k
== 0; r
++ {
118 // a == 0; shouldn't get here, but handle anyway.
133 // Pick up a digit, put down a digit.
134 for ; r
< a
.nd
; r
++ {
138 a
.d
[w
] = byte(dig
+ '0')
143 // Put down extra digits.
148 a
.d
[w
] = byte(dig
+ '0')
160 // Cheat sheet for left shift: table indexed by shift count giving
161 // number of new digits that will be introduced by that shift.
163 // For example, leftcheats[4] = {2, "625"}. That means that
164 // if we are shifting by 4 (multiplying by 16), it will add 2 digits
165 // when the string prefix is "625" through "999", and one fewer digit
166 // if the string prefix is "000" through "624".
168 // Credit for this trick goes to Ken.
170 type leftCheat
struct {
171 delta
int // number of new digits
172 cutoff
string // minus one digit if original < a.
175 var leftcheats
= []leftCheat
{
176 // Leading digits of 1/2^i = 5^i.
177 // 5^23 is not an exact 64-bit floating point number,
178 // so have to use bc for the math.
180 seq 27 | sed 's/^/5^/' | bc |
181 awk 'BEGIN{ print "\tleftCheat{ 0, \"\" }," }
183 log2 = log(2)/log(10)
184 printf("\tleftCheat{ %d, \"%s\" },\t// * %d\n",
185 int(log2*NR+1), $0, 2**NR)
194 {2, "15625"}, // * 64
195 {3, "78125"}, // * 128
196 {3, "390625"}, // * 256
197 {3, "1953125"}, // * 512
198 {4, "9765625"}, // * 1024
199 {4, "48828125"}, // * 2048
200 {4, "244140625"}, // * 4096
201 {4, "1220703125"}, // * 8192
202 {5, "6103515625"}, // * 16384
203 {5, "30517578125"}, // * 32768
204 {5, "152587890625"}, // * 65536
205 {6, "762939453125"}, // * 131072
206 {6, "3814697265625"}, // * 262144
207 {6, "19073486328125"}, // * 524288
208 {7, "95367431640625"}, // * 1048576
209 {7, "476837158203125"}, // * 2097152
210 {7, "2384185791015625"}, // * 4194304
211 {7, "11920928955078125"}, // * 8388608
212 {8, "59604644775390625"}, // * 16777216
213 {8, "298023223876953125"}, // * 33554432
214 {8, "1490116119384765625"}, // * 67108864
215 {9, "7450580596923828125"}, // * 134217728
218 // Is the leading prefix of b lexicographically less than s?
219 func prefixIsLessThan(b
[]byte, s
string) bool {
220 for i
:= 0; i
< len(s
); i
++ {
231 // Binary shift left (/ 2) by k bits. k <= maxShift to avoid overflow.
232 func leftShift(a
*decimal
, k
uint) {
233 delta
:= leftcheats
[k
].delta
234 if prefixIsLessThan(a
.d
[0:a
.nd
], leftcheats
[k
].cutoff
) {
238 r
:= a
.nd
// read index
239 w
:= a
.nd
+ delta
// write index
242 // Pick up a digit, put down a digit.
243 for r
--; r
>= 0; r
-- {
244 n
+= (int(a
.d
[r
]) - '0') << k
249 a
.d
[w
] = byte(rem
+ '0')
256 // Put down extra digits.
262 a
.d
[w
] = byte(rem
+ '0')
270 if a
.nd
>= len(a
.d
) {
277 // Binary shift left (k > 0) or right (k < 0).
278 func (a
*decimal
) Shift(k
int) {
281 // nothing to do: a == 0
284 leftShift(a
, maxShift
)
287 leftShift(a
, uint(k
))
290 rightShift(a
, maxShift
)
293 rightShift(a
, uint(-k
))
297 // If we chop a at nd digits, should we round up?
298 func shouldRoundUp(a
*decimal
, nd
int) bool {
299 if nd
< 0 || nd
>= a
.nd
{
302 if a
.d
[nd
] == '5' && nd
+1 == a
.nd
{ // exactly halfway - round to even
303 // if we truncated, a little higher than what's recorded - always round up
307 return nd
> 0 && (a
.d
[nd
-1]-'0')%2
!= 0
309 // not halfway - digit tells all
310 return a
.d
[nd
] >= '5'
313 // Round a to nd digits (or fewer).
314 // If nd is zero, it means we're rounding
315 // just to the left of the digits, as in
317 func (a
*decimal
) Round(nd
int) {
318 if nd
< 0 || nd
>= a
.nd
{
321 if shouldRoundUp(a
, nd
) {
328 // Round a down to nd digits (or fewer).
329 func (a
*decimal
) RoundDown(nd
int) {
330 if nd
< 0 || nd
>= a
.nd
{
337 // Round a up to nd digits (or fewer).
338 func (a
*decimal
) RoundUp(nd
int) {
339 if nd
< 0 || nd
>= a
.nd
{
344 for i
:= nd
- 1; i
>= 0; i
-- {
346 if c
< '9' { // can stop after this digit
354 // Change to single 1 with adjusted decimal point.
360 // Extract integer part, rounded appropriately.
361 // No guarantees about overflow.
362 func (a
*decimal
) RoundedInteger() uint64 {
364 return 0xFFFFFFFFFFFFFFFF
368 for i
= 0; i
< a
.dp
&& i
< a
.nd
; i
++ {
369 n
= n
*10 + uint64(a
.d
[i
]-'0')
371 for ; i
< a
.dp
; i
++ {
374 if shouldRoundUp(a
, a
.dp
) {