-
Notifications
You must be signed in to change notification settings - Fork 0
/
BigDecimal.as
4873 lines (4471 loc) · 202 KB
/
BigDecimal.as
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package
{
//##header 1189099963000 FOUNDATION
/* Generated from 'BigDecimal.nrx' 8 Sep 2000 11:10:50 [v2.00] */
/* Options: Binary Comments Crossref Format Java Logo Strictargs Strictcase Trace2 Verbose3 */
/* ------------------------------------------------------------------ */
/* BigDecimal -- Decimal arithmetic for Java */
/* ------------------------------------------------------------------ */
/* Copyright IBM Corporation, 1996-2006. All Rights Reserved. */
/* */
/* The BigDecimal class provides immutable arbitrary-precision */
/* floating point (including integer) decimal numbers. */
/* */
/* As the numbers are decimal, there is an exact correspondence */
/* between an instance of a BigDecimal object and its String */
/* representation; the BigDecimal class provides direct conversions */
/* to and from String and character array objects, and well as */
/* conversions to and from the Java primitive types (which may not */
/* be exact). */
/* ------------------------------------------------------------------ */
/* Notes: */
/* */
/* 1. A BigDecimal object is never changed in value once constructed; */
/* this avoids the need for locking. Note in particular that the */
/* mantissa array may be shared between many BigDecimal objects, */
/* so that once exposed it must not be altered. */
/* */
/* 2. This class looks at MathContext class fields directly (for */
/* performance). It must not and does not change them. */
/* */
/* 3. Exponent checking is delayed until finish(), as we know */
/* intermediate calculations cannot cause 31-bit overflow. */
/* [This assertion depends on MAX_DIGITS in MathContext.] */
/* */
/* 4. Comments for the public API now follow the javadoc conventions. */
/* The NetRexx -comments option is used to pass these comments */
/* through to the generated Java code (with -format, if desired). */
/* */
/* 5. System.arraycopy is faster than explicit loop as follows */
/* Mean length 4: equal */
/* Mean length 8: x2 */
/* Mean length 16: x3 */
/* Mean length 24: x4 */
/* From prior experience, we expect mean length a little below 8, */
/* but arraycopy is still the one to use, in general, until later */
/* measurements suggest otherwise. */
/* */
/* 6. 'DMSRCN' referred to below is the original (1981) IBM S/370 */
/* assembler code implementation of the algorithms below; it is */
/* now called IXXRCN and is available with the OS/390 and VM/ESA */
/* operating systems. */
/* ------------------------------------------------------------------ */
/* Change History: */
/* 1997.09.02 Initial version (derived from netrexx.lang classes) */
/* 1997.09.12 Add lostDigits checking */
/* 1997.10.06 Change mantissa to a byte array */
/* 1997.11.22 Rework power [did not prepare arguments, etc.] */
/* 1997.12.13 multiply did not prepare arguments */
/* 1997.12.14 add did not prepare and align arguments correctly */
/* 1998.05.02 0.07 packaging changes suggested by Sun and Oracle */
/* 1998.05.21 adjust remainder operator finalization */
/* 1998.06.04 rework to pass MathContext to finish() and round() */
/* 1998.06.06 change format to use round(); support rounding modes */
/* 1998.06.25 rename to BigDecimal and begin merge */
/* zero can now have trailing zeros (i.e., exp\=0) */
/* 1998.06.28 new methods: movePointXxxx, scale, toBigInteger */
/* unscaledValue, valueof */
/* 1998.07.01 improve byteaddsub to allow array reuse, etc. */
/* 1998.07.01 make null testing explicit to avoid JIT bug [Win32] */
/* 1998.07.07 scaled division [divide(BigDecimal, int, int)] */
/* 1998.07.08 setScale, faster equals */
/* 1998.07.11 allow 1E6 (no sign) <sigh>; new double/float conversion */
/* 1998.10.12 change package to com.ibm.icu.math */
/* 1998.12.14 power operator no longer rounds RHS [to match ANSI] */
/* add toBigDecimal() and BigDecimal(java.math.BigDecimal) */
/* 1998.12.29 improve byteaddsub by using table lookup */
/* 1999.02.04 lostdigits=0 behaviour rounds instead of digits+1 guard */
/* 1999.02.05 cleaner code for BigDecimal(char[]) */
/* 1999.02.06 add javadoc comments */
/* 1999.02.11 format() changed from 7 to 2 method form */
/* 1999.03.05 null pointer checking is no longer explicit */
/* 1999.03.05 simplify; changes from discussion with J. Bloch: */
/* null no longer permitted for MathContext; drop Boolean, */
/* byte, char, float, short constructor, deprecate double */
/* constructor, no blanks in string constructor, add */
/* offset and length version of char[] constructor; */
/* add valueOf(double); drop BooleanValue, charValue; */
/* add ...Exact versions of remaining convertors */
/* 1999.03.13 add toBigIntegerExact */
/* 1999.03.13 1.00 release to IBM Centre for Java Technology */
/* 1999.05.27 1.01 correct 0-0.2 bug under scaled arithmetic */
/* 1999.06.29 1.02 constructors should not allow exponent > 9 digits */
/* 1999.07.03 1.03 lost digits should not be checked if digits=0 */
/* 1999.07.06 lost digits Exception message changed */
/* 1999.07.10 1.04 more work on 0-0.2 (scaled arithmetic) */
/* 1999.07.17 improve messages from pow method */
/* 1999.08.08 performance tweaks */
/* 1999.08.15 fastpath in multiply */
/* 1999.11.05 1.05 fix problem in intValueExact [e.g., 5555555555] */
/* 1999.12.22 1.06 remove multiply fastpath, and improve performance */
/* 2000.01.01 copyright update [Y2K has arrived] */
/* 2000.06.18 1.08 no longer deprecate BigDecimal(double) */
/* ------------------------------------------------------------------ */
/**
* The <code>BigDecimal</code> class implements immutable
* arbitrary-precision decimal numbers. The methods of the
* <code>BigDecimal</code> class provide operations for fixed and
* floating point arithmetic, comparison, format conversions, and
* hashing.
* <p>
* As the numbers are decimal, there is an exact correspondence between
* an instance of a <code>BigDecimal</code> object and its
* <code>String</code> representation; the <code>BigDecimal</code> class
* provides direct conversions to and from <code>String</code> and
* character array (<code>char[]</code>) objects, as well as conversions
* to and from the Java primitive types (which may not be exact) and
* <code>BigInteger</code>.
* <p>
* In the descriptions of constructors and methods in this documentation,
* the value of a <code>BigDecimal</code> number object is shown as the
* result of invoking the <code>toString()</code> method on the object.
* The internal representation of a decimal number is neither defined
* nor exposed, and is not permitted to affect the result of any
* operation.
* <p>
* The floating point arithmetic provided by this class is defined by
* the ANSI X3.274-1996 standard, and is also documented at
* <code>http://www2.hursley.ibm.com/decimal</code>
* <br><i>[This URL will change.]</i>
*
* <h3>Operator methods</h3>
* <p>
* Operations on <code>BigDecimal</code> numbers are controlled by a
* {@link MathContext} object, which provides the context (precision and
* other information) for the operation. Methods that can take a
* <code>MathContext</code> parameter implement the standard arithmetic
* operators for <code>BigDecimal</code> objects and are known as
* <i>operator methods</i>. The default settings provided by the
* constant {@link MathContext#DEFAULT} (<code>digits=9,
* form=SCIENTIFIC, lostDigits=false, roundingMode=ROUND_HALF_UP</code>)
* perform general-purpose floating point arithmetic to nine digits of
* precision. The <code>MathContext</code> parameter must not be
* <code>null</code>.
* <p>
* Each operator method also has a version provided which does
* not take a <code>MathContext</code> parameter. For this version of
* each method, the context settings used are <code>digits=0,
* form=PLAIN, lostDigits=false, roundingMode=ROUND_HALF_UP</code>;
* these settings perform fixed point arithmetic with unlimited
* precision, as defined for the original BigDecimal class in Java 1.1
* and Java 1.2.
* <p>
* For monadic operators, only the optional <code>MathContext</code>
* parameter is present; the operation acts upon the current object.
* <p>
* For dyadic operators, a <code>BigDecimal</code> parameter is always
* present; it must not be <code>null</code>.
* The operation acts with the current object being the left-hand operand
* and the <code>BigDecimal</code> parameter being the right-hand operand.
* <p>
* For example, adding two <code>BigDecimal</code> objects referred to
* by the names <code>award</code> and <code>extra</code> could be
* written as any of:
* <p><code>
* award.add(extra)
* <br>award.add(extra, MathContext.DEFAULT)
* <br>award.add(extra, acontext)
* </code>
* <p>
* (where <code>acontext</code> is a <code>MathContext</code> object),
* which would return a <code>BigDecimal</code> object whose value is
* the result of adding <code>award</code> and <code>extra</code> under
* the appropriate context settings.
* <p>
* When a <code>BigDecimal</code> operator method is used, a set of
* rules define what the result will be (and, by implication, how the
* result would be represented as a character string).
* These rules are defined in the BigDecimal arithmetic documentation
* (see the URL above), but in summary:
* <ul>
* <li>Results are normally calculated with up to some maximum number of
* significant digits.
* For example, if the <code>MathContext</code> parameter for an operation
* were <code>MathContext.DEFAULT</code> then the result would be
* rounded to 9 digits; the division of 2 by 3 would then result in
* 0.666666667.
* <br>
* You can change the default of 9 significant digits by providing the
* method with a suitable <code>MathContext</code> object. This lets you
* calculate using as many digits as you need -- thousands, if necessary.
* Fixed point (scaled) arithmetic is indicated by using a
* <code>digits</code> setting of 0 (or omitting the
* <code>MathContext</code> parameter).
* <br>
* Similarly, you can change the algorithm used for rounding from the
* default "classic" algorithm.
* <li>
* In standard arithmetic (that is, when the <code>form</code> setting
* is not <code>PLAIN</code>), a zero result is always expressed as the
* single digit <code>'0'</code> (that is, with no sign, decimal point,
* or exponent part).
* <li>
* Except for the division and power operators in standard arithmetic,
* trailing zeros are preserved (this is in contrast to binary floating
* point operations and most electronic calculators, which lose the
* information about trailing zeros in the fractional part of results).
* <br>
* So, for example:
* <p><code>
* new BigDecimal("2.40").add( new BigDecimal("2")) => "4.40"
* <br>new BigDecimal("2.40").subtract(new BigDecimal("2")) => "0.40"
* <br>new BigDecimal("2.40").multiply(new BigDecimal("2")) => "4.80"
* <br>new BigDecimal("2.40").divide( new BigDecimal("2"), def) => "1.2"
* </code>
* <p>where the value on the right of the <code>=></code> would be the
* result of the operation, expressed as a <code>String</code>, and
* <code>def</code> (in this and following examples) refers to
* <code>MathContext.DEFAULT</code>).
* This preservation of trailing zeros is desirable for most
* calculations (including financial calculations).
* If necessary, trailing zeros may be easily removed using division by 1.
* <li>
* In standard arithmetic, exponential form is used for a result
* depending on its value and the current setting of <code>digits</code>
* (the default is 9 digits).
* If the number of places needed before the decimal point exceeds the
* <code>digits</code> setting, or the absolute value of the number is
* less than <code>0.000001</code>, then the number will be expressed in
* exponential notation; thus
* <p><code>
* new BigDecimal("1e+6").multiply(new BigDecimal("1e+6"), def)
* </code>
* <p>results in <code>1E+12</code> instead of
* <code>1000000000000</code>, and
* <p><code>
* new BigDecimal("1").divide(new BigDecimal("3E+10"), def)
* </code>
* <p>results in <code>3.33333333E-11</code> instead of
* <code>0.0000000000333333333</code>.
* <p>
* The form of the exponential notation (scientific or engineering) is
* determined by the <code>form</code> setting.
* <eul>
* <p>
* The names of methods in this class follow the conventions established
* by <code>java.lang.Number</code>, <code>java.math.BigInteger</code>,
* and <code>java.math.BigDecimal</code> in Java 1.1 and Java 1.2.
*
* @see MathContext
* @author Mike Cowlishaw
* @stable ICU 2.0
*/
/** ActionScript 3 conversion (c) 2009
* Jean-Francois Larouche, Canada
*
* To know what have been changed, just search for
* ActionScript in this file.
*
* Constructors:
*
* new BigDecimal() : Default BigDecimal to 0
* new BigDecimal(String) : String must be a decimal representation.
* new BigDecimal(int)
* new BigDecimal(Number)
*
* This class is Immutable exactly like the Java version
*
* To convert the value back:
* decimal.numberValue();
* decimal.toString()
*
**/
public class BigDecimal {
// ActionScript 3 Port
// Utility functions that does not exist in AS3
// This one may seem weird, but its incredibly important.
// because when we divide 2 ints in AS3 to put it in an array
// it will be a float. so lets use this to make sure its 100% perfect.
private static function div(a:int, b:int):int {
return (a-(a%b))/b as int;
}
// ActionScript 3 Port
// Utility functions that does not exist in AS3
private static function arraycopy(src:Array, srcindex:int, dest:Array, destindex:int, length:int):void {
var i:int;
if (destindex > srcindex) {
// in case src and dest are equals, but also doesn't hurt
// if they are different
for (i = length-1; i >= 0; --i) {
dest[i+destindex] = src[i+srcindex];
}
} else {
for (i = 0; i < length; ++i) {
dest[i+destindex] = src[i+srcindex];
}
}
}
// ActionScript 3 Port
// Utility functions that does not exist in AS3
private static function createArrayWithZeros(length:int):Array {
var retVal:Array = new Array(length);
var i:int;
for (i = 0; i < length; ++i) {
retVal[i] = 0;
}
return retVal;
}
// ActionScript 3 Port
// Utility functions that does not exist in AS3
private static function isDigit(string:String):Boolean {
return string.charCodeAt(0) >= BigDecimal.VALUE_ZERO &&
string.charCodeAt(0) <= BigDecimal.VALUE_NINE;
}
// ActionScript 3 Port
// Utility functions that does not exist in AS3
private static function isDigitInt(value:int):Boolean {
return value >= BigDecimal.VALUE_ZERO &&
value <= BigDecimal.VALUE_NINE;
}
// ActionScript 3 Port
// Utility functions that does not exist in AS3
// This was in the BigDecimal version of ibm Utility.getChars...
// Not sure if we will use it.
/* <sgml> Checks a BigDecimal argument to ensure it's a true integer
in a given range.
<p>If OK, returns it as an int. </sgml> */
// [currently only used by pow]
public static function getChars(src:String, srcBegin:int, srcEnd:int, dst:Array, dstBegin:int):void {
if (srcBegin == srcEnd) {
return;
}
for(srcBegin; srcBegin < srcEnd; ++srcBegin) {
dst[dstBegin++] = src.charAt(srcBegin);
}
}
// ActionScript 3 Port
// Used to Mutate this in a particular scenario
private function assignMyself(other:BigDecimal):void {
this.ind = other.ind;
this.form = other.form;
this.exp = other.exp;
this.mant = other.mant;
}
/* ----- Constants ----- */
/* properties constant public */ // useful to others
/**
* The <code>BigDecimal</code> constant "0".
*
* @see #ONE
* @see #TEN
* @stable ICU 2.0
*/
public static const ZERO:BigDecimal = BigDecimal.createStatic(0); // use long as we want the int constructor
// .. to be able to use this, for speed
/**
* The <code>BigDecimal</code> constant "1".
*
* @see #TEN
* @see #ZERO
* @stable ICU 2.0
*/
public static const ONE:BigDecimal = BigDecimal.createStatic(1); // use long as we want the int constructor
// .. to be able to use this, for speed
/**
* The <code>BigDecimal</code> constant "10".
*
* @see #ONE
* @see #ZERO
* @stable ICU 2.0
*/
public static const TEN:BigDecimal = BigDecimal.createStatic(10);
/* properties constant private */ // locals
private static const ispos:int = 1; // ind: indicates positive (must be 1)
private static const iszero:int = 0; // ind: indicates zero (must be 0)
private static const isneg:int = -1; // ind: indicates negative (must be -1)
// [later could add NaN, +/- infinity, here]
private static const MinExp:int = -999999999; // minimum exponent allowed
private static const MaxExp:int = 999999999; // maximum exponent allowed
private static const MinArg:int = -999999999; // minimum argument integer
private static const MaxArg:int = 999999999; // maximum argument integer
// ActionScript 3 Port
// In AS3 there is no char, byte or native arrays
// So lets create the int value of each char we need in the
// algorythms
private static const VALUE_ZERO:int = (new String("0")).charCodeAt(0); // use long as we want the int constructor
private static const VALUE_NINE:int = (new String("9")).charCodeAt(0); // use long as we want the int constructor
private static const VALUE_EUPPER:int = (new String("e")).charCodeAt(0); // use long as we want the int constructor
private static const VALUE_ELOWER:int = (new String("E")).charCodeAt(0); // use long as we want the int constructor
private static const VALUE_DOT:int = (new String(".")).charCodeAt(0); // use long as we want the int constructor
/* properties static private */
// Precalculated constant arrays (used by byteaddsub)
private static const bytecar:Array = new Array((90+99)+1); // carry/borrow array
private static const bytedig:Array = diginit(); // next digit array
/* ----- Instance properties [all private and immutable] ----- */
/* properties private */
/**
* The indicator. This may take the values:
* <ul>
* <li>ispos -- the number is positive
* <li>iszero -- the number is zero
* <li>isneg -- the number is negative
* </ul>
*
* @serial
*/
private var ind:int; // assumed undefined
// Note: some code below assumes IND = Sign [-1, 0, 1], at present.
// We only need two bits for this, but use a byte [also permits
// smooth future extension].
/**
* The formatting style. This may take the values:
* <ul>
* <li>MathContext.PLAIN -- no exponent needed
* <li>MathContext.SCIENTIFIC -- scientific notation required
* <li>MathContext.ENGINEERING -- engineering notation required
* </ul>
* <p>
* This property is an optimization; it allows us to defer number
* layout until it is actually needed as a string, hence avoiding
* unnecessary formatting.
*
* @serial
*/
private var form:int = MathContext.NOTATION_PLAIN; // assumed PLAIN
// We only need two bits for this, at present, but use a byte
// [again, to allow for smooth future extension]
/**
* The value of the mantissa.
* <p>
* Once constructed, this may become shared between several BigDecimal
* objects, so must not be altered.
* <p>
* For efficiency (speed), this is a byte array, with each byte
* taking a value of 0 -> 9.
* <p>
* If the first byte is 0 then the value of the number is zero (and
* mant.length=1, except when constructed from a plain number, for
* example, 0.000).
*
* @serial
*/
private var mant:Array; // assumed null
/**
* The exponent.
* <p>
* For fixed point arithmetic, scale is <code>-exp</code>, and can
* apply to zero.
*
* Note that this property can have a value less than MinExp when
* the mantissa has more than one digit.
*
* @serial
*/
private var exp:int;
// assumed 0
/* ---------------------------------------------------------------- */
/* Constructors */
/* ---------------------------------------------------------------- */
//#ifndef FOUNDATION
//## /**
//## * Constructs a <code>BigDecimal</code> object from a
//## * <code>java.math.BigDecimal</code>.
//## * <p>
//## * Constructs a <code>BigDecimal</code> as though the parameter had
//## * been represented as a <code>String</code> (using its
//## * <code>toString</code> method) and the
//## * {@link #BigDecimal(java.lang.String)} constructor had then been
//## * used.
//## * The parameter must not be <code>null</code>.
//## * <p>
//## * <i>(Note: this constructor is provided only in the
//## * <code>com.ibm.icu.math</code> version of the BigDecimal class.
//## * It would not be present in a <code>java.math</code> version.)</i>
//## *
//## * @param bd The <code>BigDecimal</code> to be translated.
//## * @stable ICU 2.0
//## */
//##
//## public BigDecimal(java.math.BigDecimal bd){
//## this(bd.toString());
//## return;}
//#endif
/**
* Constructs a <code>BigDecimal</code> object from a
* <code>BigInteger</code>, with scale 0.
* <p>
* Constructs a <code>BigDecimal</code> which is the exact decimal
* representation of the <code>BigInteger</code>, with a scale of
* zero.
* The value of the <code>BigDecimal</code> is identical to the value
* of the <code>BigInteger</code>.
* The parameter must not be <code>null</code>.
* <p>
* The <code>BigDecimal</code> will contain only decimal digits,
* prefixed with a leading minus sign (hyphen) if the
* <code>BigInteger</code> is negative. A leading zero will be
* present only if the <code>BigInteger</code> is zero.
*
* @param bi The <code>BigInteger</code> to be converted.
* @stable ICU 2.0
*/
// ActionScript 3 : Wont Port
/*
public BigDecimal(java.math.BigInteger JavaDoc bi){
this(bi.toString(10));
return;}
*/
// exp remains 0
/**
* Constructs a <code>BigDecimal</code> object from a
* <code>BigInteger</code> and a scale.
* <p>
* Constructs a <code>BigDecimal</code> which is the exact decimal
* representation of the <code>BigInteger</code>, scaled by the
* second parameter, which may not be negative.
* The value of the <code>BigDecimal</code> is the
* <code>BigInteger</code> divided by ten to the power of the scale.
* The <code>BigInteger</code> parameter must not be
* <code>null</code>.
* <p>
* The <code>BigDecimal</code> will contain only decimal digits, (with
* an embedded decimal point followed by <code>scale</code> decimal
* digits if the scale is positive), prefixed with a leading minus
* sign (hyphen) if the <code>BigInteger</code> is negative. A
* leading zero will be present only if the <code>BigInteger</code> is
* zero.
*
* @param bi The <code>BigInteger</code> to be converted.
* @param scale The <code>int</code> specifying the scale.
* @throws NumberFormatException if the scale is negative.
* @stable ICU 2.0
*/
// ActionScript 3 : Wont Port
/*
public BigDecimal(java.math.BigInteger JavaDoc bi,int scale){
this(bi.toString(10));
if (scale<0)
throw new java.lang.NumberFormatException JavaDoc("Negative scale:"+" "+scale);
exp=(int)-scale; // exponent is -scale
return;}
*/
/**
* Constructs a <code>BigDecimal</code> object from an array of characters.
* <p>
* Constructs a <code>BigDecimal</code> as though a
* <code>String</code> had been constructed from the character array
* and the {@link #BigDecimal(java.lang.String)} constructor had then
* been used. The parameter must not be <code>null</code>.
* <p>
* Using this constructor is faster than using the
* <code>BigDecimal(String)</code> constructor if the string is
* already available in character array form.
*
* @param inchars The <code>char[]</code> array containing the number
* to be converted.
* @throws NumberFormatException if the parameter is not a valid
* number.
* @stable ICU 2.0
*/
// ActionScript 3 : Wont Port
/*
public function BigDecimal(inchars:char[]) {
this(inchars,0,inchars.length);
}
*/
/**
* Constructs a <code>BigDecimal</code> object from an array of characters.
* <p>
* Constructs a <code>BigDecimal</code> as though a
* <code>String</code> had been constructed from the character array
* (or a subarray of that array) and the
* {@link #BigDecimal(java.lang.String)} constructor had then been
* used. The first parameter must not be <code>null</code>, and the
* subarray must be wholly contained within it.
* <p>
* Using this constructor is faster than using the
* <code>BigDecimal(String)</code> constructor if the string is
* already available within a character array.
*
* @param inchars The <code>char[]</code> array containing the number
* to be converted.
* @param offset The <code>int</code> offset into the array of the
* start of the number to be converted.
* @param length The <code>int</code> length of the number.
* @throws NumberFormatException if the parameter is not a valid
* number for any reason.
* @stable ICU 2.0
*/
public function BigDecimal(inobject:Object = 0, offset:int = 0, length:int = -1) {
var exotic:Boolean;
var hadexp:Boolean;
var d:int;
var dotoff:int;
var last:int;
var i:int = 0;
var si:int = 0;
var eneg:Boolean = false;
var k:int = 0;
var elen:int = 0;
var j:int = 0;
var sj:int = 0;
var dvalue:int = 0;
var mag:int = 0;
var inchars:String = null;
//ActionScript 3 to patch the scale
var createdFromNumber:Boolean = false;
//ActionScript 3 :
//This is only for the createStatic
//People should never pass null to the constructor
if(inobject == null) {
return;
}
//Path the multiple possibilities of constructing
if(inobject is int) {
createFromInt(inobject as int);
return;
} else if(inobject is Number) {
inchars = (inobject as Number).toString();
createdFromNumber = true;
} else if(!(inobject is String)) {
badarg("bad parameter", 0, inchars);
} else {
inchars = inobject as String;
}
//Default parameter correction
if(length == -1) {
length = inchars.length;
}
// This is the primary constructor; all incoming strings end up
// here; it uses explicit (inline) parsing for speed and to avoid
// generating intermediate (temporary) objects of any kind.
// 1998.06.25: exponent form built only if E/e in string
// 1998.06.25: trailing zeros not removed for zero
// 1999.03.06: no embedded blanks; allow offset and length
if (length<=0) {
bad(inchars); // bad conversion (empty string)
// [bad offset will raise array bounds exception]
}
/* Handle and step past sign */
ind = ispos; // assume positive
if (inchars.charAt(offset)==("-")) {
length--;
if (length==0) {
bad(inchars); // nothing after sign
}
ind=isneg;
offset++;
} else if (inchars.charAt(offset)==("+")) {
length--;
if (length==0) {
bad(inchars); // nothing after sign
}
offset++;
}
/* We're at the start of the number */
exotic = false; // have extra digits
hadexp = false; // had explicit exponent
d = 0; // count of digits found
dotoff = -1; // offset where dot was found
last = -1; // last character of mantissa
{
var $1:int = length;
i = offset;
_i:for(; $1 > 0; $1--,i++) {
si=inchars.charCodeAt(i);
if (si>=BigDecimal.VALUE_ZERO) {// test for Arabic digit
if (si<=BigDecimal.VALUE_NINE) {
last=i;
d++; // still in mantissa
continue;
}
}
if (si==BigDecimal.VALUE_DOT) { // record and ignore
if (dotoff>=0) {
bad(inchars); // two dots
}
dotoff=i-offset; // offset into mantissa
continue _i;
}
if (si!=BigDecimal.VALUE_ELOWER) {
if (si!=BigDecimal.VALUE_EUPPER) { // expect an extra digit
if ((!(isDigitInt(si)))) {
bad(inchars); // not a number
}
// defer the base 10 check until later to avoid extra method call
exotic = true; // will need conversion later
last = i;
d++; // still in mantissa
continue _i;
}
}
/* Found 'e' or 'E' -- now process explicit exponent */
// 1998.07.11: sign no longer required
if ((i-offset)>(length-2)) {
bad(inchars); // no room for even one digit
}
eneg = false;
if ((inchars.charAt(i+1))==("-")) {
eneg=true;
k=i+2;
} else if ((inchars.charAt(i+1))==("+")) {
k=i+2;
} else {
k=i+1;
}
// k is offset of first expected digit
elen = length - ((k - offset)); // possible number of digits
if ((elen==0)||(elen>9)) {
bad(inchars); // 0 or more than 9 digits
}
{
var $2:int = elen;
j = k;
_j:for(; $2 > 0; $2--,j++) {
sj=inchars.charCodeAt(j);
if (sj<BigDecimal.VALUE_ZERO) {
bad(inchars); // always bad
}
if (sj>BigDecimal.VALUE_NINE) { // maybe an exotic digit
// ActionScript 3 PORT
// Lets forget exotics for now... i dont have time.
//if ((!(isDigit(sj)))) {
// bad(inchars); // not a number
//}
//dvalue=java.lang.Character.digit(sj,10); // check base
//if (dvalue<0) {
bad(inchars); // not base 10
//}
} else {
dvalue=((sj))-((BigDecimal.VALUE_ZERO));
}
exp=(exp*10)+dvalue;
}
}/*j*/
if (eneg) {
exp = -exp; // was negative
}
hadexp=true; // remember we had one
break _i; // we are done
}
}/*i*/
/* Here when all inspected */
if (d==0) {
bad(inchars); // no mantissa digits
}
if (dotoff>=0) {
exp=(exp+dotoff)-d; // adjust exponent if had dot
}
/* strip leading zeros/dot (leave final if all 0's) */
{
var $3:int = last-1;
i = offset;
_i2:for(; i <= $3; i++) {
si=inchars.charCodeAt(i);
if (si==BigDecimal.VALUE_ZERO) {
offset++;
dotoff--;
d--;
} else if (si==BigDecimal.VALUE_DOT) {
offset++; // step past dot
dotoff--;
} else if (si<=BigDecimal.VALUE_NINE) {
break _i2;/* non-0 */
} else {/* exotic */
// ActionScript 3 PORT
// Lets forget exotics for now... i dont have time.
//if ((java.lang.Character.digit(si,10))!=0) {
break _i2; // non-0 or bad
//}
// is 0 .. strip like '0'
//offset++;
//dotoff--;
//d--;
}
}
}/*i*/
/* Create the mantissa array */
mant = new Array(d); // we know the length
j = offset; // input offset
if (exotic) {
exotica:do { // slow: check for exotica
{
var $4:int = d;
i = 0;
_i3:for(; $4 > 0; $4--, i++) {
if (i==dotoff) {
j++; // at dot
}
sj=inchars[j];
if (sj<=BigDecimal.VALUE_NINE) {
mant[i]=(sj-VALUE_ZERO);/* easy */
} else {
// ActionScript 3 PORT
// Lets forget exotics for now... i dont have time.
//dvalue=java.lang.Character.digit(sj,10);
//if (dvalue<0) {
bad(inchars); // not a number after all
//}
//mant[i]=(byte)dvalue;
}
j++;
}
}/*i*/
} while(false);
} else { /*exotic*/
simple:do {
{
var $5:int = d;
i = 0;
_i4:for(; $5 > 0; $5--, i++) {
if (i==dotoff) {
j++;
}
mant[i]=((inchars.charCodeAt(j))-(BigDecimal.VALUE_ZERO));
j++;
}
}/*i*/
} while(false);
}/*simple*/
/* Looks good. Set the sign indicator and form, as needed. */
// Trailing zeros are preserved
// The rule here for form is:
// If no E-notation, then request plain notation
// Otherwise act as though add(0,DEFAULT) and request scientific notation
// [form is already PLAIN]
if (mant[0]==0) {
ind=iszero; // force to show zero
// negative exponent is significant (e.g., -3 for 0.000) if plain
if (exp>0) {
exp=0; // positive exponent can be ignored
}
if (hadexp) { // zero becomes single digit from add
mant=ZERO.mant;
exp=0;
}
} else { // non-zero
// [ind was set earlier]
// now determine form
if (hadexp) {
form = MathContext.NOTATION_SCIENTIFIC;
// 1999.06.29 check for overflow
mag=(exp+mant.length)-1; // true exponent in scientific notation
if ((mag<MinExp)||(mag>MaxExp)) {
bad(inchars);
}
}
}
// say 'BD(c[]): mant[0] mantlen exp ind form:' mant[0] mant.length exp ind form
//ActionScript 3
//IF we create from Number, set the Default Scale to 10
if(createdFromNumber) {
var newScale:int = ((-exp) < 10) ? 10 : (-exp);
assignMyself(setScale(newScale));
}
}
/**
* Constructs a <code>BigDecimal</code> object directly from a
* <code>double</code>.
* <p>
* Constructs a <code>BigDecimal</code> which is the exact decimal
* representation of the 64-bit signed binary floating point
* parameter.
* <p>
* Note that this constructor it an exact conversion; it does not give
* the same result as converting <code>num</code> to a
* <code>String</code> using the <code>Double.toString()</code> method
* and then using the {@link #BigDecimal(java.lang.String)}
* constructor.
* To get that result, use the static {@link #valueOf(double)}
* method to construct a <code>BigDecimal</code> from a
* <code>double</code>.
*
* @param num The <code>double</code> to be converted.
* @throws NumberFormatException if the parameter is infinite or
* not a number.
* @stable ICU 2.0
*/
// ActionScript 3 : Wont Port
//public BigDecimal(double num){
// 1999.03.06: use exactly the old algorithm
// 2000.01.01: note that this constructor does give an exact result,
// so perhaps it should not be deprecated
// 2000.06.18: no longer deprecated
//#ifndef FOUNDATION
//## this((new java.math.BigDecimal(num)).toString());
//#else
//this(String.valueOf(num));
//#endif
//return;}
/**
* Constructs a <code>BigDecimal</code> object directly from a
* <code>int</code>.
* <p>
* Constructs a <code>BigDecimal</code> which is the exact decimal
* representation of the 32-bit signed binary integer parameter.
* The <code>BigDecimal</code> will contain only decimal digits,
* prefixed with a leading minus sign (hyphen) if the parameter is
* negative.
* A leading zero will be present only if the parameter is zero.
*
* @param num The <code>int</code> to be converted.
* @stable ICU 2.0
*/
private function createFromInt(num:int = 0):void {
var mun:int;
var i:int = 0;
// We fastpath commoners
if (num<=9) {
if (num>=(-9)) {
singledigit:do {
// very common single digit case
{/*select*/
if (num==0) {
mant=ZERO.mant;
ind=iszero;
} else if (num==1) {
mant=ONE.mant;
ind=ispos;
} else if (num==(-1)) {
mant=ONE.mant;
ind=isneg;
} else {
{
mant=new Array(1);
if (num>0) {
mant[0]=num as int;
ind=ispos;
} else { // num<-1
mant[0]=(-num) as int;
ind=isneg;