Académique Documents
Professionnel Documents
Culture Documents
in format 'X.YEZ'");
try{important[3]="1";
throw new IllegalArgumentException("Error, too many 'E' in strin
g");
}
catch(IndexOutOfBoundsException e){}
if(Value>=10){
throw new IllegalArgumentException ("String must contain numbers
in format 'X.YEZ' where -10>X>10");}
if(important[1].compareTo(String.valueOf(E))!=0){
throw new IllegalArgumentException ("String must contain numbers
in format 'X.YEZ where Z is a whole number'");}
size = E+1;
//since E is 10^X there must be X+1 terms
int negShift;
int Val;
if(Value>1){//if positive dont need to do anything
Val= Integer.parseInt(""+important[0].charAt(0));//can only pars
eInt strings
negShift=0;
}
else{//add the negative to the initial digit
Val= Integer.parseInt(""+important[0].charAt(0)+important[0].cha
rAt(1));
negShift=1;
}
Numbers = new int[important[0].length()-1-negShift];
Numbers[0]=Val;
for(int i=1;i<important[0].length()-1-negShift;i++){
Numbers[i]= Character.getNumericValue(important[0].charAt(i+1+ne
gShift)); }
}
catch(NumberFormatException|IndexOutOfBoundsException e){
throw new IllegalArgumentException ("String must contain numbers in
format 'X.YEZ' and limit of 2147483643");
}
}
if(this.Numbers[0]>0){signThis=1;}
else{signThis=-1;}
if(h.Numbers[0]>0){signThat=1;}
else{signThat=-1;}
for(int i=newSize-1;i>=0;i++){
if(shorts[i-range]!=longs[i]){break;}
end++;//end values that zero out
}
}
// shorter use finished
newSizeCancel = front + end;
/*if(newSize>Integer.MAX_VALUE-5){ //making sure size is within arra
y limits
throw new IllegalArgumentException("Size of new list is too big!
");//exceeded max list*/
newSize-=newSizeCancel;
newArray = new int[newSize];
for(int i=newSize-1;i>0;i++){
if(shorter+range>i&&longer>i){newArray[i]=abs(shorts[i+front]*si
gnShort+longs[i+front-range]*signLong);}
else{
if(longer>i){
newArray[i]=longs[i+front];}
else if(shorter+range>i){
newArray[i]=longs[i+front];}
}
}
h
HugeInteger newInteger = h;
newInteger.Numbers[0]*=(-1);
return add(newInteger);
}
public int compareTo(HugeInteger h){//returns which integer is bigger
if(this.size>h.size){return 1;}
else if (this.size<h.size){return -1;}
else{
int i=0;
int maxSize;
if(this.size>h.size){maxSize=this.size;}
else{maxSize=h.size;}
while(i<maxSize){
if(this.Numbers[i]!=h.Numbers[i]){break;}
}
return compareInt(this.Numbers[i],h.Numbers[i]);
}
}
public int compareInt(int n, int m){
if(n>m){return 1;}
else if (n<m){return -1;}
else{return 0;}
}
public int abs(int n){//returns absolute value
if(n<0){
return n*(-1);
}
else{
return n;
}
}
public HugeInteger multiply(HugeInteger h){
String[] multiThis = this.toString().split("E");//gets individual decima
l and power
String[] multiThat = h.toString().split("E");
int totalSize = Integer.parseInt(multiThis[1])+Integer.parseInt(multiTha
t[1]);
if(totalSize<0){
throw new IllegalArgumentException("Number is to large for array to
handle!");
}
double Decimal = Double.parseDouble(multiThis[0])*Double.parseDouble(mul
tiThat[0]);
if(Decimal>10){//will only happen once as the max can be (9.9999...)*(9.
9999...) = 99.9999...
Decimal/=10;
totalSize+=1;
}
String newValue =String.valueOf(Decimal)+"E"+String.valueOf(totalSize);/
/creates string in correct foemat
return new HugeInteger(newValue);
}
@Override
public String toString(){