1. /*
  2. * Copyright 1999,2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package peaklau.eaglefund;
  17. /**
  18. * BigInteger class. <br>
  19. * This class implements the big integer operation, eg: add, mul, div <br>
  20. * <br>
  21. * @author peaklau <br>
  22. * email:<A HREF="mailto:peaklau@hotmail.com">peaklau@hotmail.com</A> <br>
  23. * <a href="http://www.peaklau.com/fund/english/">HomePage</a>
  24. * @version $Revision: 1.2 $ $Date: 2007/10/21 14:26:24 $
  25. */
  26. public class BigInteger {
  27. private static long MAX=1000000;
  28. public long n1=0;//low
  29. public long n2=0;
  30. public long n3=0;
  31. public long n4=0;
  32. public long n5=0;//high
  33. public boolean plus=true;
  34. public BigInteger(long n){
  35. if(n<0){
  36. plus=false;
  37. n=0-n;
  38. }
  39. n1=n%MAX;
  40. n2=(nMAX)%MAX;
  41. n3=(nMAXMAX)%MAX;
  42. n4=(nMAXMAXMAX)%MAX;
  43. n5=(nMAXMAXMAXMAX)%MAX;
  44. //System.out.println(n5+" "+n4+" "+n3+" "+n2+" "+n1);
  45. }
  46. public long getValue(){
  47. long tmp=(((n5*MAX+n4)*MAX+n3)*MAX+n2)*MAX+n1;
  48. //System.out.println("long "+(((((long)n5*MAX+n4)*MAX+n3)*MAX+n2)*MAX+n1));
  49. return plus?tmp:(0-tmp);
  50. }
  51. /**
  52. * compareIgnorePlus
  53. *
  54. * @param a
  55. * @param b
  56. * @return
  57. */
  58. private static int compareIgnorePlus(BigInteger a,BigInteger b){
  59. if(a.n5>b.n5){
  60. return 1;
  61. }else if(a.n5<b.n5){
  62. return -1;
  63. }else if(a.n4>b.n4){
  64. return 1;
  65. }else if(a.n4<b.n4){
  66. return -1;
  67. }else if(a.n3>b.n3){
  68. return 1;
  69. }else if(a.n3<b.n3){
  70. return -1;
  71. }else if(a.n2>b.n2){
  72. return 1;
  73. }else if(a.n2<b.n2){
  74. return -1;
  75. }else if(a.n1>b.n1){
  76. return 1;
  77. }else if(a.n1<b.n1){
  78. return -1;
  79. }else{
  80. return 0;
  81. }
  82. }
  83. /**
  84. * add
  85. * @param a
  86. * @param b
  87. * @return
  88. */
  89. public static BigInteger add(BigInteger a,BigInteger b){
  90. BigInteger tmp=new BigInteger(0);
  91. long aaa=0;
  92. if((a.plus&&b.plus)||(!a.plus&&!b.plus)){
  93. tmp.plus=a.plus;
  94. aaa=a.n1+b.n1;
  95. tmp.n1=aaa%MAX;
  96. aaa=a.n2+b.n2+aaaMAX;
  97. tmp.n2=aaa%MAX;
  98. aaa=a.n3+b.n3+aaaMAX;
  99. tmp.n3=aaa%MAX;
  100. aaa=a.n4+b.n4+aaaMAX;
  101. tmp.n4=aaa%MAX;
  102. aaa=a.n5+b.n5+aaaMAX;
  103. tmp.n5=aaa%MAX;
  104. }else if(compareIgnorePlus(a,b)>=0){
  105. tmp.plus=a.plus;
  106. aaa=MAX+a.n1-b.n1;
  107. tmp.n1=aaa%MAX;
  108. aaa=MAX+a.n2-b.n2-(aaa<MAX?1:0);
  109. tmp.n2=aaa%MAX;
  110. aaa=MAX+a.n3-b.n3-(aaa<MAX?1:0);
  111. tmp.n3=aaa%MAX;
  112. aaa=MAX+a.n4-b.n4-(aaa<MAX?1:0);
  113. tmp.n4=aaa%MAX;
  114. aaa=MAX+a.n5-b.n5-(aaa<MAX?1:0);
  115. tmp.n5=aaa%MAX;
  116. }else{
  117. tmp.plus=b.plus;
  118. aaa=MAX+b.n1-a.n1;
  119. tmp.n1=aaa%MAX;
  120. aaa=MAX+b.n2-a.n2-(aaa<MAX?1:0);
  121. tmp.n2=aaa%MAX;
  122. aaa=MAX+b.n3-a.n3-(aaa<MAX?1:0);
  123. tmp.n3=aaa%MAX;
  124. aaa=MAX+b.n4-a.n4-(aaa<MAX?1:0);
  125. tmp.n4=aaa%MAX;
  126. aaa=MAX+b.n5-a.n5-(aaa<MAX?1:0);
  127. tmp.n5=aaa%MAX;
  128. }
  129. return tmp;
  130. }
  131. /**
  132. * mul
  133. * @param a
  134. * @param b
  135. * @return
  136. */
  137. public static BigInteger mul(BigInteger a,BigInteger b){
  138. BigInteger tmp=new BigInteger(0);
  139. if((a.plus&&b.plus)||(!a.plus&&!b.plus)){
  140. tmp.plus=true;
  141. }else{
  142. tmp.plus=false;
  143. }
  144. long aaa=0;
  145. aaa=a.n1*b.n1;
  146. tmp.n1=aaa%MAX;
  147. aaa=a.n1*b.n2+a.n2*b.n1+aaaMAX;
  148. tmp.n2=aaa%MAX;
  149. aaa=a.n3*b.n1+a.n1*b.n3+a.n2*b.n2+aaaMAX;
  150. tmp.n3=aaa%MAX;
  151. aaa=a.n4*b.n1+a.n1*b.n4+a.n3*b.n2+a.n2*b.n3+aaaMAX;
  152. tmp.n4=aaa%MAX;
  153. aaa=a.n5*b.n1+a.n1*b.n5+a.n4*b.n2+a.n2*b.n4+a.n3*b.n3+aaaMAX;
  154. tmp.n5=aaa%MAX;
  155. return tmp;
  156. }
  157. /**
  158. * div
  159. * @param a
  160. * @param b
  161. * @return
  162. */
  163. public static BigInteger div(BigInteger a,BigInteger b){
  164. //System.out.println(a+" div "+b);
  165. BigInteger tmp=new BigInteger(0);
  166. if((a.plus&&b.plus)||(!a.plus&&!b.plus)){
  167. tmp.plus=true;
  168. }else{
  169. tmp.plus=false;
  170. }
  171. if(b.getValue()==0){
  172. return tmp;
  173. }
  174. if(compareIgnorePlus(a,b)<0){
  175. return tmp;
  176. }
  177. if(compareIgnorePlus(a,b)==0){
  178. BigInteger result=new BigInteger(1);
  179. result.plus=tmp.plus;
  180. return result;
  181. }
  182. long base=0;
  183. long range=Long.MAX_VALUE;
  184. while(range!=0){
  185. if(compareIgnorePlus(mul(new BigInteger(base+range),b),a)>0){
  186. range=range2;
  187. }else{
  188. base+=range;
  189. }
  190. //System.out.println("base="+base);
  191. }
  192. BigInteger result=new BigInteger(base);
  193. result.plus=tmp.plus;
  194. return result;
  195. }
  196. public String toString() {
  197. StringBuffer buffer = new StringBuffer();
  198. buffer.append("BigInteger[");
  199. buffer.append("MAX = ").append(MAX);
  200. buffer.append(", n1 = ").append(n1);
  201. buffer.append(", n2 = ").append(n2);
  202. buffer.append(", n3 = ").append(n3);
  203. buffer.append(", n4 = ").append(n4);
  204. buffer.append(", n5 = ").append(n5);
  205. buffer.append(", plus = ").append(plus);
  206. buffer.append(", value = ").append(this.getValue());
  207. buffer.append("]");
  208. return buffer.toString();
  209. }
  210. /*
  211. public static void main(String[]args){
  212. long a=31758;
  213. long b=100000;
  214. //BigInteger bi=new BigInteger(a);
  215. //System.out.println(bi.getValue());
  216. //System.out.println(compareIgnorePlus(new BigInteger(a),new BigInteger(b)));
  217. //System.out.println(add(new BigInteger(a),new BigInteger(b)).getValue()+" "+(a+b));
  218. //System.out.println(mul(new BigInteger(a),new BigInteger(b)).getValue()+" "+((long)a*(long)b));
  219. //System.out.println(div(new BigInteger(a),new BigInteger(b)).getValue()+" "+((long)a/(long)b));
  220. //System.out.println(div(mul(new BigInteger(a),new BigInteger(b)),new BigInteger(10000)).getValue()+" "+((long)a*(long)b/(long)10000L));
  221. long percent=-5217;
  222. long quantity=2000000000;
  223. long value=25934;
  224. BigInteger oldValue=BigInteger.div(BigInteger.mul(new BigInteger(value),new BigInteger(1000000)),new BigInteger(percent+1000000));
  225. System.out.println("oldValue "+oldValue.getValue());
  226. BigInteger tmp1=BigInteger.mul(BigInteger.mul(oldValue,new BigInteger(quantity)),new BigInteger(percent));
  227. System.out.println("tmp1 "+tmp1.getValue());
  228. BigInteger tmp2=BigInteger.mul(BigInteger.mul(oldValue,new BigInteger(quantity)),new BigInteger(10000));
  229. System.out.println("tmp2 "+tmp2.getValue());
  230. BigInteger addPerDayPerFund=BigInteger.div(tmp1,BigInteger.mul(new BigInteger(10000),new BigInteger(1000000)));
  231. System.out.println("addPerDayPerFund "+addPerDayPerFund.getValue());
  232. //System.out.println(div(mul(new BigInteger(a),new BigInteger(b)),new BigInteger(10000)).getValue()+" "+((long)a*(long)b/(long)10000L));
  233. }
  234. */
  235. }