Different binary outputs from js and py

Question:

I have tried turning a number into binary digits, which worked in both Python and JavaScript.
My issue is that they both return a different combination.

When I enter 585190997647163394,

JavaScript returns: 100000011111000001000001110010100100100001000000000000000000

Python returns: 100000011111000001000001110010100100100001000000000000000010

The penultimate digits in the binary combinations do not match.


Here is my code:

JavaScript:

var bin = (+in).toString(2);
console.log(bin);

Python:

print(bin(int(input("int >"))))
Asked By: River

||

Answers:

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.

Answered By: jabaa
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.