BigInt icon indicating copy to clipboard operation
BigInt copied to clipboard

Negative number incorrect

Open username1565 opened this issue 7 years ago • 0 comments

  1. I cann't include your bigInt.js to html page, using
<script src="bigInt.js"></script>

The first error what there is displayed in console.log() is: Uncaught ReferenceError: str2bigInt is not defined So I used this old bigInt: http://icosahedral.net/script/BigInt.js

I did try to write function to convert bigInt to JS number (because bigInt seems as array in console.log).

Function, like this:


function bigInt2Int(x){
//May to return JS number if this absolute value of this
//is lesser or equals than "document.write(Number.MAX_SAFE_INTEGER);"
	var JSNum;
	if(negative(x)){//invalid array length, but I see 255 in the end of value
		if(
			!greater(
				x,
				str2bigInt(
					Number.MAX_SAFE_INTEGER.toString(),
					10,
					Number.MAX_SAFE_INTEGER.toString().length*4,
					0
				)
			)
		){//error
			throw new Error("Error: bigInt, with value greater than Number.MAX_SAFE_INTEGER ("+Number.MAX_SAFE_INTEGER+") cann't be converted correctly to JS number.");
		}else{//or convert to JS number
			JSNum = 0;
			for(i=x.length-1; i>=0;i--){
				JSNum = JSNum + x[i]*Math.pow(2, 15*i);
			}
			JSNum = 0-JSNum; //make negative number
		}
	}else{
		console.log('POSITIVE', integer.toString(), integer.toString().length);
		if(
			!greater(
				x,
				str2bigInt(
					Number.MAX_SAFE_INTEGER.toString(),
					10,
					Number.MAX_SAFE_INTEGER.toString().length*4,
					0
				)
			)
		){//then convert to JS number
			JSNum = 0;
			for(i=x.length-1; i>=0;i--){
				JSNum = JSNum + x[i]*Math.pow(2, 15*i);
			}
		}
		else{//or error
			throw new Error("Error: bigInt, with value greater than Number.MAX_SAFE_INTEGER ("+Number.MAX_SAFE_INTEGER+") cann't be converted correctly to JS number.");
		}
	}
	
	return JSNum;
}

//positive
console.log('Max_JS_number: = ', Number.MAX_SAFE_INTEGER); //2^53-1 = 9007199254740991
var integer = 9007199254740991; //using this
var bigInt_arr = str2bigInt(integer.toString(),10,integer.toString().length*4, 0); //too long array length

console.log(
	' integer', integer
	, '\n bigInteger array: ', bigInt_arr	//to big array length
	, '\n bitlength: integer.toString(2).length = ', integer.toString(2).length
);
console.log('bigInt2Int = ', bigInt2Int(bigInt_arr));

//negative
var integer = -9007199254740991; //negative JS nubmer
var bigInt_arr = str2bigInt(integer.toString(),10,integer.toString().length*4, 0); //long array length

console.log(
	' integer', integer
	, '\n bigInteger array: ', bigInt_arr	//this is the same array
	, '\n bitlength: integer.toString(2).length = ', integer.toString(2).length
);
console.log('bigInt2Int = ', bigInt2Int(bigInt_arr)); //This may be negative, but this is a positive number.

//comparison
console.log(
	(
		equals(
			str2bigInt(
				'9007199254740991',
				10,
				'9007199254740991'.toString().length*4,
				0
			),
			str2bigInt(
				'-9007199254740991'.toString(),
				10,
				'-9007199254740991'.toString().length*4,
				0
			)
		)
	)? true : false
);

As you can see, negative numbers equals to positive nubmers. There is no any function to get absolute value. funtion negative(x) not working correctly. Can you add all this to your bigInt.js? Also, you can make compatible this with HTML in your free time.

Best regards.

username1565 avatar Jul 26 '18 03:07 username1565