blob: 04caedb6d2eb58e2b0ec8558f1b748a1b627e3ce [file] [log] [blame]
Russ Cox079c00a2008-11-17 12:34:03 -08001// 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
12package strconv
13
Russ Cox8a7cbad2009-01-15 17:22:17 -080014type decimal struct {
Russ Cox079c00a2008-11-17 12:34:03 -080015 // TODO(rsc): Can make d[] a bit smaller and add
16 // truncated bool;
Robert Griesemer314b3582009-11-04 23:20:49 -080017 d [2000]byte; // digits
18 nd int; // number of digits used
19 dp int; // decimal point
20}
Russ Cox079c00a2008-11-17 12:34:03 -080021
Russ Cox8a7cbad2009-01-15 17:22:17 -080022func (a *decimal) String() string {
Russ Cox079c00a2008-11-17 12:34:03 -080023 n := 10 + a.nd;
24 if a.dp > 0 {
Robert Griesemer40621d52009-11-09 12:07:39 -080025 n += a.dp
Russ Cox079c00a2008-11-17 12:34:03 -080026 }
27 if a.dp < 0 {
Robert Griesemer40621d52009-11-09 12:07:39 -080028 n += -a.dp
Russ Cox079c00a2008-11-17 12:34:03 -080029 }
30
Russ Cox55645042009-01-06 15:19:02 -080031 buf := make([]byte, n);
Russ Cox079c00a2008-11-17 12:34:03 -080032 w := 0;
33 switch {
Russ Coxcf9b7f72008-11-19 12:50:34 -080034 case a.nd == 0:
Robert Griesemer40621d52009-11-09 12:07:39 -080035 return "0"
Russ Coxcf9b7f72008-11-19 12:50:34 -080036
Russ Cox079c00a2008-11-17 12:34:03 -080037 case a.dp <= 0:
38 // zeros fill space between decimal point and digits
39 buf[w] = '0';
40 w++;
41 buf[w] = '.';
42 w++;
Robert Griesemer3bb00322009-11-09 21:23:52 -080043 w += digitZero(buf[w : w+-a.dp]);
Rob Pikee70cedf2009-11-18 15:24:24 -080044 w += copy(buf[w:w+a.nd], a.d[0:a.nd]);
Russ Cox079c00a2008-11-17 12:34:03 -080045
46 case a.dp < a.nd:
47 // decimal point in middle of digits
Rob Pikee70cedf2009-11-18 15:24:24 -080048 w += copy(buf[w:w+a.dp], a.d[0:a.dp]);
Russ Cox079c00a2008-11-17 12:34:03 -080049 buf[w] = '.';
50 w++;
Rob Pikee70cedf2009-11-18 15:24:24 -080051 w += copy(buf[w:w+a.nd-a.dp], a.d[a.dp:a.nd]);
Russ Cox079c00a2008-11-17 12:34:03 -080052
53 default:
54 // zeros fill space between digits and decimal point
Rob Pikee70cedf2009-11-18 15:24:24 -080055 w += copy(buf[w:w+a.nd], a.d[0:a.nd]);
Robert Griesemer3bb00322009-11-09 21:23:52 -080056 w += digitZero(buf[w : w+a.dp-a.nd]);
Russ Cox079c00a2008-11-17 12:34:03 -080057 }
58 return string(buf[0:w]);
59}
60
Russ Cox8a7cbad2009-01-15 17:22:17 -080061func copy(dst []byte, src []byte) int {
Russ Cox079c00a2008-11-17 12:34:03 -080062 for i := 0; i < len(dst); i++ {
Robert Griesemer40621d52009-11-09 12:07:39 -080063 dst[i] = src[i]
Russ Cox079c00a2008-11-17 12:34:03 -080064 }
65 return len(dst);
66}
67
Russ Cox8a7cbad2009-01-15 17:22:17 -080068func digitZero(dst []byte) int {
Russ Cox079c00a2008-11-17 12:34:03 -080069 for i := 0; i < len(dst); i++ {
Robert Griesemer40621d52009-11-09 12:07:39 -080070 dst[i] = '0'
Russ Cox079c00a2008-11-17 12:34:03 -080071 }
72 return len(dst);
73}
74
Russ Cox8a7cbad2009-01-15 17:22:17 -080075// trim trailing zeros from number.
Russ Cox079c00a2008-11-17 12:34:03 -080076// (They are meaningless; the decimal point is tracked
77// independent of the number of digits.)
Russ Cox8a7cbad2009-01-15 17:22:17 -080078func trim(a *decimal) {
Robert Griesemer3bb00322009-11-09 21:23:52 -080079 for a.nd > 0 && a.d[a.nd-1] == '0' {
Robert Griesemer40621d52009-11-09 12:07:39 -080080 a.nd--
Russ Cox079c00a2008-11-17 12:34:03 -080081 }
82 if a.nd == 0 {
Robert Griesemer40621d52009-11-09 12:07:39 -080083 a.dp = 0
Russ Cox079c00a2008-11-17 12:34:03 -080084 }
85}
86
87// Assign v to a.
Russ Cox8a7cbad2009-01-15 17:22:17 -080088func (a *decimal) Assign(v uint64) {
Russ Cox079c00a2008-11-17 12:34:03 -080089 var buf [50]byte;
90
91 // Write reversed decimal in buf.
92 n := 0;
93 for v > 0 {
Robert Griesemer3bb00322009-11-09 21:23:52 -080094 v1 := v / 10;
95 v -= 10 * v1;
96 buf[n] = byte(v + '0');
Russ Cox079c00a2008-11-17 12:34:03 -080097 n++;
98 v = v1;
99 }
100
101 // Reverse again to produce forward decimal in a.d.
102 a.nd = 0;
Robert Griesemer314b3582009-11-04 23:20:49 -0800103 for n--; n >= 0; n-- {
Russ Cox079c00a2008-11-17 12:34:03 -0800104 a.d[a.nd] = buf[n];
105 a.nd++;
106 }
107 a.dp = a.nd;
Russ Cox8a7cbad2009-01-15 17:22:17 -0800108 trim(a);
Russ Cox079c00a2008-11-17 12:34:03 -0800109}
110
Russ Cox8a7cbad2009-01-15 17:22:17 -0800111func newDecimal(i uint64) *decimal {
112 a := new(decimal);
Russ Cox079c00a2008-11-17 12:34:03 -0800113 a.Assign(i);
114 return a;
115}
116
117// Maximum shift that we can do in one pass without overflow.
118// Signed int has 31 bits, and we have to be able to accomodate 9<<k.
Russ Cox8a7cbad2009-01-15 17:22:17 -0800119const maxShift = 27
Russ Cox079c00a2008-11-17 12:34:03 -0800120
Russ Cox8a7cbad2009-01-15 17:22:17 -0800121// Binary shift right (* 2) by k bits. k <= maxShift to avoid overflow.
122func rightShift(a *decimal, k uint) {
Russ Cox079c00a2008-11-17 12:34:03 -0800123 r := 0; // read pointer
124 w := 0; // write pointer
125
126 // Pick up enough leading digits to cover first shift.
127 n := 0;
128 for ; n>>k == 0; r++ {
129 if r >= a.nd {
130 if n == 0 {
Russ Coxcf9b7f72008-11-19 12:50:34 -0800131 // a == 0; shouldn't get here, but handle anyway.
Russ Cox079c00a2008-11-17 12:34:03 -0800132 a.nd = 0;
133 return;
134 }
Russ Coxcf9b7f72008-11-19 12:50:34 -0800135 for n>>k == 0 {
Robert Griesemer3bb00322009-11-09 21:23:52 -0800136 n = n * 10;
Russ Cox079c00a2008-11-17 12:34:03 -0800137 r++;
138 }
139 break;
140 }
141 c := int(a.d[r]);
Robert Griesemer314b3582009-11-04 23:20:49 -0800142 n = n*10 + c - '0';
Russ Cox079c00a2008-11-17 12:34:03 -0800143 }
Robert Griesemer3bb00322009-11-09 21:23:52 -0800144 a.dp -= r - 1;
Russ Cox079c00a2008-11-17 12:34:03 -0800145
146 // Pick up a digit, put down a digit.
147 for ; r < a.nd; r++ {
148 c := int(a.d[r]);
Robert Griesemer3bb00322009-11-09 21:23:52 -0800149 dig := n >> k;
150 n -= dig << k;
151 a.d[w] = byte(dig + '0');
Russ Cox079c00a2008-11-17 12:34:03 -0800152 w++;
Robert Griesemer314b3582009-11-04 23:20:49 -0800153 n = n*10 + c - '0';
Russ Cox079c00a2008-11-17 12:34:03 -0800154 }
155
156 // Put down extra digits.
157 for n > 0 {
Robert Griesemer3bb00322009-11-09 21:23:52 -0800158 dig := n >> k;
159 n -= dig << k;
160 a.d[w] = byte(dig + '0');
Russ Cox079c00a2008-11-17 12:34:03 -0800161 w++;
Robert Griesemer3bb00322009-11-09 21:23:52 -0800162 n = n * 10;
Russ Cox079c00a2008-11-17 12:34:03 -0800163 }
164
165 a.nd = w;
Russ Cox8a7cbad2009-01-15 17:22:17 -0800166 trim(a);
Russ Cox079c00a2008-11-17 12:34:03 -0800167}
168
169// Cheat sheet for left shift: table indexed by shift count giving
170// number of new digits that will be introduced by that shift.
171//
Russ Cox8a7cbad2009-01-15 17:22:17 -0800172// For example, leftcheats[4] = {2, "625"}. That means that
Russ Cox079c00a2008-11-17 12:34:03 -0800173// if we are shifting by 4 (multiplying by 16), it will add 2 digits
174// when the string prefix is "625" through "999", and one fewer digit
175// if the string prefix is "000" through "624".
176//
177// Credit for this trick goes to Ken.
178
Russ Cox8a7cbad2009-01-15 17:22:17 -0800179type leftCheat struct {
Robert Griesemer314b3582009-11-04 23:20:49 -0800180 delta int; // number of new digits
181 cutoff string; // minus one digit if original < a.
Russ Cox079c00a2008-11-17 12:34:03 -0800182}
183
Robert Griesemer314b3582009-11-04 23:20:49 -0800184var leftcheats = []leftCheat{
Russ Cox079c00a2008-11-17 12:34:03 -0800185 // Leading digits of 1/2^i = 5^i.
186 // 5^23 is not an exact 64-bit floating point number,
187 // so have to use bc for the math.
188 /*
Robert Griesemer314b3582009-11-04 23:20:49 -0800189 seq 27 | sed 's/^/5^/' | bc |
190 awk 'BEGIN{ print "\tleftCheat{ 0, \"\" }," }
191 {
192 log2 = log(2)/log(10)
193 printf("\tleftCheat{ %d, \"%s\" },\t// * %d\n",
194 int(log2*NR+1), $0, 2**NR)
195 }'
196 */
197 leftCheat{0, ""},
198 leftCheat{1, "5"}, // * 2
199 leftCheat{1, "25"}, // * 4
200 leftCheat{1, "125"}, // * 8
201 leftCheat{2, "625"}, // * 16
202 leftCheat{2, "3125"}, // * 32
203 leftCheat{2, "15625"}, // * 64
204 leftCheat{3, "78125"}, // * 128
205 leftCheat{3, "390625"}, // * 256
206 leftCheat{3, "1953125"}, // * 512
207 leftCheat{4, "9765625"}, // * 1024
208 leftCheat{4, "48828125"}, // * 2048
209 leftCheat{4, "244140625"}, // * 4096
210 leftCheat{4, "1220703125"}, // * 8192
211 leftCheat{5, "6103515625"}, // * 16384
212 leftCheat{5, "30517578125"}, // * 32768
213 leftCheat{5, "152587890625"}, // * 65536
214 leftCheat{6, "762939453125"}, // * 131072
215 leftCheat{6, "3814697265625"}, // * 262144
216 leftCheat{6, "19073486328125"}, // * 524288
217 leftCheat{7, "95367431640625"}, // * 1048576
218 leftCheat{7, "476837158203125"}, // * 2097152
219 leftCheat{7, "2384185791015625"}, // * 4194304
220 leftCheat{7, "11920928955078125"}, // * 8388608
221 leftCheat{8, "59604644775390625"}, // * 16777216
222 leftCheat{8, "298023223876953125"}, // * 33554432
223 leftCheat{8, "1490116119384765625"}, // * 67108864
224 leftCheat{9, "7450580596923828125"}, // * 134217728
Russ Coxbe2edb52009-03-03 08:39:12 -0800225}
Russ Cox079c00a2008-11-17 12:34:03 -0800226
227// Is the leading prefix of b lexicographically less than s?
Russ Cox8a7cbad2009-01-15 17:22:17 -0800228func prefixIsLessThan(b []byte, s string) bool {
Russ Cox079c00a2008-11-17 12:34:03 -0800229 for i := 0; i < len(s); i++ {
230 if i >= len(b) {
Robert Griesemer40621d52009-11-09 12:07:39 -0800231 return true
Russ Cox079c00a2008-11-17 12:34:03 -0800232 }
233 if b[i] != s[i] {
Robert Griesemer40621d52009-11-09 12:07:39 -0800234 return b[i] < s[i]
Russ Cox079c00a2008-11-17 12:34:03 -0800235 }
236 }
237 return false;
238}
239
Russ Cox8a7cbad2009-01-15 17:22:17 -0800240// Binary shift left (/ 2) by k bits. k <= maxShift to avoid overflow.
241func leftShift(a *decimal, k uint) {
242 delta := leftcheats[k].delta;
Robert Griesemer3bb00322009-11-09 21:23:52 -0800243 if prefixIsLessThan(a.d[0:a.nd], leftcheats[k].cutoff) {
Robert Griesemer40621d52009-11-09 12:07:39 -0800244 delta--
Russ Cox079c00a2008-11-17 12:34:03 -0800245 }
246
Robert Griesemer314b3582009-11-04 23:20:49 -0800247 r := a.nd; // read index
Russ Cox079c00a2008-11-17 12:34:03 -0800248 w := a.nd + delta; // write index
249 n := 0;
250
251 // Pick up a digit, put down a digit.
252 for r--; r >= 0; r-- {
Robert Griesemer3bb00322009-11-09 21:23:52 -0800253 n += (int(a.d[r]) - '0') << k;
254 quo := n / 10;
Russ Cox079c00a2008-11-17 12:34:03 -0800255 rem := n - 10*quo;
256 w--;
Robert Griesemer3bb00322009-11-09 21:23:52 -0800257 a.d[w] = byte(rem + '0');
Russ Cox079c00a2008-11-17 12:34:03 -0800258 n = quo;
259 }
260
261 // Put down extra digits.
262 for n > 0 {
Robert Griesemer3bb00322009-11-09 21:23:52 -0800263 quo := n / 10;
Russ Cox079c00a2008-11-17 12:34:03 -0800264 rem := n - 10*quo;
265 w--;
Robert Griesemer3bb00322009-11-09 21:23:52 -0800266 a.d[w] = byte(rem + '0');
Russ Cox079c00a2008-11-17 12:34:03 -0800267 n = quo;
268 }
269
Russ Cox079c00a2008-11-17 12:34:03 -0800270 a.nd += delta;
271 a.dp += delta;
Russ Cox8a7cbad2009-01-15 17:22:17 -0800272 trim(a);
Russ Cox079c00a2008-11-17 12:34:03 -0800273}
274
275// Binary shift left (k > 0) or right (k < 0).
276// Returns receiver for convenience.
Russ Cox8a7cbad2009-01-15 17:22:17 -0800277func (a *decimal) Shift(k int) *decimal {
Russ Cox079c00a2008-11-17 12:34:03 -0800278 switch {
Russ Coxcf9b7f72008-11-19 12:50:34 -0800279 case a.nd == 0:
280 // nothing to do: a == 0
Russ Cox079c00a2008-11-17 12:34:03 -0800281 case k > 0:
Russ Cox8a7cbad2009-01-15 17:22:17 -0800282 for k > maxShift {
283 leftShift(a, maxShift);
284 k -= maxShift;
Russ Cox079c00a2008-11-17 12:34:03 -0800285 }
Russ Cox8a7cbad2009-01-15 17:22:17 -0800286 leftShift(a, uint(k));
Russ Cox079c00a2008-11-17 12:34:03 -0800287 case k < 0:
Russ Cox8a7cbad2009-01-15 17:22:17 -0800288 for k < -maxShift {
289 rightShift(a, maxShift);
290 k += maxShift;
Russ Cox079c00a2008-11-17 12:34:03 -0800291 }
Russ Cox8a7cbad2009-01-15 17:22:17 -0800292 rightShift(a, uint(-k));
Russ Cox079c00a2008-11-17 12:34:03 -0800293 }
294 return a;
295}
296
297// If we chop a at nd digits, should we round up?
Russ Cox8a7cbad2009-01-15 17:22:17 -0800298func shouldRoundUp(a *decimal, nd int) bool {
Russ Cox079c00a2008-11-17 12:34:03 -0800299 if nd <= 0 || nd >= a.nd {
Robert Griesemer40621d52009-11-09 12:07:39 -0800300 return false
Russ Cox079c00a2008-11-17 12:34:03 -0800301 }
302 if a.d[nd] == '5' && nd+1 == a.nd { // exactly halfway - round to even
Robert Griesemer3bb00322009-11-09 21:23:52 -0800303 return (a.d[nd-1]-'0')%2 != 0
Russ Cox079c00a2008-11-17 12:34:03 -0800304 }
305 // not halfway - digit tells all
306 return a.d[nd] >= '5';
307}
308
309// Round a to nd digits (or fewer).
310// Returns receiver for convenience.
Russ Cox8a7cbad2009-01-15 17:22:17 -0800311func (a *decimal) Round(nd int) *decimal {
Russ Cox079c00a2008-11-17 12:34:03 -0800312 if nd <= 0 || nd >= a.nd {
Robert Griesemer40621d52009-11-09 12:07:39 -0800313 return a
Russ Cox079c00a2008-11-17 12:34:03 -0800314 }
Russ Coxa50cbf62009-06-24 20:12:50 -0700315 if shouldRoundUp(a, nd) {
Robert Griesemer40621d52009-11-09 12:07:39 -0800316 return a.RoundUp(nd)
Russ Cox079c00a2008-11-17 12:34:03 -0800317 }
318 return a.RoundDown(nd);
319}
320
321// Round a down to nd digits (or fewer).
322// Returns receiver for convenience.
Russ Cox8a7cbad2009-01-15 17:22:17 -0800323func (a *decimal) RoundDown(nd int) *decimal {
Russ Cox079c00a2008-11-17 12:34:03 -0800324 if nd <= 0 || nd >= a.nd {
Robert Griesemer40621d52009-11-09 12:07:39 -0800325 return a
Russ Cox079c00a2008-11-17 12:34:03 -0800326 }
327 a.nd = nd;
Russ Cox8a7cbad2009-01-15 17:22:17 -0800328 trim(a);
Russ Cox079c00a2008-11-17 12:34:03 -0800329 return a;
330}
331
332// Round a up to nd digits (or fewer).
333// Returns receiver for convenience.
Russ Cox8a7cbad2009-01-15 17:22:17 -0800334func (a *decimal) RoundUp(nd int) *decimal {
Russ Cox079c00a2008-11-17 12:34:03 -0800335 if nd <= 0 || nd >= a.nd {
Robert Griesemer40621d52009-11-09 12:07:39 -0800336 return a
Russ Cox079c00a2008-11-17 12:34:03 -0800337 }
338
339 // round up
Robert Griesemer3bb00322009-11-09 21:23:52 -0800340 for i := nd - 1; i >= 0; i-- {
Russ Cox079c00a2008-11-17 12:34:03 -0800341 c := a.d[i];
Robert Griesemer314b3582009-11-04 23:20:49 -0800342 if c < '9' { // can stop after this digit
Russ Cox079c00a2008-11-17 12:34:03 -0800343 a.d[i]++;
Robert Griesemer3bb00322009-11-09 21:23:52 -0800344 a.nd = i + 1;
Russ Cox079c00a2008-11-17 12:34:03 -0800345 return a;
346 }
347 }
348
349 // Number is all 9s.
350 // Change to single 1 with adjusted decimal point.
351 a.d[0] = '1';
352 a.nd = 1;
353 a.dp++;
354 return a;
355}
356
357// Extract integer part, rounded appropriately.
358// No guarantees about overflow.
Russ Cox8a7cbad2009-01-15 17:22:17 -0800359func (a *decimal) RoundedInteger() uint64 {
Russ Cox079c00a2008-11-17 12:34:03 -0800360 if a.dp > 20 {
Robert Griesemer40621d52009-11-09 12:07:39 -0800361 return 0xFFFFFFFFFFFFFFFF
Russ Cox079c00a2008-11-17 12:34:03 -0800362 }
363 var i int;
364 n := uint64(0);
365 for i = 0; i < a.dp && i < a.nd; i++ {
Robert Griesemer3bb00322009-11-09 21:23:52 -0800366 n = n*10 + uint64(a.d[i]-'0')
Russ Cox079c00a2008-11-17 12:34:03 -0800367 }
368 for ; i < a.dp; i++ {
Robert Griesemer40621d52009-11-09 12:07:39 -0800369 n *= 10
Russ Cox079c00a2008-11-17 12:34:03 -0800370 }
Russ Cox8a7cbad2009-01-15 17:22:17 -0800371 if shouldRoundUp(a, a.dp) {
Robert Griesemer40621d52009-11-09 12:07:39 -0800372 n++
Russ Cox079c00a2008-11-17 12:34:03 -0800373 }
374 return n;
375}