1 3 4 package com.ibm.icu.math; 5 import java.math.BigInteger ; 6 import com.ibm.icu.impl.Utility; 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 260 261 public class BigDecimal extends java.lang.Number implements java.io.Serializable ,java.lang.Comparable { 262 private static final java.lang.String $0="BigDecimal.nrx"; 263 264 265 266 267 275 public static final com.ibm.icu.math.BigDecimal ZERO=new com.ibm.icu.math.BigDecimal((long)0); 278 285 public static final com.ibm.icu.math.BigDecimal ONE=new com.ibm.icu.math.BigDecimal((long)1); 288 295 public static final com.ibm.icu.math.BigDecimal TEN=new com.ibm.icu.math.BigDecimal(10); 296 297 303 public static final int ROUND_CEILING=com.ibm.icu.math.MathContext.ROUND_CEILING; 304 305 310 public static final int ROUND_DOWN=com.ibm.icu.math.MathContext.ROUND_DOWN; 311 312 317 public static final int ROUND_FLOOR=com.ibm.icu.math.MathContext.ROUND_FLOOR; 318 319 325 public static final int ROUND_HALF_DOWN=com.ibm.icu.math.MathContext.ROUND_HALF_DOWN; 326 327 333 public static final int ROUND_HALF_EVEN=com.ibm.icu.math.MathContext.ROUND_HALF_EVEN; 334 335 341 public static final int ROUND_HALF_UP=com.ibm.icu.math.MathContext.ROUND_HALF_UP; 342 343 348 public static final int ROUND_UNNECESSARY=com.ibm.icu.math.MathContext.ROUND_UNNECESSARY; 349 350 355 public static final int ROUND_UP=com.ibm.icu.math.MathContext.ROUND_UP; 356 357 private static final byte ispos=1; private static final byte iszero=0; private static final byte isneg=-1; 363 private static final int MinExp=-999999999; private static final int MaxExp=999999999; private static final int MinArg=-999999999; private static final int MaxArg=999999999; 368 private static final com.ibm.icu.math.MathContext plainMC=new com.ibm.icu.math.MathContext(0,com.ibm.icu.math.MathContext.PLAIN); 370 372 private static final long serialVersionUID=8245355804974198832L; 374 375 private static final java.lang.String copyright=" Copyright (c) IBM Corporation 1996, 2000. All rights reserved. "; 376 377 378 private static byte bytecar[]=new byte[(90+99)+1]; private static byte bytedig[]=diginit(); 382 383 384 385 395 private byte ind; 400 414 private byte form=(byte)com.ibm.icu.math.MathContext.PLAIN; 418 433 private byte mant[]; 435 446 private int exp; 447 449 450 451 452 453 478 497 498 public BigDecimal(java.math.BigInteger bi){ 499 this(bi.toString(10)); 500 return;} 501 503 527 528 public BigDecimal(java.math.BigInteger bi,int scale){ 529 this(bi.toString(10)); 530 if (scale<0) 531 throw new java.lang.NumberFormatException ("Negative scale:"+" "+scale); 532 exp=(int)-scale; return;} 534 535 553 554 public BigDecimal(char inchars[]){ 555 this(inchars,0,inchars.length); 556 return;} 557 558 581 582 public BigDecimal(char inchars[],int offset,int length){super(); 583 boolean exotic; 584 boolean hadexp; 585 int d; 586 int dotoff; 587 int last; 588 int i=0; 589 char si=0; 590 boolean eneg=false; 591 int k=0; 592 int elen=0; 593 int j=0; 594 char sj=0; 595 int dvalue=0; 596 int mag=0; 597 if (length<=0) 604 bad(inchars); 607 608 ind=ispos; if (inchars[offset]==('-')) 610 { 611 length--; 612 if (length==0) 613 bad(inchars); ind=isneg; 615 offset++; 616 } 617 else 618 if (inchars[offset]==('+')) 619 { 620 length--; 621 if (length==0) 622 bad(inchars); offset++; 624 } 625 626 627 exotic=false; hadexp=false; d=0; dotoff=-1; last=-1; {int $1=length;i=offset;i:for(;$1>0;$1--,i++){ 633 si=inchars[i]; 634 if (si>='0') if (si<='9') 636 { 637 last=i; 638 d++; continue i; 640 } 641 if (si=='.') 642 { if (dotoff>=0) 644 bad(inchars); dotoff=i-offset; continue i; 647 } 648 if (si!='e') 649 if (si!='E') 650 { if ((!(java.lang.Character.isDigit(si)))) 652 bad(inchars); exotic=true; last=i; 656 d++; continue i; 658 } 659 660 if ((i-offset)>(length-2)) 662 bad(inchars); eneg=false; 664 if ((inchars[i+1])==('-')) 665 { 666 eneg=true; 667 k=i+2; 668 } 669 else 670 if ((inchars[i+1])==('+')) 671 k=i+2; 672 else 673 k=i+1; 674 elen=length-((k-offset)); if ((elen==0)|(elen>9)) 677 bad(inchars); {int $2=elen;j=k;j:for(;$2>0;$2--,j++){ 679 sj=inchars[j]; 680 if (sj<'0') 681 bad(inchars); if (sj>'9') 683 { if ((!(java.lang.Character.isDigit(sj)))) 685 bad(inchars); dvalue=java.lang.Character.digit(sj,10); if (dvalue<0) 688 bad(inchars); } 690 else 691 dvalue=((int)(sj))-((int)('0')); 692 exp=(exp*10)+dvalue; 693 } 694 } 695 if (eneg) 696 exp=(int)-exp; hadexp=true; break i; } 700 } 701 702 703 if (d==0) 704 bad(inchars); if (dotoff>=0) 706 exp=(exp+dotoff)-d; 708 709 {int $3=last-1;i=offset;i:for(;i<=$3;i++){ 710 si=inchars[i]; 711 if (si=='0') 712 { 713 offset++; 714 dotoff--; 715 d--; 716 } 717 else 718 if (si=='.') 719 { 720 offset++; dotoff--; 722 } 723 else 724 if (si<='9') 725 break i; 726 else 727 { 728 if ((java.lang.Character.digit(si,10))!=0) 729 break i; offset++; 732 dotoff--; 733 d--; 734 } 735 } 736 } 737 738 739 mant=new byte[d]; j=offset; if (exotic) 742 {exotica:do{ {int $4=d;i=0;i:for(;$4>0;$4--,i++){ 744 if (i==dotoff) 745 j++; sj=inchars[j]; 747 if (sj<='9') 748 mant[i]=(byte)(((int)(sj))-((int)('0'))); 749 else 750 { 751 dvalue=java.lang.Character.digit(sj,10); 752 if (dvalue<0) 753 bad(inchars); mant[i]=(byte)dvalue; 755 } 756 j++; 757 } 758 } 759 }while(false);} 760 else 761 {simple:do{ 762 {int $5=d;i=0;i:for(;$5>0;$5--,i++){ 763 if (i==dotoff) 764 j++; 765 mant[i]=(byte)(((int)(inchars[j]))-((int)('0'))); 766 j++; 767 } 768 } 769 }while(false);} 770 771 772 if (mant[0]==0) 778 { 779 ind=iszero; if (exp>0) 782 exp=0; if (hadexp) 784 { mant=ZERO.mant; 786 exp=0; 787 } 788 } 789 else 790 { if (hadexp) 794 { 795 form=(byte)com.ibm.icu.math.MathContext.SCIENTIFIC; 796 mag=(exp+mant.length)-1; if ((mag<MinExp)|(mag>MaxExp)) 799 bad(inchars); 800 } 801 } 802 return; 804 } 805 806 828 829 public BigDecimal(double num){ 830 this(String.valueOf(num)); 838 return;} 840 854 855 public BigDecimal(int num){super(); 856 int mun; 857 int i=0; 858 if (num<=9) 860 if (num>=(-9)) 861 {singledigit:do{ 862 { 864 if (num==0) 865 { 866 mant=ZERO.mant; 867 ind=iszero; 868 } 869 else if (num==1) 870 { 871 mant=ONE.mant; 872 ind=ispos; 873 } 874 else if (num==(-1)) 875 { 876 mant=ONE.mant; 877 ind=isneg; 878 } 879 else{ 880 { 881 mant=new byte[1]; 882 if (num>0) 883 { 884 mant[0]=(byte)num; 885 ind=ispos; 886 } 887 else 888 { mant[0]=(byte)((int)-num); 890 ind=isneg; 891 } 892 } 893 } 894 } 895 return; 896 }while(false);} 897 898 899 if (num>0) 900 { 901 ind=ispos; 902 num=(int)-num; 903 } 904 else 905 ind=isneg; mun=num; {i=9;i:for(;;i--){ 911 mun=mun/10; 912 if (mun==0) 913 break i; 914 } 915 } 916 mant=new byte[10-i]; 918 {i=(10-i)-1;i:for(;;i--){ 919 mant[i]=(byte)-(((byte)(num%10))); 920 num=num/10; 921 if (num==0) 922 break i; 923 } 924 } 925 return; 926 } 927 928 942 943 public BigDecimal(long num){super(); 944 long mun; 945 int i=0; 946 950 if (num>0) 951 { 952 ind=ispos; 953 num=(long)-num; 954 } 955 else 956 if (num==0) 957 ind=iszero; 958 else 959 ind=isneg; 960 mun=num; 961 {i=18;i:for(;;i--){ 962 mun=mun/10; 963 if (mun==0) 964 break i; 965 } 966 } 967 mant=new byte[19-i]; 969 {i=(19-i)-1;i:for(;;i--){ 970 mant[i]=(byte)-(((byte)(num%10))); 971 num=num/10; 972 if (num==0) 973 break i; 974 } 975 } 976 return; 977 } 978 979 1036 1037 public BigDecimal(java.lang.String string){ 1038 this(string.toCharArray(),0,string.length()); 1039 return;} 1040 1041 1042 1043 private BigDecimal(){super(); 1044 return; 1045 } 1046 1047 1048 1049 1050 1051 1065 1066 public com.ibm.icu.math.BigDecimal abs(){ 1067 return this.abs(plainMC); 1068 } 1069 1070 1086 1087 public com.ibm.icu.math.BigDecimal abs(com.ibm.icu.math.MathContext set){ 1088 if (this.ind==isneg) 1089 return this.negate(set); 1090 return this.plus(set); 1091 } 1092 1093 1110 1111 public com.ibm.icu.math.BigDecimal add(com.ibm.icu.math.BigDecimal rhs){ 1112 return this.add(rhs,plainMC); 1113 } 1114 1115 1130 1131 public com.ibm.icu.math.BigDecimal add(com.ibm.icu.math.BigDecimal rhs,com.ibm.icu.math.MathContext set){ 1132 com.ibm.icu.math.BigDecimal lhs; 1133 int reqdig; 1134 com.ibm.icu.math.BigDecimal res; 1135 byte usel[]; 1136 int usellen; 1137 byte user[]; 1138 int userlen; 1139 int newlen=0; 1140 int tlen=0; 1141 int mult=0; 1142 byte t[]=null; 1143 int ia=0; 1144 int ib=0; 1145 int ea=0; 1146 int eb=0; 1147 byte ca=0; 1148 byte cb=0; 1149 1150 if (set.lostDigits) 1151 checkdigits(rhs,set.digits); 1152 lhs=this; 1154 1155 if (lhs.ind==0) 1157 if (set.form!=com.ibm.icu.math.MathContext.PLAIN) 1158 return rhs.plus(set); 1159 if (rhs.ind==0) 1160 if (set.form!=com.ibm.icu.math.MathContext.PLAIN) 1161 return lhs.plus(set); 1162 1163 1164 reqdig=set.digits; if (reqdig>0) 1166 { 1167 if (lhs.mant.length>reqdig) 1168 lhs=clone(lhs).round(set); 1169 if (rhs.mant.length>reqdig) 1170 rhs=clone(rhs).round(set); 1171 } 1173 1174 res=new com.ibm.icu.math.BigDecimal(); 1176 1180 usel=lhs.mant; 1184 usellen=lhs.mant.length; 1185 user=rhs.mant; 1186 userlen=rhs.mant.length; 1187 {padder:do{ 1188 if (lhs.exp==rhs.exp) 1189 { 1190 res.exp=lhs.exp; 1192 } 1193 else if (lhs.exp>rhs.exp) 1194 { newlen=(usellen+lhs.exp)-rhs.exp; 1196 1199 if (newlen>=((userlen+reqdig)+1)) 1200 if (reqdig>0) 1201 { 1202 res.mant=usel; 1204 res.exp=lhs.exp; 1205 res.ind=lhs.ind; 1206 if (usellen<reqdig) 1207 { res.mant=extend(lhs.mant,reqdig); 1209 res.exp=res.exp-((reqdig-usellen)); 1210 } 1211 return res.finish(set,false); 1212 } 1213 res.exp=rhs.exp; if (newlen>(reqdig+1)) 1216 if (reqdig>0) 1217 { 1218 tlen=(newlen-reqdig)-1; userlen=userlen-tlen; 1221 res.exp=res.exp+tlen; 1222 newlen=reqdig+1; 1223 } 1224 if (newlen>usellen) 1225 usellen=newlen; } 1227 else{ newlen=(userlen+rhs.exp)-lhs.exp; 1229 if (newlen>=((usellen+reqdig)+1)) 1230 if (reqdig>0) 1231 { 1232 res.mant=user; 1234 res.exp=rhs.exp; 1235 res.ind=rhs.ind; 1236 if (userlen<reqdig) 1237 { res.mant=extend(rhs.mant,reqdig); 1239 res.exp=res.exp-((reqdig-userlen)); 1240 } 1241 return res.finish(set,false); 1242 } 1243 res.exp=lhs.exp; if (newlen>(reqdig+1)) 1246 if (reqdig>0) 1247 { 1248 tlen=(newlen-reqdig)-1; usellen=usellen-tlen; 1251 res.exp=res.exp+tlen; 1252 newlen=reqdig+1; 1253 } 1254 if (newlen>userlen) 1255 userlen=newlen; } 1257 }while(false);} 1258 1259 1260 if (lhs.ind==iszero) 1264 res.ind=ispos; 1265 else 1266 res.ind=lhs.ind; if (((lhs.ind==isneg)?1:0)==((rhs.ind==isneg)?1:0)) mult=1; 1269 else 1270 {signdiff:do{ mult=-1; 1275 {swaptest:do{ 1276 if (rhs.ind==iszero){ 1277 }else if ((usellen<userlen)|(lhs.ind==iszero)) 1279 { t=usel; 1281 usel=user; 1282 user=t; tlen=usellen; 1284 usellen=userlen; 1285 userlen=tlen; res.ind=(byte)-res.ind; } 1288 else if (usellen>userlen){ 1289 }else{ 1291 { 1293 ia=0; 1294 ib=0; 1295 ea=usel.length-1; 1296 eb=user.length-1; 1297 {compare:for(;;){ 1298 if (ia<=ea) 1299 ca=usel[ia]; 1300 else 1301 { 1302 if (ib>eb) 1303 { 1304 if (set.form!=com.ibm.icu.math.MathContext.PLAIN) 1305 return ZERO; 1306 break compare; 1308 } 1309 ca=(byte)0; 1310 } 1311 if (ib<=eb) 1312 cb=user[ib]; 1313 else 1314 cb=(byte)0; 1315 if (ca!=cb) 1316 { 1317 if (ca<cb) 1318 { 1319 t=usel; 1320 usel=user; 1321 user=t; tlen=usellen; 1323 usellen=userlen; 1324 userlen=tlen; res.ind=(byte)-res.ind; 1326 } 1327 break compare; 1328 } 1329 1330 ia++; 1331 ib++; 1332 } 1333 } 1334 } } 1336 }while(false);} 1337 }while(false);} 1338 1339 1340 res.mant=byteaddsub(usel,usellen,user,userlen,mult,false); 1342 1344 return res.finish(set,false); 1346 } 1347 1348 1364 1365 public int compareTo(com.ibm.icu.math.BigDecimal rhs){ 1366 return this.compareTo(rhs,plainMC); 1367 } 1368 1369 1400 1401 public int compareTo(com.ibm.icu.math.BigDecimal rhs,com.ibm.icu.math.MathContext set){ 1402 int thislength=0; 1403 int i=0; 1404 com.ibm.icu.math.BigDecimal newrhs; 1405 if (set.lostDigits) 1407 checkdigits(rhs,set.digits); 1408 if ((this.ind==rhs.ind)&(this.exp==rhs.exp)) 1410 { 1411 1412 thislength=this.mant.length; 1413 if (thislength<rhs.mant.length) 1414 return (byte)-this.ind; 1415 if (thislength>rhs.mant.length) 1416 return this.ind; 1417 1419 if ((thislength<=set.digits)|(set.digits==0)) 1420 { 1421 {int $6=thislength;i=0;i:for(;$6>0;$6--,i++){ 1422 if (this.mant[i]<rhs.mant[i]) 1423 return (byte)-this.ind; 1424 if (this.mant[i]>rhs.mant[i]) 1425 return this.ind; 1426 } 1427 } 1428 return 0; } 1430 1431 } 1432 else 1433 { 1434 1435 if (this.ind<rhs.ind) 1436 return -1; 1437 if (this.ind>rhs.ind) 1438 return 1; 1439 } 1440 1441 newrhs=clone(rhs); newrhs.ind=(byte)-newrhs.ind; return this.add(newrhs,set).ind; } 1445 1446 1465 1466 public com.ibm.icu.math.BigDecimal divide(com.ibm.icu.math.BigDecimal rhs){ 1467 return this.dodivide('D',rhs,plainMC,-1); 1468 } 1469 1470 1500 1501 public com.ibm.icu.math.BigDecimal divide(com.ibm.icu.math.BigDecimal rhs,int round){ 1502 com.ibm.icu.math.MathContext set; 1503 set=new com.ibm.icu.math.MathContext(0,com.ibm.icu.math.MathContext.PLAIN,false,round); return this.dodivide('D',rhs,set,-1); } 1506 1507 1540 1541 public com.ibm.icu.math.BigDecimal divide(com.ibm.icu.math.BigDecimal rhs,int scale,int round){ 1542 com.ibm.icu.math.MathContext set; 1543 if (scale<0) 1544 throw new java.lang.ArithmeticException ("Negative scale:"+" "+scale); 1545 set=new com.ibm.icu.math.MathContext(0,com.ibm.icu.math.MathContext.PLAIN,false,round); return this.dodivide('D',rhs,set,scale); 1547 } 1548 1549 1565 1566 public com.ibm.icu.math.BigDecimal divide(com.ibm.icu.math.BigDecimal rhs,com.ibm.icu.math.MathContext set){ 1567 return this.dodivide('D',rhs,set,-1); 1568 } 1569 1570 1585 1586 public com.ibm.icu.math.BigDecimal divideInteger(com.ibm.icu.math.BigDecimal rhs){ 1587 return this.dodivide('I',rhs,plainMC,0); 1589 } 1590 1591 1610 1611 public com.ibm.icu.math.BigDecimal divideInteger(com.ibm.icu.math.BigDecimal rhs,com.ibm.icu.math.MathContext set){ 1612 return this.dodivide('I',rhs,set,0); 1614 } 1615 1616 1630 1631 public com.ibm.icu.math.BigDecimal max(com.ibm.icu.math.BigDecimal rhs){ 1632 return this.max(rhs,plainMC); 1633 } 1634 1635 1657 1658 public com.ibm.icu.math.BigDecimal max(com.ibm.icu.math.BigDecimal rhs,com.ibm.icu.math.MathContext set){ 1659 if ((this.compareTo(rhs,set))>=0) 1660 return this.plus(set); 1661 else 1662 return rhs.plus(set); 1663 } 1664 1665 1679 1680 public com.ibm.icu.math.BigDecimal min(com.ibm.icu.math.BigDecimal rhs){ 1681 return this.min(rhs,plainMC); 1682 } 1683 1684 1706 1707 public com.ibm.icu.math.BigDecimal min(com.ibm.icu.math.BigDecimal rhs,com.ibm.icu.math.MathContext set){ 1708 if ((this.compareTo(rhs,set))<=0) 1709 return this.plus(set); 1710 else 1711 return rhs.plus(set); 1712 } 1713 1714 1732 1733 public com.ibm.icu.math.BigDecimal multiply(com.ibm.icu.math.BigDecimal rhs){ 1734 return this.multiply(rhs,plainMC); 1735 } 1736 1737 1752 1753 public com.ibm.icu.math.BigDecimal multiply(com.ibm.icu.math.BigDecimal rhs,com.ibm.icu.math.MathContext set){ 1754 com.ibm.icu.math.BigDecimal lhs; 1755 int padding; 1756 int reqdig; 1757 byte multer[]=null; 1758 byte multand[]=null; 1759 int multandlen; 1760 int acclen=0; 1761 com.ibm.icu.math.BigDecimal res; 1762 byte acc[]; 1763 int n=0; 1764 byte mult=0; 1765 if (set.lostDigits) 1766 checkdigits(rhs,set.digits); 1767 lhs=this; 1769 1770 padding=0; reqdig=set.digits; if (reqdig>0) 1773 { 1774 if (lhs.mant.length>reqdig) 1775 lhs=clone(lhs).round(set); 1776 if (rhs.mant.length>reqdig) 1777 rhs=clone(rhs).round(set); 1778 } 1780 else 1781 { 1782 if (lhs.exp>0) 1785 padding=padding+lhs.exp; 1786 if (rhs.exp>0) 1787 padding=padding+rhs.exp; 1788 } 1789 1790 if (lhs.mant.length<rhs.mant.length) 1795 { 1796 multer=lhs.mant; 1797 multand=rhs.mant; 1798 } 1799 else 1800 { 1801 multer=rhs.mant; 1802 multand=lhs.mant; 1803 } 1804 1805 1806 multandlen=(multer.length+multand.length)-1; if ((multer[0]*multand[0])>9) 1809 acclen=multandlen+1; 1810 else 1811 acclen=multandlen; 1812 1813 1814 res=new com.ibm.icu.math.BigDecimal(); acc=new byte[acclen]; {int $7=multer.length;n=0;n:for(;$7>0;$7--,n++){ 1821 mult=multer[n]; 1822 if (mult!=0) 1823 { acc=byteaddsub(acc,acc.length,multand,multandlen,mult,true); 1826 } 1827 multandlen--; } 1830 } 1831 1832 res.ind=(byte)(lhs.ind*rhs.ind); res.exp=(lhs.exp+rhs.exp)-padding; 1836 1837 if (padding==0) 1838 res.mant=acc; 1839 else 1840 res.mant=extend(acc,acc.length+padding); return res.finish(set,false); 1842 } 1843 1844 1859 1860 public com.ibm.icu.math.BigDecimal negate(){ 1861 return this.negate(plainMC); 1862 } 1863 1864 1877 1878 public com.ibm.icu.math.BigDecimal negate(com.ibm.icu.math.MathContext set){ 1879 com.ibm.icu.math.BigDecimal res; 1880 if (set.lostDigits) 1883 checkdigits((com.ibm.icu.math.BigDecimal)null,set.digits); 1884 res=clone(this); res.ind=(byte)-res.ind; 1886 return res.finish(set,false); 1887 } 1888 1889 1905 1906 public com.ibm.icu.math.BigDecimal plus(){ 1907 return this.plus(plainMC); 1908 } 1909 1910 1927 1928 public com.ibm.icu.math.BigDecimal plus(com.ibm.icu.math.MathContext set){ 1929 if (set.lostDigits) 1932 checkdigits((com.ibm.icu.math.BigDecimal)null,set.digits); 1933 if (set.form==com.ibm.icu.math.MathContext.PLAIN) 1935 if (this.form==com.ibm.icu.math.MathContext.PLAIN) 1936 { 1937 if (this.mant.length<=set.digits) 1938 return this; 1939 if (set.digits==0) 1940 return this; 1941 } 1942 return clone(this).finish(set,false); 1943 } 1944 1945 1971 1972 public com.ibm.icu.math.BigDecimal pow(com.ibm.icu.math.BigDecimal rhs){ 1973 return this.pow(rhs,plainMC); 1974 } 1975 1978 2004 2005 public com.ibm.icu.math.BigDecimal pow(com.ibm.icu.math.BigDecimal rhs,com.ibm.icu.math.MathContext set){ 2006 int n; 2007 com.ibm.icu.math.BigDecimal lhs; 2008 int reqdig; 2009 int workdigits=0; 2010 int L=0; 2011 com.ibm.icu.math.MathContext workset; 2012 com.ibm.icu.math.BigDecimal res; 2013 boolean seenbit; 2014 int i=0; 2015 if (set.lostDigits) 2016 checkdigits(rhs,set.digits); 2017 n=rhs.intcheck(MinArg,MaxArg); lhs=this; 2020 reqdig=set.digits; if (reqdig==0) 2022 { 2023 if (rhs.ind==isneg) 2024 throw new java.lang.ArithmeticException ("Negative power:"+" "+rhs.toString()); 2025 workdigits=0; 2026 } 2027 else 2028 { 2029 if ((rhs.mant.length+rhs.exp)>reqdig) 2030 throw new java.lang.ArithmeticException ("Too many digits:"+" "+rhs.toString()); 2031 2032 2033 if (lhs.mant.length>reqdig) 2034 lhs=clone(lhs).round(set); 2035 2036 2037 L=rhs.mant.length+rhs.exp; workdigits=(reqdig+L)+1; } 2040 2041 2042 workset=new com.ibm.icu.math.MathContext(workdigits,set.form,false,set.roundingMode); 2045 2046 res=ONE; if (n==0) 2048 return res; if (n<0) 2050 n=(int)-n; seenbit=false; {i=1;i:for(;;i++){ n=n+n; if (n<0) 2055 { seenbit=true; res=res.multiply(lhs,workset); } 2059 if (i==31) 2060 break i; if ((!seenbit)) 2062 continue i; res=res.multiply(res,workset); } 2065 } if (rhs.ind<0) res=ONE.divide(res,workset); return res.finish(set,true); } 2070 2071 2088 2089 public com.ibm.icu.math.BigDecimal remainder(com.ibm.icu.math.BigDecimal rhs){ 2090 return this.dodivide('R',rhs,plainMC,-1); 2091 } 2092 2093 2115 2116 public com.ibm.icu.math.BigDecimal remainder(com.ibm.icu.math.BigDecimal rhs,com.ibm.icu.math.MathContext set){ 2117 return this.dodivide('R',rhs,set,-1); 2118 } 2119 2120 2137 2138 public com.ibm.icu.math.BigDecimal subtract(com.ibm.icu.math.BigDecimal rhs){ 2139 return this.subtract(rhs,plainMC); 2140 } 2141 2142 2157 2158 public com.ibm.icu.math.BigDecimal subtract(com.ibm.icu.math.BigDecimal rhs,com.ibm.icu.math.MathContext set){ 2159 com.ibm.icu.math.BigDecimal newrhs; 2160 if (set.lostDigits) 2161 checkdigits(rhs,set.digits); 2162 2164 newrhs=clone(rhs); newrhs.ind=(byte)-newrhs.ind; return this.add(newrhs,set); } 2169 2170 2171 2172 2173 2174 2185 2186 public byte byteValueExact(){ 2187 int num; 2188 num=this.intValueExact(); if ((num>127)|(num<(-128))) 2190 throw new java.lang.ArithmeticException ("Conversion overflow:"+" "+this.toString()); 2191 return (byte)num; 2192 } 2193 2194 2216 2217 public int compareTo(java.lang.Object rhsobj){ 2218 return compareTo((com.ibm.icu.math.BigDecimal)rhsobj,plainMC); 2220 } 2221 2222 2237 2238 public double doubleValue(){ 2239 return java.lang.Double.valueOf(this.toString()).doubleValue(); 2242 } 2243 2244 2270 2271 public boolean equals(java.lang.Object obj){ 2272 com.ibm.icu.math.BigDecimal rhs; 2273 int i=0; 2274 char lca[]=null; 2275 char rca[]=null; 2276 if (obj==null) 2278 return false; if ((!(((obj instanceof com.ibm.icu.math.BigDecimal))))) 2280 return false; rhs=(com.ibm.icu.math.BigDecimal)obj; if (this.ind!=rhs.ind) 2283 return false; if (((this.mant.length==rhs.mant.length)&(this.exp==rhs.exp))&(this.form==rhs.form)) 2285 2286 { {int $8=this.mant.length;i=0;i:for(;$8>0;$8--,i++){ 2289 if (this.mant[i]!=rhs.mant[i]) 2290 return false; 2291 } 2292 } 2293 } 2294 else 2295 { lca=this.layout(); rca=rhs.layout(); 2298 if (lca.length!=rca.length) 2299 return false; {int $9=lca.length;i=0;i:for(;$9>0;$9--,i++){ 2302 if (lca[i]!=rca[i]) 2303 return false; 2304 } 2305 } 2306 } 2307 return true; } 2309 2310 2325 2326 public float floatValue(){ 2327 return java.lang.Float.valueOf(this.toString()).floatValue(); 2328 } 2329 2330 2391 2392 public java.lang.String format(int before,int after){ 2393 return format(before,after,-1,-1,com.ibm.icu.math.MathContext.SCIENTIFIC,ROUND_HALF_UP); 2394 } 2395 2396 2505 2506 public java.lang.String format(int before,int after,int explaces,int exdigits,int exformint,int exround){ 2507 com.ibm.icu.math.BigDecimal num; 2508 int mag=0; 2509 int thisafter=0; 2510 int lead=0; 2511 byte newmant[]=null; 2512 int chop=0; 2513 int need=0; 2514 int oldexp=0; 2515 char a[]; 2516 int p=0; 2517 char newa[]=null; 2518 int i=0; 2519 int places=0; 2520 2521 2522 2523 if ((before<(-1))|(before==0)) 2524 badarg("format",1,java.lang.String.valueOf(before)); 2525 if (after<(-1)) 2526 badarg("format",2,java.lang.String.valueOf(after)); 2527 if ((explaces<(-1))|(explaces==0)) 2528 badarg("format",3,java.lang.String.valueOf(explaces)); 2529 if (exdigits<(-1)) 2530 badarg("format",4,java.lang.String.valueOf(explaces)); 2531 { 2532 if (exformint==com.ibm.icu.math.MathContext.SCIENTIFIC){ 2533 }else if (exformint==com.ibm.icu.math.MathContext.ENGINEERING){ 2534 }else if (exformint==(-1)) 2535 exformint=com.ibm.icu.math.MathContext.SCIENTIFIC; 2536 else{ 2538 badarg("format",5,java.lang.String.valueOf(exformint)); 2539 } 2540 } 2541 if (exround!=ROUND_HALF_UP) 2544 {try{ if (exround==(-1)) 2546 exround=ROUND_HALF_UP; 2547 else 2548 new com.ibm.icu.math.MathContext(9,com.ibm.icu.math.MathContext.SCIENTIFIC,false,exround); 2549 } 2550 catch (java.lang.IllegalArgumentException $10){ 2551 badarg("format",6,java.lang.String.valueOf(exround)); 2552 }} 2553 2554 num=clone(this); 2556 2566 2567 2568 {setform:do{ 2569 if (exdigits==(-1)) 2570 num.form=(byte)com.ibm.icu.math.MathContext.PLAIN; 2571 else if (num.ind==iszero) 2572 num.form=(byte)com.ibm.icu.math.MathContext.PLAIN; 2573 else{ 2574 mag=num.exp+num.mant.length; 2576 if (mag>exdigits) 2577 num.form=(byte)exformint; 2578 else 2579 if (mag<(-5)) 2580 num.form=(byte)exformint; 2581 else 2582 num.form=(byte)com.ibm.icu.math.MathContext.PLAIN; 2583 } 2584 }while(false);} 2585 2586 2590 if (after>=0) 2591 {setafter:for(;;){ 2592 { 2594 if (num.form==com.ibm.icu.math.MathContext.PLAIN) 2595 thisafter=(int)-num.exp; else if (num.form==com.ibm.icu.math.MathContext.SCIENTIFIC) 2597 thisafter=num.mant.length-1; 2598 else{ lead=(((num.exp+num.mant.length)-1))%3; if (lead<0) 2601 lead=3+lead; lead++; if (lead>=num.mant.length) 2604 thisafter=0; 2605 else 2606 thisafter=num.mant.length-lead; 2607 } 2608 } 2609 if (thisafter==after) 2610 break setafter; if (thisafter<after) 2612 { newmant=extend(num.mant,(num.mant.length+after)-thisafter); 2615 num.mant=newmant; 2616 num.exp=num.exp-((after-thisafter)); if (num.exp<MinExp) 2618 throw new java.lang.ArithmeticException ("Exponent Overflow:"+" "+num.exp); 2619 break setafter; 2620 } 2621 chop=thisafter-after; if (chop>num.mant.length) 2626 { num.mant=ZERO.mant; 2629 num.ind=iszero; 2630 num.exp=0; 2631 continue setafter; } 2633 need=num.mant.length-chop; oldexp=num.exp; num.round(need,exround); 2638 if ((num.exp-oldexp)==chop) 2641 break setafter; } 2644 } 2645 2646 a=num.layout(); 2648 2649 if (before>0) 2651 { 2652 {int $11=a.length;p=0;p:for(;$11>0;$11--,p++){ 2654 if (a[p]=='.') 2655 break p; 2656 if (a[p]=='E') 2657 break p; 2658 } 2659 } 2660 if (p>before) 2663 badarg("format",1,java.lang.String.valueOf(before)); if (p<before) 2665 { newa=new char[(a.length+before)-p]; 2667 {int $12=before-p;i=0;i:for(;$12>0;$12--,i++){ 2668 newa[i]=' '; 2669 } 2670 } 2671 java.lang.System.arraycopy((java.lang.Object )a,0,(java.lang.Object )newa,i,a.length); 2672 a=newa; 2673 } 2674 } 2676 2677 if (explaces>0) 2678 { 2679 {int $13=a.length-1;p=a.length-1;p:for(;$13>0;$13--,p--){ 2681 if (a[p]=='E') 2682 break p; 2683 } 2684 } 2685 if (p==0) 2687 { newa=new char[(a.length+explaces)+2]; 2689 java.lang.System.arraycopy((java.lang.Object )a,0,(java.lang.Object )newa,0,a.length); 2690 {int $14=explaces+2;i=a.length;i:for(;$14>0;$14--,i++){ 2691 newa[i]=' '; 2692 } 2693 } 2694 a=newa; 2695 } 2696 else 2697 { places=(a.length-p)-2; if (places>explaces) 2700 badarg("format",3,java.lang.String.valueOf(explaces)); 2701 if (places<explaces) 2702 { newa=new char[(a.length+explaces)-places]; 2704 java.lang.System.arraycopy((java.lang.Object )a,0,(java.lang.Object )newa,0,p+2); {int $15=explaces-places;i=p+2;i:for(;$15>0;$15--,i++){ 2706 newa[i]='0'; 2707 } 2708 } 2709 java.lang.System.arraycopy((java.lang.Object )a,p+2,(java.lang.Object )newa,i,places); a=newa; 2711 } 2712 } 2714 } 2715 return new java.lang.String (a); 2716 } 2717 2718 2732 2733 public int hashCode(){ 2734 return this.toString().hashCode(); 2737 } 2738 2739 2752 2753 public int intValue(){ 2754 return toBigInteger().intValue(); 2755 } 2756 2757 2769 2770 public int intValueExact(){ 2771 int lodigit; 2772 int useexp=0; 2773 int result; 2774 int i=0; 2775 int topdig=0; 2776 if (ind==iszero) 2780 return 0; 2782 lodigit=mant.length-1; 2783 if (exp<0) 2784 { 2785 lodigit=lodigit+exp; 2787 if ((!(allzero(mant,lodigit+1)))) 2788 throw new java.lang.ArithmeticException ("Decimal part non-zero:"+" "+this.toString()); 2789 if (lodigit<0) 2790 return 0; useexp=0; 2792 } 2793 else 2794 { 2795 if ((exp+lodigit)>9) throw new java.lang.ArithmeticException ("Conversion overflow:"+" "+this.toString()); 2797 useexp=exp; 2798 } 2799 2800 result=0; 2801 {int $16=lodigit+useexp;i=0;i:for(;i<=$16;i++){ 2802 result=result*10; 2803 if (i<=lodigit) 2804 result=result+mant[i]; 2805 } 2806 } 2807 2808 2809 if ((lodigit+useexp)==9) 2810 { 2811 topdig=result/1000000000; if (topdig!=mant[0]) 2815 { if (result==java.lang.Integer.MIN_VALUE) if (ind==isneg) if (mant[0]==2) 2820 return result; throw new java.lang.ArithmeticException ("Conversion overflow:"+" "+this.toString()); 2822 } 2823 } 2824 2825 2826 if (ind==ispos) 2827 return result; 2828 return (int)-result; 2829 } 2830 2831 2844 2845 public long longValue(){ 2846 return toBigInteger().longValue(); 2847 } 2848 2849 2861 2862 public long longValueExact(){ 2863 int lodigit; 2864 int cstart=0; 2865 int useexp=0; 2866 long result; 2867 int i=0; 2868 long topdig=0; 2869 if (ind==0) 2871 return 0; lodigit=mant.length-1; if (exp<0) 2874 { 2875 lodigit=lodigit+exp; 2877 if (lodigit<0) 2878 cstart=0; 2879 else 2880 cstart=lodigit+1; 2881 if ((!(allzero(mant,cstart)))) 2882 throw new java.lang.ArithmeticException ("Decimal part non-zero:"+" "+this.toString()); 2883 if (lodigit<0) 2884 return 0; useexp=0; 2886 } 2887 else 2888 { 2889 if ((exp+mant.length)>18) throw new java.lang.ArithmeticException ("Conversion overflow:"+" "+this.toString()); 2891 useexp=exp; 2892 } 2893 2894 2895 result=(long)0; 2899 {int $17=lodigit+useexp;i=0;i:for(;i<=$17;i++){ 2900 result=result*10; 2901 if (i<=lodigit) 2902 result=result+mant[i]; 2903 } 2904 } 2905 2906 2907 if ((lodigit+useexp)==18) 2908 { 2909 topdig=result/1000000000000000000L; if (topdig!=mant[0]) 2911 { if (result==java.lang.Long.MIN_VALUE) if (ind==isneg) if (mant[0]==9) 2916 return result; throw new java.lang.ArithmeticException ("Conversion overflow:"+" "+this.toString()); 2918 } 2919 } 2920 2921 2922 if (ind==ispos) 2923 return result; 2924 return (long)-result; 2925 } 2926 2927 2948 2949 public com.ibm.icu.math.BigDecimal movePointLeft(int n){ 2950 com.ibm.icu.math.BigDecimal res; 2951 res=clone(this); 2953 res.exp=res.exp-n; 2954 return res.finish(plainMC,false); } 2956 2957 2978 2979 public com.ibm.icu.math.BigDecimal movePointRight(int n){ 2980 com.ibm.icu.math.BigDecimal res; 2981 res=clone(this); 2982 res.exp=res.exp+n; 2983 return res.finish(plainMC,false); 2984 } 2985 2986 2997 2998 public int scale(){ 2999 if (exp>=0) 3000 return 0; return (int)-exp; 3002 } 3003 3004 3030 3031 public com.ibm.icu.math.BigDecimal setScale(int scale){ 3032 return setScale(scale,ROUND_UNNECESSARY); 3033 } 3034 3035 3068 3069 public com.ibm.icu.math.BigDecimal setScale(int scale,int round){ 3070 int ourscale; 3071 com.ibm.icu.math.BigDecimal res; 3072 int padding=0; 3073 int newlen=0; 3074 ourscale=this.scale(); 3077 if (ourscale==scale) if (this.form==com.ibm.icu.math.MathContext.PLAIN) return this; 3080 res=clone(this); if (ourscale<=scale) 3082 { if (ourscale==0) 3085 padding=res.exp+scale; 3086 else 3087 padding=scale-ourscale; 3088 res.mant=extend(res.mant,res.mant.length+padding); 3089 res.exp=(int)-scale; } 3091 else 3092 { 3093 if (scale<0) 3094 throw new java.lang.ArithmeticException ("Negative scale:"+" "+scale); 3095 newlen=res.mant.length-((ourscale-scale)); res=res.round(newlen,round); if (res.exp!=((int)-scale)) 3101 { 3102 res.mant=extend(res.mant,res.mant.length+1); 3103 res.exp=res.exp-1; 3104 } 3105 } 3106 res.form=(byte)com.ibm.icu.math.MathContext.PLAIN; return res; 3108 } 3109 3110 3122 3123 public short shortValueExact(){ 3124 int num; 3125 num=this.intValueExact(); if ((num>32767)|(num<(-32768))) 3127 throw new java.lang.ArithmeticException ("Conversion overflow:"+" "+this.toString()); 3128 return (short)num; 3129 } 3130 3131 3144 3145 public int signum(){ 3146 return (int)this.ind; } 3148 3149 3174 3186 3187 public java.math.BigInteger toBigInteger(){ 3188 com.ibm.icu.math.BigDecimal res=null; 3189 int newlen=0; 3190 byte newmant[]=null; 3191 { 3192 if ((exp>=0)&(form==com.ibm.icu.math.MathContext.PLAIN)) 3193 res=this; else if (exp>=0) 3195 { 3196 res=clone(this); res.form=(byte)com.ibm.icu.math.MathContext.PLAIN; } 3199 else{ 3200 { if (((int)-this.exp)>=this.mant.length) 3203 res=ZERO; else 3205 { 3206 res=clone(this); newlen=res.mant.length+res.exp; 3208 newmant=new byte[newlen]; java.lang.System.arraycopy((java.lang.Object )res.mant,0,(java.lang.Object )newmant,0,newlen); 3210 res.mant=newmant; 3211 res.form=(byte)com.ibm.icu.math.MathContext.PLAIN; 3212 res.exp=0; 3213 } 3214 } 3215 } 3216 } 3217 return new BigInteger (new java.lang.String (res.layout())); 3218 } 3219 3220 3232 3233 public java.math.BigInteger toBigIntegerExact(){ 3234 3235 if (exp<0) 3236 { 3238 if ((!(allzero(mant,mant.length+exp)))) 3239 throw new java.lang.ArithmeticException ("Decimal part non-zero:"+" "+this.toString()); 3240 } 3241 return toBigInteger(); 3242 } 3243 3244 3255 3256 public char[] toCharArray(){ 3257 return layout(); 3258 } 3259 3260 3278 3279 public java.lang.String toString(){ 3280 return new java.lang.String (layout()); 3281 } 3282 3283 3295 3296 public java.math.BigInteger unscaledValue(){ 3297 com.ibm.icu.math.BigDecimal res=null; 3298 if (exp>=0) 3299 res=this; 3300 else 3301 { 3302 res=clone(this); res.exp=0; } 3305 return res.toBigInteger(); 3306 } 3307 3308 3329 3330 public static com.ibm.icu.math.BigDecimal valueOf(double dub){ 3331 return new com.ibm.icu.math.BigDecimal((new java.lang.Double (dub)).toString()); 3335 } 3336 3337 3347 3348 public static com.ibm.icu.math.BigDecimal valueOf(long lint){ 3349 return valueOf(lint,0); 3350 } 3351 3352 3374 3375 public static com.ibm.icu.math.BigDecimal valueOf(long lint,int scale){ 3376 com.ibm.icu.math.BigDecimal res=null; 3377 { 3378 if (lint==0) 3379 res=ZERO; 3380 else if (lint==1) 3381 res=ONE; 3382 else if (lint==10) 3383 res=TEN; 3384 else{ 3385 res=new com.ibm.icu.math.BigDecimal(lint); 3386 } 3387 } 3388 if (scale==0) 3389 return res; 3390 if (scale<0) 3391 throw new java.lang.NumberFormatException ("Negative scale:"+" "+scale); 3392 res=clone(res); res.exp=(int)-scale; return res; 3395 } 3396 3397 3398 3399 3400 3401 3410 3411 private char[] layout(){ 3412 char cmant[]; 3413 int i=0; 3414 java.lang.StringBuffer sb=null; 3415 int euse=0; 3416 int sig=0; 3417 char csign=0; 3418 char rec[]=null; 3419 int needsign; 3420 int mag; 3421 int len=0; 3422 cmant=new char[mant.length]; {int $18=mant.length;i=0;i:for(;$18>0;$18--,i++){ 3424 cmant[i]=(char)(mant[i]+((int)('0'))); 3425 } 3426 } 3427 3428 if (form!=com.ibm.icu.math.MathContext.PLAIN) 3429 { 3430 sb=new java.lang.StringBuffer (cmant.length+15); if (ind==isneg) 3432 sb.append('-'); 3433 euse=(exp+cmant.length)-1; 3435 if (form==com.ibm.icu.math.MathContext.SCIENTIFIC) 3436 { sb.append(cmant[0]); if (cmant.length>1) sb.append('.').append(cmant,1,cmant.length-1); 3440 } 3441 else 3442 {engineering:do{ 3443 sig=euse%3; if (sig<0) 3445 sig=3+sig; euse=euse-sig; 3447 sig++; 3448 if (sig>=cmant.length) 3449 { sb.append(cmant,0,cmant.length); 3451 {int $19=sig-cmant.length;for(;$19>0;$19--){ 3452 sb.append('0'); 3453 } 3454 } 3455 } 3456 else 3457 { sb.append(cmant,0,sig).append('.').append(cmant,sig,cmant.length-sig); 3459 } 3460 }while(false);} 3461 if (euse!=0) 3462 { 3463 if (euse<0) 3464 { 3465 csign='-'; 3466 euse=(int)-euse; 3467 } 3468 else 3469 csign='+'; 3470 sb.append('E').append(csign).append(euse); 3471 } 3472 rec=new char[sb.length()]; 3473 Utility.getChars(sb, 0,sb.length(),rec,0); 3474 return rec; 3475 } 3476 3477 3478 if (exp==0) 3479 { 3480 if (ind>=0) 3481 return cmant; rec=new char[cmant.length+1]; 3483 rec[0]='-'; 3484 java.lang.System.arraycopy((java.lang.Object )cmant,0,(java.lang.Object )rec,1,cmant.length); 3485 return rec; 3486 } 3487 3488 3489 needsign=(int)((ind==isneg)?1:0); 3491 3493 mag=exp+cmant.length; 3494 3495 if (mag<1) 3496 { 3497 len=(needsign+2)-exp; rec=new char[len]; 3499 if (needsign!=0) 3500 rec[0]='-'; 3501 rec[needsign]='0'; 3502 rec[needsign+1]='.'; 3503 {int $20=(int)-mag;i=needsign+2;i:for(;$20>0;$20--,i++){ rec[i]='0'; 3505 } 3506 } 3507 java.lang.System.arraycopy((java.lang.Object )cmant,0,(java.lang.Object )rec,(needsign+2)-mag,cmant.length); 3508 return rec; 3509 } 3510 3511 if (mag>cmant.length) 3512 { 3513 len=needsign+mag; 3514 rec=new char[len]; 3515 if (needsign!=0) 3516 rec[0]='-'; 3517 java.lang.System.arraycopy((java.lang.Object )cmant,0,(java.lang.Object )rec,needsign,cmant.length); 3518 {int $21=mag-cmant.length;i=needsign+cmant.length;i:for(;$21>0;$21--,i++){ rec[i]='0'; 3520 } 3521 } 3522 return rec; 3523 } 3524 3525 3526 len=(needsign+1)+cmant.length; 3527 rec=new char[len]; 3528 if (needsign!=0) 3529 rec[0]='-'; 3530 java.lang.System.arraycopy((java.lang.Object )cmant,0,(java.lang.Object )rec,needsign,mag); 3531 rec[needsign+mag]='.'; 3532 java.lang.System.arraycopy((java.lang.Object )cmant,mag,(java.lang.Object )rec,(needsign+mag)+1,cmant.length-mag); 3533 return rec; 3534 } 3535 3536 3539 3541 private int intcheck(int min,int max){ 3542 int i; 3543 i=this.intValueExact(); if ((i<min)|(i>max)) 3546 throw new java.lang.ArithmeticException ("Conversion overflow:"+" "+i); 3547 return i; 3548 } 3549 3550 3551 3598 3599 private com.ibm.icu.math.BigDecimal dodivide(char code,com.ibm.icu.math.BigDecimal rhs,com.ibm.icu.math.MathContext set,int scale){ 3600 com.ibm.icu.math.BigDecimal lhs; 3601 int reqdig; 3602 int newexp; 3603 com.ibm.icu.math.BigDecimal res; 3604 int newlen; 3605 byte var1[]; 3606 int var1len; 3607 byte var2[]; 3608 int var2len; 3609 int b2b; 3610 int have; 3611 int thisdigit=0; 3612 int i=0; 3613 byte v2=0; 3614 int ba=0; 3615 int mult=0; 3616 int start=0; 3617 int padding=0; 3618 int d=0; 3619 byte newvar1[]=null; 3620 byte lasthave=0; 3621 int actdig=0; 3622 byte newmant[]=null; 3623 3624 if (set.lostDigits) 3625 checkdigits(rhs,set.digits); 3626 lhs=this; 3628 if (rhs.ind==0) 3630 throw new java.lang.ArithmeticException ("Divide by 0"); if (lhs.ind==0) 3632 { if (set.form!=com.ibm.icu.math.MathContext.PLAIN) 3634 return ZERO; 3635 if (scale==(-1)) 3636 return lhs; 3637 return lhs.setScale(scale); 3638 } 3639 3640 3641 reqdig=set.digits; if (reqdig>0) 3643 { 3644 if (lhs.mant.length>reqdig) 3645 lhs=clone(lhs).round(set); 3646 if (rhs.mant.length>reqdig) 3647 rhs=clone(rhs).round(set); 3648 } 3649 else 3650 { 3651 if (scale==(-1)) 3652 scale=lhs.scale(); 3653 reqdig=lhs.mant.length; if (scale!=((int)-lhs.exp)) 3657 reqdig=(reqdig+scale)+lhs.exp; 3658 reqdig=(reqdig-((rhs.mant.length-1)))-rhs.exp; if (reqdig<lhs.mant.length) 3660 reqdig=lhs.mant.length; if (reqdig<rhs.mant.length) 3662 reqdig=rhs.mant.length; } 3664 3665 3666 newexp=((lhs.exp-rhs.exp)+lhs.mant.length)-rhs.mant.length; 3667 3668 if (newexp<0) 3669 if (code!='D') 3670 { 3671 if (code=='I') 3672 return ZERO; 3674 return clone(lhs).finish(set,false); 3675 } 3676 3677 3678 res=new com.ibm.icu.math.BigDecimal(); res.ind=(byte)(lhs.ind*rhs.ind); res.exp=newexp; res.mant=new byte[reqdig+1]; 3683 3684 newlen=(reqdig+reqdig)+1; 3686 var1=extend(lhs.mant,newlen); var1len=newlen; 3689 var2=rhs.mant; 3690 var2len=newlen; 3691 3692 3693 b2b=(var2[0]*10)+1; 3694 if (var2.length>1) 3695 b2b=b2b+var2[1]; 3696 3697 3698 have=0; 3699 {outer:for(;;){ 3700 thisdigit=0; 3701 3702 {inner:for(;;){ 3703 if (var1len<var2len) 3704 break inner; if (var1len==var2len) 3706 { {compare:do{ {int $22=var1len;i=0;i:for(;$22>0;$22--,i++){ 3709 if (i<var2.length) 3711 v2=var2[i]; 3712 else 3713 v2=(byte)0; 3714 if (var1[i]<v2) 3715 break inner; if (var1[i]>v2) 3717 break compare; } 3719 } 3720 3725 thisdigit++; 3726 res.mant[have]=(byte)thisdigit; 3727 have++; 3728 var1[0]=(byte)0; break outer; 3731 }while(false);} 3732 3733 ba=(int)var1[0]; } else 3736 { 3737 3738 ba=var1[0]*10; 3739 if (var1len>1) 3740 ba=ba+var1[1]; 3741 } 3742 3743 mult=(ba*10)/b2b; 3744 if (mult==0) 3745 mult=1; 3746 thisdigit=thisdigit+mult; 3747 var1=byteaddsub(var1,var1len,var2,var2len,(int)-mult,true); 3749 if (var1[0]!=0) 3750 continue inner; 3753 {int $23=var1len-2;start=0;start:for(;start<=$23;start++){ 3754 if (var1[start]!=0) 3755 break start; 3756 var1len--; 3757 } 3758 } 3759 if (start==0) 3760 continue inner; 3761 java.lang.System.arraycopy((java.lang.Object )var1,start,(java.lang.Object )var1,0,var1len); 3763 } 3764 } 3765 3766 3767 if ((have!=0)|(thisdigit!=0)) 3768 { res.mant[have]=(byte)thisdigit; 3770 have++; 3771 if (have==(reqdig+1)) 3772 break outer; if (var1[0]==0) 3774 break outer; } 3776 3777 if (scale>=0) 3778 if (((int)-res.exp)>scale) 3779 break outer; 3780 3781 if (code!='D') 3782 if (res.exp<=0) 3783 break outer; 3784 res.exp=res.exp-1; 3787 var2len--; 3788 } 3789 } 3790 3791 3792 if (have==0) 3794 have=1; 3796 if ((code=='I')|(code=='R')) 3797 { 3798 if ((have+res.exp)>reqdig) 3799 throw new java.lang.ArithmeticException ("Integer overflow"); 3800 3801 if (code=='R') 3802 {remainder:do{ 3803 3804 if (res.mant[0]==0) return clone(lhs).finish(set,false); if (var1[0]==0) 3807 return ZERO; res.ind=lhs.ind; 3811 padding=((reqdig+reqdig)+1)-lhs.mant.length; 3812 res.exp=(res.exp-padding)+lhs.exp; 3813 3814 3816 d=var1len; 3817 {i=d-1;i:for(;i>=1;i--){if(!((res.exp<lhs.exp)&(res.exp<rhs.exp)))break; 3818 if (var1[i]!=0) 3819 break i; 3820 d--; 3821 res.exp=res.exp+1; 3822 } 3823 } 3824 if (d<var1.length) 3825 { 3826 newvar1=new byte[d]; 3827 java.lang.System.arraycopy((java.lang.Object )var1,0,(java.lang.Object )newvar1,0,d); var1=newvar1; 3829 } 3830 res.mant=var1; 3831 return res.finish(set,false); 3832 }while(false);} 3833 } 3834 3835 else 3836 { 3837 if (var1[0]!=0) 3842 { lasthave=res.mant[have-1]; 3844 if (((lasthave%5))==0) 3845 res.mant[have-1]=(byte)(lasthave+1); 3846 } 3847 } 3848 3849 3850 if (scale>=0) 3852 {scaled:do{ 3853 if (have!=res.mant.length) 3855 res.exp=res.exp-((res.mant.length-have)); 3857 actdig=res.mant.length-((((int)-res.exp)-scale)); 3859 res.round(actdig,set.roundingMode); if (res.exp!=((int)-scale)) 3863 { 3864 res.mant=extend(res.mant,res.mant.length+1); 3865 res.exp=res.exp-1; 3866 } 3867 return res.finish(set,true); }while(false);} 3869 3870 if (have==res.mant.length) 3872 { res.round(set); 3874 have=reqdig; 3875 } 3876 else 3877 { 3878 if (res.mant[0]==0) 3879 return ZERO; newmant=new byte[have]; java.lang.System.arraycopy((java.lang.Object )res.mant,0,(java.lang.Object )newmant,0,have); 3885 res.mant=newmant; 3886 } 3887 return res.finish(set,true); 3888 } 3889 3890 3891 3892 3893 private void bad(char s[]){ 3894 throw new java.lang.NumberFormatException ("Not a number:"+" "+java.lang.String.valueOf(s)); 3895 } 3896 3897 3901 3902 private void badarg(java.lang.String name,int pos,java.lang.String value){ 3903 throw new java.lang.IllegalArgumentException ("Bad argument"+" "+pos+" "+"to"+" "+name+":"+" "+value); 3904 } 3905 3906 3912 3913 private static final byte[] extend(byte inarr[],int newlen){ 3914 byte newarr[]; 3915 if (inarr.length==newlen) 3916 return inarr; 3917 newarr=new byte[newlen]; 3918 java.lang.System.arraycopy((java.lang.Object )inarr,0,(java.lang.Object )newarr,0,inarr.length); 3919 return newarr; 3921 } 3922 3923 3950 3958 private static final byte[] byteaddsub(byte a[],int avlen,byte b[],int bvlen,int m,boolean reuse){ 3959 int alength; 3960 int blength; 3961 int ap; 3962 int bp; 3963 int maxarr; 3964 byte reb[]; 3965 boolean quickm; 3966 int digit; 3967 int op=0; 3968 int dp90=0; 3969 byte newarr[]; 3970 int i=0; 3971 3972 3973 3974 3975 alength=a.length; blength=b.length; ap=avlen-1; bp=bvlen-1; maxarr=bp; 3981 if (maxarr<ap) 3982 maxarr=ap; 3983 reb=(byte[])null; if (reuse) 3985 if ((maxarr+1)==alength) 3986 reb=a; if (reb==null) 3988 reb=new byte[maxarr+1]; 3990 quickm=false; if (m==1) 3992 quickm=true; else 3994 if (m==(-1)) 3995 quickm=true; 3997 digit=0; {op=maxarr;op:for(;op>=0;op--){ 3999 if (ap>=0) 4000 { 4001 if (ap<alength) 4002 digit=digit+a[ap]; ap--; 4004 } 4005 if (bp>=0) 4006 { 4007 if (bp<blength) 4008 { if (quickm) 4010 { 4011 if (m>0) 4012 digit=digit+b[bp]; else 4014 digit=digit-b[bp]; } 4016 else 4017 digit=digit+(b[bp]*m); 4018 } 4019 bp--; 4020 } 4021 4022 if (digit<10) 4023 if (digit>=0) 4024 {quick:do{ reb[op]=(byte)digit; 4026 digit=0; continue op; 4028 }while(false);} 4029 dp90=digit+90; 4030 reb[op]=bytedig[dp90]; digit=bytecar[dp90]; } 4033 } 4034 4035 if (digit==0) 4036 return reb; 4040 4041 newarr=(byte[])null; 4042 if (reuse) 4043 if ((maxarr+2)==a.length) 4044 newarr=a; if (newarr==null) 4046 newarr=new byte[maxarr+2]; 4047 newarr[0]=(byte)digit; if (maxarr<10) 4050 {int $24=maxarr+1;i=0;i:for(;$24>0;$24--,i++){ 4051 newarr[i+1]=reb[i]; 4052 } 4053 } 4054 else 4055 java.lang.System.arraycopy((java.lang.Object )reb,0,(java.lang.Object )newarr,1,maxarr+1); 4056 return newarr; 4057 } 4058 4059 4061 4062 private static final byte[] diginit(){ 4063 byte work[]; 4064 int op=0; 4065 int digit=0; 4066 work=new byte[(90+99)+1]; 4067 {op=0;op:for(;op<=(90+99);op++){ 4068 digit=op-90; 4069 if (digit>=0) 4070 { 4071 work[op]=(byte)(digit%10); 4072 bytecar[op]=(byte)(digit/10); continue op; 4074 } 4075 digit=digit+100; work[op]=(byte)(digit%10); 4078 bytecar[op]=(byte)((digit/10)-10); } 4080 } 4081 return work; 4082 } 4083 4084 4089 4090 private static final com.ibm.icu.math.BigDecimal clone(com.ibm.icu.math.BigDecimal dec){ 4091 com.ibm.icu.math.BigDecimal copy; 4092 copy=new com.ibm.icu.math.BigDecimal(); 4093 copy.ind=dec.ind; 4094 copy.exp=dec.exp; 4095 copy.form=dec.form; 4096 copy.mant=dec.mant; 4097 return copy; 4098 } 4099 4100 4104 4105 private void checkdigits(com.ibm.icu.math.BigDecimal rhs,int dig){ 4106 if (dig==0) 4107 return; if (this.mant.length>dig) 4110 if ((!(allzero(this.mant,dig)))) 4111 throw new java.lang.ArithmeticException ("Too many digits:"+" "+this.toString()); 4112 if (rhs==null) 4113 return; if (rhs.mant.length>dig) 4115 if ((!(allzero(rhs.mant,dig)))) 4116 throw new java.lang.ArithmeticException ("Too many digits:"+" "+rhs.toString()); 4117 } 4118 4119 4122 4123 private com.ibm.icu.math.BigDecimal round(com.ibm.icu.math.MathContext set){ 4124 return round(set.digits,set.roundingMode); 4125 } 4126 4127 4139 4140 private com.ibm.icu.math.BigDecimal round(int len,int mode){ 4141 int adjust; 4142 int sign; 4143 byte oldmant[]; 4144 boolean reuse=false; 4145 byte first=0; 4146 int increment; 4147 byte newmant[]=null; 4148 adjust=mant.length-len; 4149 if (adjust<=0) 4150 return this; 4152 exp=exp+adjust; sign=(int)ind; oldmant=mant; if (len>0) 4156 { 4157 mant=new byte[len]; 4159 java.lang.System.arraycopy((java.lang.Object )oldmant,0,(java.lang.Object )mant,0,len); 4160 reuse=true; first=oldmant[len]; } 4163 else 4164 { 4165 mant=ZERO.mant; 4166 ind=iszero; 4167 reuse=false; if (len==0) 4169 first=oldmant[0]; 4170 else 4171 first=(byte)0; } 4173 4174 increment=0; {modes:do{ 4177 if (mode==ROUND_HALF_UP) 4178 { if (first>=5) 4180 increment=sign; 4181 } 4182 else if (mode==ROUND_UNNECESSARY) 4183 { if ((!(allzero(oldmant,len)))) 4186 throw new java.lang.ArithmeticException ("Rounding necessary"); 4187 } 4188 else if (mode==ROUND_HALF_DOWN) 4189 { if (first>5) 4191 increment=sign; 4192 else 4193 if (first==5) 4194 if ((!(allzero(oldmant,len+1)))) 4195 increment=sign; 4196 } 4197 else if (mode==ROUND_HALF_EVEN) 4198 { if (first>5) 4200 increment=sign; 4201 else 4202 if (first==5) 4203 { 4204 if ((!(allzero(oldmant,len+1)))) 4205 increment=sign; 4206 else 4207 if ((((mant[mant.length-1])%2))==1) 4208 increment=sign; 4209 } 4210 } 4211 else if (mode==ROUND_DOWN){ 4212 }else if (mode==ROUND_UP) 4214 { if ((!(allzero(oldmant,len)))) 4216 increment=sign; 4217 } 4218 else if (mode==ROUND_CEILING) 4219 { if (sign>0) 4221 if ((!(allzero(oldmant,len)))) 4222 increment=sign; 4223 } 4224 else if (mode==ROUND_FLOOR) 4225 { if (sign<0) 4227 if ((!(allzero(oldmant,len)))) 4228 increment=sign; 4229 } 4230 else{ 4231 throw new java.lang.IllegalArgumentException ("Bad round value:"+" "+mode); 4232 } 4233 }while(false);} 4234 4235 if (increment!=0) 4236 {bump:do{ 4237 if (ind==iszero) 4238 { 4239 mant=ONE.mant; 4241 ind=(byte)increment; 4242 } 4243 else 4244 { 4245 if (ind==isneg) 4247 increment=(int)-increment; 4248 newmant=byteaddsub(mant,mant.length,ONE.mant,1,increment,reuse); 4249 if (newmant.length>mant.length) 4250 { exp++; 4253 java.lang.System.arraycopy((java.lang.Object )newmant,0,(java.lang.Object )mant,0,mant.length); 4255 } 4256 else 4257 mant=newmant; 4258 } 4259 }while(false);} 4260 if (exp>MaxExp) 4262 throw new java.lang.ArithmeticException ("Exponent Overflow:"+" "+exp); 4263 return this; 4264 } 4265 4266 4274 4275 private static final boolean allzero(byte array[],int start){ 4276 int i=0; 4277 if (start<0) 4278 start=0; 4279 {int $25=array.length-1;i=start;i:for(;i<=$25;i++){ 4280 if (array[i]!=0) 4281 return false; 4282 } 4283 } 4284 return true; 4285 } 4286 4287 4303 4304 private com.ibm.icu.math.BigDecimal finish(com.ibm.icu.math.MathContext set,boolean strip){ 4305 int d=0; 4306 int i=0; 4307 byte newmant[]=null; 4308 int mag=0; 4309 int sig=0; 4310 4311 if (set.digits!=0) 4312 if (this.mant.length>set.digits) 4313 this.round(set); 4314 4315 4317 if (strip) 4318 if (set.form!=com.ibm.icu.math.MathContext.PLAIN) 4319 { 4320 d=this.mant.length; 4321 4322 {i=d-1;i:for(;i>=1;i--){ 4323 if (this.mant[i]!=0) 4324 break i; 4325 d--; 4326 exp++; 4327 } 4328 } 4329 if (d<this.mant.length) 4330 { 4331 newmant=new byte[d]; 4332 java.lang.System.arraycopy((java.lang.Object )this.mant,0,(java.lang.Object )newmant,0,d); 4333 this.mant=newmant; 4334 } 4335 } 4336 4337 form=(byte)com.ibm.icu.math.MathContext.PLAIN; 4339 4340 {int $26=this.mant.length;i=0;i:for(;$26>0;$26--,i++){ 4341 if (this.mant[i]!=0) 4342 { 4343 if (i>0) 4346 {delead:do{ 4347 newmant=new byte[this.mant.length-i]; 4348 java.lang.System.arraycopy((java.lang.Object )this.mant,i,(java.lang.Object )newmant,0,this.mant.length-i); 4349 this.mant=newmant; 4350 }while(false);} 4351 mag=exp+mant.length; 4353 if (mag>0) 4354 { if (mag>set.digits) 4356 if (set.digits!=0) 4357 form=(byte)set.form; 4358 if ((mag-1)<=MaxExp) 4359 return this; } 4361 else 4362 if (mag<(-5)) 4363 form=(byte)set.form; 4364 4365 mag--; 4366 if ((mag<MinExp)|(mag>MaxExp)) 4367 {overflow:do{ 4368 if (form==com.ibm.icu.math.MathContext.ENGINEERING) 4370 { 4371 sig=mag%3; if (sig<0) 4373 sig=3+sig; mag=mag-sig; if (mag>=MinExp) 4377 if (mag<=MaxExp) 4378 break overflow; 4379 } 4380 throw new java.lang.ArithmeticException ("Exponent Overflow:"+" "+mag); 4381 }while(false);} 4382 return this; 4383 } 4384 } 4385 } 4386 4387 ind=iszero; 4389 { 4390 if (set.form!=com.ibm.icu.math.MathContext.PLAIN) 4391 exp=0; else if (exp>0) 4393 exp=0; else{ 4395 if (exp<MinExp) 4397 throw new java.lang.ArithmeticException ("Exponent Overflow:"+" "+exp); 4398 } 4399 } 4400 mant=ZERO.mant; return this; 4402 } 4403 } 4404 | Popular Tags |