__BIGINT_THIS_FILE_IS( "add.js", "v0.5 beta 9", "2003-12-28" );

//////////////////////////////////////////////////////////////////////////////////////

Bigint.add = function( number1, number2 ) {
	var bigNumber1 = Bigint.serio( number1 );
	var bigNumber2 = Bigint.serio( number2 );
	var cmp;
	var bigResult;

	if( bigNumber1.sign >= 0 ) {
		if( bigNumber2.sign >= 0 ) {
			bigResult = Bigint._add( bigNumber1, bigNumber2 );
			bigResult.sign = bigResult.isZero()? 0 : (+1) ;
		} else {
			cmp = Bigint._tablecmp( bigNumber1.table, bigNumber2.table );
			if( cmp > 0 ) {
				bigResult = Bigint._sub( bigNumber1, bigNumber2 );
				bigResult.sign = (+1);
			} else if( cmp === 0 ) {
				return Bigint(0);
			} else {
				bigResult = Bigint._sub( bigNumber2, bigNumber1 );
				bigResult.sign = (-1);
			}
		}
	} else {
		if( bigNumber2.sign >= 0 ) {
			cmp = Bigint._tablecmp( bigNumber1.table, bigNumber2.table );
			if( cmp > 0 ) {
				bigResult = Bigint._sub( bigNumber1, bigNumber2 );
				bigResult.sign = (-1);
			} else if( cmp === 0 ) {
				return Bigint(0);
			} else {
				bigResult = Bigint._sub( bigNumber2, bigNumber1 );
				bigResult.sign = (+1);
			}
		} else {
			bigResult = Bigint._add( bigNumber1, bigNumber2 );
			bigResult.sign = (-1);
		}
	}
	return bigResult;
}

Bigint._add = function( bigNumber1, bigNumber2 ) {
	var bigSum = new Bigint();

	var tableSize = Math.max( bigNumber1.table.length, bigNumber2.table.length );

	var flag = 0;
	for( var i=0; i < tableSize || flag ===1 ; i++ ) {
		if( bigNumber1.table[i] === void 0 ) bigNumber1.table[i] = 0;
		if( bigNumber2.table[i] === void 0 ) bigNumber2.table[i] = 0;

		bigSum.table[i] = bigNumber1.table[i] + bigNumber2.table[i] + flag;
		if( bigSum.table[i] >= __BIGINT_BIG_UNIT ) {
			flag = 1;
			bigSum.table[i] -= __BIGINT_BIG_UNIT;
		} else {
			flag = 0;
		}
	}

	return bigSum;
}


Bigint._add_typeN = function( bigNumber, smallNumber ) {
	var bigResult = bigNumber.copy();
	bigResult.table[0] += smallNumber;

	var i = 0;
	var flag = 0;
	for( ; i<bigResult.table.length; i++ ) {
		bigResult.table[i] += flag;
		if( bigResult.table[i] >= __BIGINT_BIG_UNIT ) {
			bigResult.table[i] -= __BIGINT_BIG_UNIT;
			flag = 1;
		} else {
			flag = 0;
			break;
		}
	}	

	if( flag ) {
		bigResult.table[i] = 1;
	}

	return bigResult;
}

Bigint.prototype.increment = function( input ) {
	if( input === void 0 ) {
		this._increment( 1 );
	} else if( input.isBigint || input >= __BIGINT_BIG_UNIT || input < 0 || this.sign < 0 ) {
		var bigResult = Bigint.add( this , input );
		this.sign = bigResult.sign;
		this.table = bigResult.table;
	} else {
		var n = Math.floor( input - 0 );
		if( isNaN( n ) ) {
			Bigint._error( "increment" , "Bad input: " + input );
			this.setNaB();
		} else {
			this._increment( n );
		}
	}
}

Bigint.prototype._increment = function( n ) {
	if( n === void 0 ) n = 1;
	this.table[0] += n;
	var i = 0;
	var flag = 0;
	for( ; i < this.table.length; i++ ) {
		this.table[i] += flag;
		if( this.table[i] >= __BIGINT_BIG_UNIT ) {
			this.table[i] -= __BIGINT_BIG_UNIT;
			flag = 1;
		} else {
			flag = 0;
			break;
		}
	}	

	if( flag ) {
		this.table[i] = 1;
	}
}

Bigint._stradd = function( strNumber1, strNumber2 ) {
	var biggerTable = new Array();

	var n1, n2;
	var flag = 0;
	var i = 0;
	var pos1 = strNumber1.length;
	var pos2 = strNumber2.length;
	for( ; pos1 > 0 || pos2 > 0; pos1 -= __BIGINT_BIGGER_POWER, pos2 -= __BIGINT_BIGGER_POWER, i++ ) {
		n1 = strNumber1.substring( pos1 - __BIGINT_BIGGER_POWER, pos1 ) - 0;
		n2 = strNumber2.substring( pos2 - __BIGINT_BIGGER_POWER, pos2 ) - 0;

		var nResult = n1 + n2 + flag;
		if( nResult >= __BIGINT_BIGGER_UNIT ) {
			biggerTable[i] = nResult - __BIGINT_BIGGER_UNIT;
			flag = 1;
		} else {
			biggerTable[i] = nResult;
			flag = 0;
		}
	}

	if( flag ) biggerTable[i] = 1;

	var lastIndex = biggerTable.length - 1;
	var strResult = biggerTable[ lastIndex ] + "";

	for( var i = lastIndex - 1; i >= 0; i-- ) {
		strResult += Bigint._paddingBigger( biggerTable[ i ] );
	}
	return strResult;
}