Bug with outputting long numbers in javascript - javascript

Here is the code:
var q = 10000000000000011;
console.log(q);
Output will be:
10000000000000012
If I try to output 10000000000000010 or 10000000000000012, everything is fine.
Conversion to string doesn't help either.
How can I avoid this bug?

The maximum safe integer in JavaScript (2^53 - 1). which is 9007199254740991. You will need to use a big integer library to store such large numbers

As has been pointed out, the maximum integer size in JS is Number.MAX_SAFE_INTEGER (9007199254740991)
So if you want larger numbers, you'll have to get creative and use something like scientific notation:
10000000000000011 ~= 1 × 10^9 (1e9)

Related

Different binary outputs from js and py

So, I am trying to turn a number to binary, which seems fine in both javascript and python, but my issue is that when I do so, they both return a different binary string.
I enter: 585190997647163394
JavaScript returns: 100000011111000001000001110010100100100001000000000000000000
Python returns: 100000011111000001000001110010100100100001000000000000000010
If you can´t see the difference, the penultimate digit in the string python has returned is a 1 instead of a 0, like in the javascript string.
Please explain to me how this can happen/how to fix it.
Thank you!
Here is the code if needed:
js:
var bin = (+in).toString(2);
console.log(bin);
py:
print(bin(int(input("bingledingle >"))))
JavaScript uses floating point numbers with double precision. 585190997647163394 is too large.
585190997647163394 > Number.MAX_SAFE_INTEGER
The number is rounded to 585190997647163392.
You can use BigInt instead.
Python has numbers with arbitrary precision. Numbers are stored as strings. Python doesn't round the number to store it.

How to use JavaScript to handle complex 100 digits (+/-) times 100 digits (+/-) multiplication without having an overflow error?

When I tried BigInt, the result returned a wrong result:
BigInt(123456789123456789*111111111111)
13717421013703702653171662848n
And this is what the actual result should be like by hand-written calculation:
13717421013703703578986282579
Is there a way to produce the correct result without this error? Thank you.
Instead of passing the multiplication to BigInt, convert both numbers to BigInt and multiply them after that:
const big = 123456789123456789n * 111111111111n;
console.log(big)
Note, use your browser's console, not the snippet's one (also, this won't work in Safari).

How to convert from Base36 to Base10 in JS

I have a base36 number 00001CGUMZYCB99J
But if I try convert it in JavaScript to base 10 with
parseInt("00001CGUMZYCB99J", 36);
I get wrong results like 177207000002463650 or 177207000002463648. The expected result is 177207000002463655. I found two websites that get the result right anyway: translatorscafe and dcode.
But how can I do this in JS?
The outcome of that conversion exceeds Number.MAX_SAFE_INTEGER (i.e. the base-10 value is too large to fit in a JavaScript integer), which means you need some sort of arbitrary precision library to do the conversion, for instance biginteger:
const BigInteger = require('biginteger').BigInteger;
let value = BigInteger.parse('00001CGUMZYCB99J', 36);
console.log( value.toString() ) // 177207000002463655
JavaScript stores all values in a double. Therefore, large numbers will have some of the less significant digits changed. If you want to deal with large numbers, you have to use a special library like this BigInteger one.
If you use this library, you can convert between bases like this:
BigInteger.parse("00001CGUMZYCB99J", 36);
Keep in mind that you need to keep using the library, you can't convert it back into a normal number, or you will face the same problem.

parseInt changes the integer

I am trying to pull a number (72157648141531978), which starts at the 21st character, out of the title of a page like so:
parseInt(document.title.substring(21), 10);
This returns the string as an integer of 72157648141531980. I can't seem to figure out why it is changing the last two numbers. Any help would be appreciated.
According to What is JavaScript's highest integer value that a Number can go to without losing precision? the max value of an integer is 9007199254740992.
I tried your calculation on http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_parseint and I can confirm your problem.
It looks like an issue parsing beyond this max value and it is rounding the last 2 figures.
You have exceeded the limits of double-precision floating-point format, as used by JavaScript. You cannot use that precise number directly in JavaScript. You can use it as a string, but if you need to do arithmetic on it you will need a bignum library.

Javascript can't render this number correctly: 3494793310847464221

I have an interesting question, I have been doing some work with javascript and a database ID came out as "3494793310847464221", now this is being entered into javascript as a number yet it is using the number as a different value, both when output to an alert and when being passed to another javascript function.
Here is some example code to show the error to its fullest.
<html><head><script language="javascript">alert( 3494793310847464221);
var rar = 3494793310847464221;
alert(rar);
</script></head></html>
This has completly baffeled me and for once google is not my friend...
btw the number is 179 more then the number there...
Your number is larger than the maximum allowed integer value in javascript (2^53). This has previously been covered by What is JavaScript's highest integer value that a Number can go to without losing precision?
In JavaScript, all numbers (even integral ones) are stored as IEEE-754 floating-point numbers. However, FPs have limited "precision" (see the Wikipedia article for more info), so your number isn't able to be represented exactly.
You will need to either store your number as a string or use some other "bignum" approach (unfortunately, I don't know of any JS bignum libraries off the top of my head).
Edit: After doing a little digging, it doesn't seem as if there's been a lot of work done in the way of JavaScript bignum libraries. In fact, the only bignum implementation of any kind that I was able to find is Edward Martin's JavaScript High Precision Calculator.
Use a string instead.
179 more is one way to look at it. Another way is, after the first 16 digits, any further digit is 0. I don't know the details, but it looks like your variable only stores up to 16 digits.
That number exceeds (2^31)-1, and that's the problem; javascript uses 32-bit signed integers (meaning, a range from –2,147,483,648 to 2,147,483,647). Your best choice is to use strings, and create functions to manipulate the strings as numbers.
I wouldn't be all too surprised, if there already was a library that does what you need.
One possible solution is to use a BigInt library such as: http://www.leemon.com/crypto/BigInt.html
This will allow you to store integers of arbitrary precision, but it will not be as fast as standard arithmetic.
Since it's to big to be stored as int, it's converted to float. In JavaScript ther is no explicit integer and float types, there's only universal Number type.
"Can't increment and decrement a string easily..."
Really?
function incr_num(x) {
var lastdigit=Number(x.charAt(x.length-1));
if (lastdigit!=9) return (x.substring(0,x.length-1))+""+(lastdigit+1);
if (x=="9") return "10";
return incr_num(x.substring(0,x.length-1))+"0";
}
function decr_num(x) {
if(x=="0") return "(error: cannot decrement zero)";
var lastdigit=Number(x.charAt(x.length-1));
if (lastdigit!=0) return (x.substring(0,x.length-1))+""+(lastdigit-1);
if (x=="10") return "9"; // delete this line if you like leading zero
return decr_num(x.substring(0,x.length-1))+"9";
}
Just guessing, but perhaps the number is stored as a floating type, and the difference might be because of some rounding error. If that is the case it might work correctly if you use another interpreter (browser, or whatever you are running it in)

Categories