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.
15 // TODO(rsc): Can make d[] a bit smaller and add
17 d
[2000]byte // digits
18 nd
int // number of digits used
19 dp
int // decimal point
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 func newDecimal(i
uint64) *decimal
{
110 // Maximum shift that we can do in one pass without overflow.
111 // Signed int has 31 bits, and we have to be able to accomodate 9<<k.
114 // Binary shift right (* 2) by k bits. k <= maxShift to avoid overflow.
115 func rightShift(a
*decimal
, k
uint) {
116 r
:= 0 // read pointer
117 w
:= 0 // write pointer
119 // Pick up enough leading digits to cover first shift.
121 for ; n
>>k
== 0; r
++ {
124 // a == 0; shouldn't get here, but handle anyway.
139 // Pick up a digit, put down a digit.
140 for ; r
< a
.nd
; r
++ {
144 a
.d
[w
] = byte(dig
+ '0')
149 // Put down extra digits.
153 a
.d
[w
] = byte(dig
+ '0')
162 // Cheat sheet for left shift: table indexed by shift count giving
163 // number of new digits that will be introduced by that shift.
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".
170 // Credit for this trick goes to Ken.
172 type leftCheat
struct {
173 delta
int // number of new digits
174 cutoff
string // minus one digit if original < a.
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.
182 seq 27 | sed 's/^/5^/' | bc |
183 awk 'BEGIN{ print "\tleftCheat{ 0, \"\" }," }
185 log2 = log(2)/log(10)
186 printf("\tleftCheat{ %d, \"%s\" },\t// * %d\n",
187 int(log2*NR+1), $0, 2**NR)
196 {2, "15625"}, // * 64
197 {3, "78125"}, // * 128
198 {3, "390625"}, // * 256
199 {3, "1953125"}, // * 512
200 {4, "9765625"}, // * 1024
201 {4, "48828125"}, // * 2048
202 {4, "244140625"}, // * 4096
203 {4, "1220703125"}, // * 8192
204 {5, "6103515625"}, // * 16384
205 {5, "30517578125"}, // * 32768
206 {5, "152587890625"}, // * 65536
207 {6, "762939453125"}, // * 131072
208 {6, "3814697265625"}, // * 262144
209 {6, "19073486328125"}, // * 524288
210 {7, "95367431640625"}, // * 1048576
211 {7, "476837158203125"}, // * 2097152
212 {7, "2384185791015625"}, // * 4194304
213 {7, "11920928955078125"}, // * 8388608
214 {8, "59604644775390625"}, // * 16777216
215 {8, "298023223876953125"}, // * 33554432
216 {8, "1490116119384765625"}, // * 67108864
217 {9, "7450580596923828125"}, // * 134217728
220 // Is the leading prefix of b lexicographically less than s?
221 func prefixIsLessThan(b
[]byte, s
string) bool {
222 for i
:= 0; i
< len(s
); i
++ {
233 // Binary shift left (/ 2) by k bits. k <= maxShift to avoid overflow.
234 func leftShift(a
*decimal
, k
uint) {
235 delta
:= leftcheats
[k
].delta
236 if prefixIsLessThan(a
.d
[0:a
.nd
], leftcheats
[k
].cutoff
) {
240 r
:= a
.nd
// read index
241 w
:= a
.nd
+ delta
// write index
244 // Pick up a digit, put down a digit.
245 for r
--; r
>= 0; r
-- {
246 n
+= (int(a
.d
[r
]) - '0') << k
250 a
.d
[w
] = byte(rem
+ '0')
254 // Put down extra digits.
259 a
.d
[w
] = byte(rem
+ '0')
268 // Binary shift left (k > 0) or right (k < 0).
269 // Returns receiver for convenience.
270 func (a
*decimal
) Shift(k
int) *decimal
{
273 // nothing to do: a == 0
276 leftShift(a
, maxShift
)
279 leftShift(a
, uint(k
))
282 rightShift(a
, maxShift
)
285 rightShift(a
, uint(-k
))
290 // If we chop a at nd digits, should we round up?
291 func shouldRoundUp(a
*decimal
, nd
int) bool {
292 if nd
< 0 || nd
>= a
.nd
{
295 if a
.d
[nd
] == '5' && nd
+1 == a
.nd
{ // exactly halfway - round to even
296 return nd
> 0 && (a
.d
[nd
-1]-'0')%2
!= 0
298 // not halfway - digit tells all
299 return a
.d
[nd
] >= '5'
302 // Round a to nd digits (or fewer).
303 // Returns receiver for convenience.
304 // If nd is zero, it means we're rounding
305 // just to the left of the digits, as in
307 func (a
*decimal
) Round(nd
int) *decimal
{
308 if nd
< 0 || nd
>= a
.nd
{
311 if shouldRoundUp(a
, nd
) {
314 return a
.RoundDown(nd
)
317 // Round a down to nd digits (or fewer).
318 // Returns receiver for convenience.
319 func (a
*decimal
) RoundDown(nd
int) *decimal
{
320 if nd
< 0 || nd
>= a
.nd
{
328 // Round a up to nd digits (or fewer).
329 // Returns receiver for convenience.
330 func (a
*decimal
) RoundUp(nd
int) *decimal
{
331 if nd
< 0 || nd
>= a
.nd
{
336 for i
:= nd
- 1; i
>= 0; i
-- {
338 if c
< '9' { // can stop after this digit
346 // Change to single 1 with adjusted decimal point.
353 // Extract integer part, rounded appropriately.
354 // No guarantees about overflow.
355 func (a
*decimal
) RoundedInteger() uint64 {
357 return 0xFFFFFFFFFFFFFFFF
361 for i
= 0; i
< a
.dp
&& i
< a
.nd
; i
++ {
362 n
= n
*10 + uint64(a
.d
[i
]-'0')
364 for ; i
< a
.dp
; i
++ {
367 if shouldRoundUp(a
, a
.dp
) {