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 >"))))
>Solution :
JavaScript uses floating point numbesr 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.