- /*
- * Copyright 1999,2004 The Apache Software Foundation.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- package peaklau.eaglefund;
- /**
- * BigInteger class. <br>
- * This class implements the big integer operation, eg: add, mul, div <br>
- * <br>
- * @author peaklau <br>
- * email:<A HREF="mailto:peaklau@hotmail.com">peaklau@hotmail.com</A> <br>
- * <a href="http://www.peaklau.com/fund/english/">HomePage</a>
- * @version $Revision: 1.2 $ $Date: 2007/10/21 14:26:24 $
- */
- public class BigInteger {
- private static long MAX=1000000;
- public long n1=0;//low
- public long n2=0;
- public long n3=0;
- public long n4=0;
- public long n5=0;//high
- public boolean plus=true;
- public BigInteger(long n){
- if(n<0){
- plus=false;
- n=0-n;
- }
- n1=n%MAX;
- n2=(nMAX)%MAX;
- n3=(nMAXMAX)%MAX;
- n4=(nMAXMAXMAX)%MAX;
- n5=(nMAXMAXMAXMAX)%MAX;
- //System.out.println(n5+" "+n4+" "+n3+" "+n2+" "+n1);
- }
- public long getValue(){
- long tmp=(((n5*MAX+n4)*MAX+n3)*MAX+n2)*MAX+n1;
- //System.out.println("long "+(((((long)n5*MAX+n4)*MAX+n3)*MAX+n2)*MAX+n1));
- return plus?tmp:(0-tmp);
- }
- /**
- * compareIgnorePlus
- *
- * @param a
- * @param b
- * @return
- */
- private static int compareIgnorePlus(BigInteger a,BigInteger b){
- if(a.n5>b.n5){
- return 1;
- }else if(a.n5<b.n5){
- return -1;
- }else if(a.n4>b.n4){
- return 1;
- }else if(a.n4<b.n4){
- return -1;
- }else if(a.n3>b.n3){
- return 1;
- }else if(a.n3<b.n3){
- return -1;
- }else if(a.n2>b.n2){
- return 1;
- }else if(a.n2<b.n2){
- return -1;
- }else if(a.n1>b.n1){
- return 1;
- }else if(a.n1<b.n1){
- return -1;
- }else{
- return 0;
- }
- }
- /**
- * add
- * @param a
- * @param b
- * @return
- */
- public static BigInteger add(BigInteger a,BigInteger b){
- BigInteger tmp=new BigInteger(0);
- long aaa=0;
- if((a.plus&&b.plus)||(!a.plus&&!b.plus)){
- tmp.plus=a.plus;
- aaa=a.n1+b.n1;
- tmp.n1=aaa%MAX;
- aaa=a.n2+b.n2+aaaMAX;
- tmp.n2=aaa%MAX;
- aaa=a.n3+b.n3+aaaMAX;
- tmp.n3=aaa%MAX;
- aaa=a.n4+b.n4+aaaMAX;
- tmp.n4=aaa%MAX;
- aaa=a.n5+b.n5+aaaMAX;
- tmp.n5=aaa%MAX;
- }else if(compareIgnorePlus(a,b)>=0){
- tmp.plus=a.plus;
- aaa=MAX+a.n1-b.n1;
- tmp.n1=aaa%MAX;
- aaa=MAX+a.n2-b.n2-(aaa<MAX?1:0);
- tmp.n2=aaa%MAX;
- aaa=MAX+a.n3-b.n3-(aaa<MAX?1:0);
- tmp.n3=aaa%MAX;
- aaa=MAX+a.n4-b.n4-(aaa<MAX?1:0);
- tmp.n4=aaa%MAX;
- aaa=MAX+a.n5-b.n5-(aaa<MAX?1:0);
- tmp.n5=aaa%MAX;
- }else{
- tmp.plus=b.plus;
- aaa=MAX+b.n1-a.n1;
- tmp.n1=aaa%MAX;
- aaa=MAX+b.n2-a.n2-(aaa<MAX?1:0);
- tmp.n2=aaa%MAX;
- aaa=MAX+b.n3-a.n3-(aaa<MAX?1:0);
- tmp.n3=aaa%MAX;
- aaa=MAX+b.n4-a.n4-(aaa<MAX?1:0);
- tmp.n4=aaa%MAX;
- aaa=MAX+b.n5-a.n5-(aaa<MAX?1:0);
- tmp.n5=aaa%MAX;
- }
- return tmp;
- }
- /**
- * mul
- * @param a
- * @param b
- * @return
- */
- public static BigInteger mul(BigInteger a,BigInteger b){
- BigInteger tmp=new BigInteger(0);
- if((a.plus&&b.plus)||(!a.plus&&!b.plus)){
- tmp.plus=true;
- }else{
- tmp.plus=false;
- }
- long aaa=0;
- aaa=a.n1*b.n1;
- tmp.n1=aaa%MAX;
- aaa=a.n1*b.n2+a.n2*b.n1+aaaMAX;
- tmp.n2=aaa%MAX;
- aaa=a.n3*b.n1+a.n1*b.n3+a.n2*b.n2+aaaMAX;
- tmp.n3=aaa%MAX;
- aaa=a.n4*b.n1+a.n1*b.n4+a.n3*b.n2+a.n2*b.n3+aaaMAX;
- tmp.n4=aaa%MAX;
- aaa=a.n5*b.n1+a.n1*b.n5+a.n4*b.n2+a.n2*b.n4+a.n3*b.n3+aaaMAX;
- tmp.n5=aaa%MAX;
- return tmp;
- }
- /**
- * div
- * @param a
- * @param b
- * @return
- */
- public static BigInteger div(BigInteger a,BigInteger b){
- //System.out.println(a+" div "+b);
- BigInteger tmp=new BigInteger(0);
- if((a.plus&&b.plus)||(!a.plus&&!b.plus)){
- tmp.plus=true;
- }else{
- tmp.plus=false;
- }
- if(b.getValue()==0){
- return tmp;
- }
- if(compareIgnorePlus(a,b)<0){
- return tmp;
- }
- if(compareIgnorePlus(a,b)==0){
- BigInteger result=new BigInteger(1);
- result.plus=tmp.plus;
- return result;
- }
- long base=0;
- long range=Long.MAX_VALUE;
- while(range!=0){
- if(compareIgnorePlus(mul(new BigInteger(base+range),b),a)>0){
- range=range2;
- }else{
- base+=range;
- }
- //System.out.println("base="+base);
- }
- BigInteger result=new BigInteger(base);
- result.plus=tmp.plus;
- return result;
- }
- public String toString() {
- StringBuffer buffer = new StringBuffer();
- buffer.append("BigInteger[");
- buffer.append("MAX = ").append(MAX);
- buffer.append(", n1 = ").append(n1);
- buffer.append(", n2 = ").append(n2);
- buffer.append(", n3 = ").append(n3);
- buffer.append(", n4 = ").append(n4);
- buffer.append(", n5 = ").append(n5);
- buffer.append(", plus = ").append(plus);
- buffer.append(", value = ").append(this.getValue());
- buffer.append("]");
- return buffer.toString();
- }
- /*
- public static void main(String[]args){
- long a=31758;
- long b=100000;
- //BigInteger bi=new BigInteger(a);
- //System.out.println(bi.getValue());
- //System.out.println(compareIgnorePlus(new BigInteger(a),new BigInteger(b)));
- //System.out.println(add(new BigInteger(a),new BigInteger(b)).getValue()+" "+(a+b));
- //System.out.println(mul(new BigInteger(a),new BigInteger(b)).getValue()+" "+((long)a*(long)b));
- //System.out.println(div(new BigInteger(a),new BigInteger(b)).getValue()+" "+((long)a/(long)b));
- //System.out.println(div(mul(new BigInteger(a),new BigInteger(b)),new BigInteger(10000)).getValue()+" "+((long)a*(long)b/(long)10000L));
- long percent=-5217;
- long quantity=2000000000;
- long value=25934;
- BigInteger oldValue=BigInteger.div(BigInteger.mul(new BigInteger(value),new BigInteger(1000000)),new BigInteger(percent+1000000));
- System.out.println("oldValue "+oldValue.getValue());
- BigInteger tmp1=BigInteger.mul(BigInteger.mul(oldValue,new BigInteger(quantity)),new BigInteger(percent));
- System.out.println("tmp1 "+tmp1.getValue());
- BigInteger tmp2=BigInteger.mul(BigInteger.mul(oldValue,new BigInteger(quantity)),new BigInteger(10000));
- System.out.println("tmp2 "+tmp2.getValue());
- BigInteger addPerDayPerFund=BigInteger.div(tmp1,BigInteger.mul(new BigInteger(10000),new BigInteger(1000000)));
- System.out.println("addPerDayPerFund "+addPerDayPerFund.getValue());
- //System.out.println(div(mul(new BigInteger(a),new BigInteger(b)),new BigInteger(10000)).getValue()+" "+((long)a*(long)b/(long)10000L));
- }
- */
- }